Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6076553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:37:15+00:00 2026-05-23T10:37:15+00:00

I use mongodb for my blog platform, where users can create their own blogs.

  • 0

I use mongodb for my blog platform, where users can create their own blogs. All entries from all blogs are in an entries collection. The document of an entry looks like:

{
  'blog_id':xxx,
  'timestamp':xxx,
  'title':xxx,
  'content':xxx
}

As the question says, is there any way to select, say, last 3 entries for each blog?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T10:37:16+00:00Added an answer on May 23, 2026 at 10:37 am

    The only way to do this in basic mongo if you can live with two things :

    • An additional field in your entry document, let’s call it “age”
    • A new blog entry taking an additional update

    If so, here’s how you do it :

    1. Upon creating a new intro do your normal insert and then execute this update to increase the age of all posts (including the one you just inserted for this blog) :

      db.entries.update({blog_id: BLOG_ID}, {age:{$inc:1}}, false, true)

    2. When querying, use the following query which will return the most recent 3 entries for each blog :

      db.entries.find({age:{$lte:3}, timestamp:{$gte:STARTOFMONTH, $lt:ENDOFMONTH}}).sort({blog_id:1, age:1})

    Note that this solution is actually concurrency safe (no entries with duplicate ages).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We use MongoDB to collect logs on pageviews. $collection_name = logs..date('Y')...date('m')...date('d'); $collection = $this->Mongo->$collection_name;
Hello everyone I use MongoDB find() method like this: $cursor = $collection->find(); foreach($cursor as
I use python + mongodb to store some item ranking data in a collection
i use mongodb and save file to gridfs now i want edit images from
I'm trying to use MongoDB to cache some data for me, but I can't
I would like to use mongodb directly from the client-side, using javascript inside my
Could you please tell me why I can't delete from collection records with ids
I have a good reason to use MongoDB for part of my app. But
Is it a good idea to use MongoDB in .NET desktop application?
I'm implementing blog based on MongoDB. Let's look at first how Mongo guys recommend

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.