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 7433179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:34:29+00:00 2026-05-29T09:34:29+00:00

Mongo has the nice operator $slice, which let’s you only retrieve a sub sets

  • 0

Mongo has the nice operator $slice, which let’s you only retrieve a sub sets of an entiry’s embedded array. From their official docs:

db.posts.find({}, {comments:{$slice: 5}}) // first 5 comments
db.posts.find({}, {comments:{$slice: -5}}) // last 5 comments
db.posts.find({}, {comments:{$slice: [20, 10]}}) // skip 20, limit 10
db.posts.find({}, {comments:{$slice: [-20, 10]}}) // 20 from end, limit 10

However I can’t find where it sais how the embedded array’s elements are ordered before being fetched this way. And, more important, can I change the ordering before $slicing them?

  • 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-29T09:34:30+00:00Added an answer on May 29, 2026 at 9:34 am

    Unfortunately you can’t do this at current moment. Mongodb $slice always return documents with nested array in default (how it was inserted) order and you can’t apply any other order.

    Some notes:

    1. Embedded arrays always keep their order (as items was inserted)
    2. Currently if you filter on embedded array values, mongodb return all nested array values, not only matched. See more details in the jira bug.

    In the next example filtering will be applied only on a root documents, and when you use $slice it return first 5 comments, but not from matched by filter “comments.name“:

    db.posts.find({"comments.name": "Test comment"}, {comments:{$slice: 5}})
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I start with replica sets and each mongo server has a 500G
I have a Mongo collection called documents which has the following structure: { name:
John Nunemaker has a blog post with some nice tips about Mongo ObjectIds --
I have a Mongo collection where each document has a set of unique embedded
So I have an embedded document that tracks group memberships. Each embedded document has
I'm implementing blog based on MongoDB. Let's look at first how Mongo guys recommend
I have a collection which is an action log between two users. It has
I am currently involved in some interesting programming language research which has, up until
Rails' ActiveRecord has a feature called Query Caching (ActiveRecord::QueryCache) which saves the result of
meta_map = {} results = db.meta.find({'corpus_id':id, 'method':method}) #this Mongo query only takes 3ms print

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.