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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:12:20+00:00 2026-05-29T10:12:20+00:00

I’ve got a associative container (with the semantics of a c++ map) serialized in

  • 0

I’ve got a associative container (with the semantics of a c++ map) serialized in a mongo db array. In my collection I retrieve arrays which contain my key as so.

db.qrs.find( { "u.i" : 111892 })

I would like to sort on the “value” element of my “map”. Such as :

db.qrs.find( { "u.i" : 731612 }).sort( { "u.c" : -1} )

However this just sorts the array within the document, rather then sorting per row in the result set. An example sorted row:

 { "_id" : ObjectId("4f2f9acea2b3adfce39ae395"), "i" : 11, "r" : 1, 
"s" : ")n(kQ!$A=@%RPv-SB0dQXh7ME*%lw=!$@(VshFU1H6#b7r*wb()t3Ps!^xQ1rNr7j", "u" : 
[   {   "c" : 7,    "i" : 29518,    "p" : 0 },  { "c" : 1,  "i" : 
924577,     "p" : 3 },  {   "c" : 1,    "i" : 731612,   "p" : 1 } ] }

I had some hope that the position operator might work, alas it does not do what I expected it to , my query was formulated as:

db.qrs.find( { "u.i" : 111892 }).sort( { "u.$.c" : -1} )

So, how does my desired query look like ?

Can I somehow transpose the result set to hoist the key-value pair matching my predicate to become part of the top-level document structure of the result set ?

  • 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-29T10:12:21+00:00Added an answer on May 29, 2026 at 10:12 am

    MongoDB never sorts the array itself. You’re seeing the array as it exists in the document.

    Your specific use case is not supported by MongoDB at this time. You cannot match a specific element in the query and then sort on that element alone.

    You can only fix this by avoiding the use of an array, for example by using the value for “i” as the key of the embedded element :

    { "_id" : ObjectId("4f2f9acea2b3adfce39ae395"), "i" : 11, "r" : 1, 
    "s" : ")n(kQ!$A=@%RPv-SB0dQXh7ME*%lw=!$@(VshFU1H6#b7r*wb()t3Ps!^xQ1rNr7j", "u" : 
    {29518:   {   "c" : 7, "p" : 0 },  924577: { "c" : 1,"p" : 3 },  731612: {   "c" : 1,   "p" : 1 } }}
    

    You can then sort through

    db.qrs.find({"u.29518":{$exists:true}}).sort({"u.29518.c"-1})
    

    Obviously this solution may not be available to you but I don’t see any other routes that do not involve putting your array elements in a dedicated collection (which is a better schema decision in my opinion).

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
i got an object with contents of html markup in it, for example: string
I would like to run a str_replace or preg_replace which looks for certain words
I have just tried to save a simple *.rtf file with some websites and

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.