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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:24:45+00:00 2026-05-26T02:24:45+00:00

I have a collection I’d like to query as follows: return all documents up

  • 0

I have a collection I’d like to query as follows:

  • return all documents
  • up to 2 comments (e.g. a slice, either 0, 1, or 2 comments)
  • all comments must have views > 10

It seems like I need to create a function to evaluate each document individually, but it’s not clear how that’s done, particularly given I want to do a slice and return up to n items matching that criteria.

Sample schema:

{
    title: "One",
    comments: [
        {
            title: "comment1",
            views: 9
        },
        {
            title: "comment2",
            views: 10
        },
        {
            title: "comment3",
            views: 11
        },
        {
            title: "comment4",
            views: 12
        },
    ]
}

I want to do something like:

db.collection.find({"comments.views": {$gt: 10}}, {comments:{$slice: 2}})

But this returns any document with a comment with > 10 views, and then slices 2 comments… I want to return on those comments which have > 10 items. I cannot do it on the client AND use $slice without losing some comments, so I need to do it on the DB. Thoughts?

  • 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-26T02:24:45+00:00Added an answer on May 26, 2026 at 2:24 am

    But this returns any document with a comment with > 10 views, and then
    slices 2 comments

    This is the behavior of filtering multi level embedded document, normally the matching filter would return the whole document, not the subsets.

    Usually positional operator $ used to match the sub documents in updates. But the feature is not yet implemented in return specifiers.

    There is an outstanding issue already in mongo Support for positional ($) operator in fields to return specifier. (Please login to vote if you really needed the feature)

    So you have to handle the

    • up to 2 comments (e.g. a slice, either 0, 1, or 2 comments)

    part in your application by loop through all collections.

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

Sidebar

Related Questions

I have a collection of objects to which I'd like to just add a
I have a collection that holds multiple types of items that all inherit from
I have a collection where each document looks like this {access_key:'xxxxxxxxx', keyword: banana, count:12,
I have collection of documents, witch has an array, of embedded documents, how i
For example, I have collection with documents, where documents can have field url (but
I have collection as follows . { name : pqr , loc : [
I have a collection of documents consisting of several fields, and I need to
I have collection of documents Users User { Status: ACTIVE, Login: { UserName: login,
I have a collection of objects of one type that I'd like to convert
I have collection of documents Message in RavenDB. Definition: class Message { string Content;

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.