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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:21:08+00:00 2026-05-28T15:21:08+00:00

Lets say I have mongo documents like this: Question 1 { answers:[ {content: ‘answer1’},

  • 0

Lets say I have mongo documents like this:

Question 1

{
    answers:[
       {content: 'answer1'},
       {content: '2nd answer'}
    ]
}

Question 2

{
    answers:[
       {content: 'answer1'},
       {content: '2nd answer'}
       {content: 'The third answer'}
    ]
}

Is there a way to order the collection by size of answers?

After a little research I saw suggestions of adding another field, that would contain number of answers and use it as a reference but may be there is native way to do it?

  • 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-28T15:21:09+00:00Added an answer on May 28, 2026 at 3:21 pm

    I thought you might be able to use $size, but that’s only to find arrays of a certain size, not ordering.

    From the mongo documentation:
    http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-%24size

    You cannot use $size to find a range of sizes (for example: arrays with more than 1 element). If you need to query for a range, create an extra size field that you increment when you add elements. Indexes cannot be used for the $size portion of a query, although if other query expressions are included indexes may be used to search for matches on that portion of the query expression.

    Looks like you can probably fairly easily do this with the new aggregation framework, edit: which isn’t out yet.
    http://www.mongodb.org/display/DOCS/Aggregation+Framework

    Update Now the Aggregation Framework is out…

    > db.test.aggregate([
      {$unwind: "$answers"}, 
      {$group: {_id:"$_id", answers: {$push:"$answers"}, size: {$sum:1}}}, 
      {$sort:{size:1}}]);
    {
    "result" : [
        {
            "_id" : ObjectId("5053b4547d820880c3469365"),
            "answers" : [
                {
                    "content" : "answer1"
                },
                {
                    "content" : "2nd answer"
                }
            ],
            "size" : 2
        },
        {
            "_id" : ObjectId("5053b46d7d820880c3469366"),
            "answers" : [
                {
                    "content" : "answer1"
                },
                {
                    "content" : "2nd answer"
                },
                {
                    "content" : "The third answer"
                }
            ],
            "size" : 3
        }
      ],
      "ok" : 1
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I have an array like this: string [] Filelist = ... I
Lets say i have a piece of code like this: test pattern | pattern
Lets say I have a p tag like this: <div id = example> <p
So lets say I have an array containing a hash like this: [{head=> {title=>$20,000
Let's say I have some json like this in mongo: {n:5} and a java
Lets say have this immutable record type: public class Record { public Record(int x,
Lets say we have something like: <div class=row> <div class=box> <a class=more href=#more/> </div>
Lets say we have this code: <div id='m1'> <div class=c></div> <div class=c></div> <div class=c></div>
lets say I have a Datetime column in DB. It has a data like
Lets say I have this array, int[] numbers = {1, 3, 4, 9, 2};

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.