[SERVER-34265] when field name is number,sort by multiple fields is incorrect Created: 03/Apr/18  Updated: 24/Apr/18  Resolved: 04/Apr/18

Status: Closed
Project: Core Server
Component/s: Querying
Affects Version/s: 3.4.4
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: deng yong Assignee: Kelsey Schubert
Resolution: Duplicate Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
duplicates MONGOSH-1037 Numeric field are sorted to the front... Closed
Operating System: ALL
Participants:

 Description   

when field name is number,sort the result like this:

db.test.find().sort({"505":-1,"504":1 });

,and the result is:

{
    "_id" : ObjectId("5ac30f8761d473226afba2f3"),
    "503" : "12asdt",
    "504" : 10,
    "505" : 0.12,
    "506" : 10
}
 
/* 2 */
{
    "_id" : ObjectId("5ac2de8ab25cfdcfa67ddeca"),
    "503" : "abdc",
    "504" : 12,
    "505" : 12.3,
    "506" : 12
}
 
/* 3 */
{
    "_id" : ObjectId("5ac2de9cb25cfdcfa67dded4"),
    "503" : "t2q3t",
    "504" : 12,
    "505" : 12.1,
    "506" : 12
}
 
/* 4 */
{
    "_id" : ObjectId("5ac2de5eb25cfdcfa67ddeaf"),
    "503" : "abcd",
    "504" : 13,
    "505" : 3.6,
    "506" : 13
}
 
/* 5 */
{
    "_id" : ObjectId("5ac2de7bb25cfdcfa67ddec3"),
    "503" : "ttwewqet",
    "504" : 13,
    "505" : 0.12,
    "506" : 13
}
 
/* 6 */
{
    "_id" : ObjectId("5ac2dea9b25cfdcfa67ddedb"),
    "503" : "ttwewqet",
    "504" : 14,
    "505" : 0.12,
    "506" : 14
}

result is not correspond to the sort param。But it's right when filed name is characters.



 Comments   
Comment by Kelsey Schubert [ 04/Apr/18 ]

Hi deng_yong365,

Thank you for reporting this issue. This issue is tracked in SERVER-11358. Numeric keys in JavaScript and most dictionary/object implementations have undefined ordering system, which can result in behavior like this. As a workaround, I would suggest using a driver to ensure the correct order is maintained rather than running these queries through the shell.

In addition, I would recommend against numeric field names in MongoDB since they have other potential side effects: a numeric field name can be conflated with an array index and result in backfilling the array with the right combination of update syntax and documents.

Please feel free to vote for SERVER-11358 and the related ticket, SERVER-28569, and watch these tickets for updates.

Kind regards,
Kelsey

Generated at Thu Feb 08 04:36:04 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.