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

  • Home
  • SEARCH
  • 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 8224979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T15:15:27+00:00 2026-06-07T15:15:27+00:00

I have documents in MongoDB which look like this { _id: …, ranks:[ {pos:

  • 0

I have documents in MongoDB which look like this

{ 
    "_id": "...",
    "ranks":[
    {"pos": 1,"type":"some type"},
    {"pos": 56,"type":"other type"},
    ...
    ]
}

I want to find all items where ‘ranks.type’ matches “some type” and sort them by ‘pos’ in this embedded document.

In SQL I would do something like this (tables document and document_rank)

SELECT
    D.* 
FROM
    document D 
JOIN
    document_rank DR ON DR.document_id = D.id
WHERE
    DR.type="some type" 
ORDER BY 
    DR.pos

I’m just asking if I can do this by embedding ranks or I have to link them to document. My experiments show that I have to link.

  • 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-06-07T15:15:28+00:00Added an answer on June 7, 2026 at 3:15 pm

    You are getting back a single document, so it doesn’t make sense to ask about sorting – there is no concept of sorting embedded elements in an array within a single document.

    However, starting in 2.1 (development version) the aggregation framework lets you do what you want by letting you select documents, $unwind the array, and then sort the resultant documents (you can even change the general shape of the result if needed using $project).

    db.coll.aggregate([
          {$unwind:"$ranks"},
          {$match:{"ranks.type":"some type"}},
          {$sort:{"ranks.pos":1}}
    ])
    

    Note that these are not your full original documents, they are documents with only one “ranks” embedded element each, that’s why they can be sorted on the embedded field now.

    If this sort of operation is integral to your application, then the schema design you have may not be the best choice. I would suggest looking at the types of queries and access patterns your application will have with these objects, in case changing the schema would simplify things significantly.

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

Sidebar

Related Questions

i have a mongoDB collection named col that has documents that look like this
I have a document in MongoDB which holds some meta info like the date
I have 10000 documents in one MongoDB collection. I'd like to update all the
The limitation of one document in MongoDB is 4Mo. I have some documents really
In MongoDB, I have documents defined like: { 'foo': { 'a': [1,2,3,4,5], 'b': [1,5,9],
I have a document in mongodb that is structured similar to this: { _id:
I have several documents in a MongoDB Collection, with a field 'name' (which is
(Sorry if it's a trivial question.) I have documents that looks like this (Python
I have a number of MongoDB collections which take a number of JSON documents
I have a MongoDB collection with objects, the _id property of which is a

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.