[DOCS-1521] Sort collections by a sub-element array feature using index gives a correct order Created: 16/May/13  Updated: 30/Oct/23  Resolved: 29/Jul/13

Status: Closed
Project: Documentation
Component/s: manual
Affects Version/s: mongodb-2.4
Fix Version/s: Server_Docs_20231030

Type: Tracking Priority: Minor - P4
Reporter: braloum Assignee: Kay Kim (Inactive)
Resolution: Done Votes: 0
Labels: array, bug, feature, query, sort
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to DOCS-1111 Misleading/ambiguous statement in the... Closed
Participants:
Days since reply: 10 years, 39 weeks, 6 days ago

 Description   

I have this kind of collection :

 
"File" 
{ 
"_id" : { "$oid" : "4f730e3bb8be296910000180"}
, "Name" : "File1.jpg"
, "Folders" : [ 
                { "F_id" : { "$oid" : "4f72f503b8be296d78000166"} , "Ord" : 1} 
                , { "F_id" : { "$oid" : "4f730eedb8be296e78000180"} , "Ord" : 3}
            ]
}
{ 
"_id" : { "$oid" : "4f730e3ab8be296978000181"} 
, "Name" : "File2.jpg"
, "Folders" : [ 
                { "F_id" : { "$oid" : "4f72f503b8be296d78000166"} , "Ord" : 2} 
                , { "F_id" : { "$oid" : "4f730eedb8be296e78000180"} , "Ord" : 2}
                , { "F_id" : { "$oid" : "4f730eedb8be296e78000132"} , "Ord" : 1}
            ] 
}
{ 
"_id" : { "$oid" : "4f730e38b8be296e78000182"}
, "Name" : "File3.jpg"
, "Folders" : [ 
                { "F_id" : { "$oid" : "4f72f503b8be296d78000166"} , "Ord" : 3} 
                , { "F_id" : { "$oid" : "4f730eedb8be296e78000180"} , "Ord" : 1}
            ]
}

"Folders" can contains a lot of more elements, and reflects in which Folder "F_id" the file is available, and at what order it take place "Ord" in this folder.
Then I would like to simply get all the Files available in one Folder in the correct Order.

If I do something like :

 
File.find( {"Folders.F_id":{"$oid":4f72f503b8be296d78000166} } ).sort({"Folders.Ord":1}) ;

I get the incorrect Order !

I thought I should use map/reduce or aggregation framework to perform this...

But when I create an index:

 
File.ensureIndex("Folders.F_id" : 1 , "Folders.Ord" : 1)

With the same query :

 
File.find( {"Folders.F_id":{"$oid":4f72f503b8be296d78000166} } ).sort({"Folders.Ord":1}) ;

I get the correct Order I expected !

Is it a bug, a stroke of luck or a feature I can count on in the future ??

If it is a feature, this should be written on the doc ! (as there is no more needs to use map/reduce nor aggregation framework)

Regards


Generated at Thu Feb 08 07:41:14 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.