[SERVER-16365] Compound index creation does not follow the order we specifiy Created: 01/Dec/14  Updated: 11/Jan/15  Resolved: 02/Dec/14

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

Type: Bug Priority: Major - P3
Reporter: Arun Devaraj Assignee: Unassigned
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: Windows
Steps To Reproduce:

1. Create a compound index-
db.collection1.ensureIndex(

{"ClassId":1,"ModifiedDate":1}

)

2. After index is created, run collection1.stats() to look at the index created.
You see "ModifiedDate_1_ClassId_1" instead of "ClassId_1_ModifiedDate_1"

Participants:

 Description   

When we create a compound index specifying a certain order of fields the order is reversed when the index is created.



 Comments   
Comment by J Rassi [ 02/Dec/14 ]

This is a known issue with the mongo shell: the shell re-orders elements within documents that contain integers as field names. See SERVER-11358, and the tickets linked therein.

I would advise that you modify your schema such that your documents do not contain integers as field names. Aside from this issue, you may also encounter unintuitive behavior when projecting subdocuments with these fields (see comment at SERVER-15013).

As an alternative, you can work around this issue by creating the index using a MongoDB driver instead of the mongo shell. See PyMongo's create_index() function, as an example.

Comment by Arun Devaraj [ 02/Dec/14 ]

Looks like I have an idea on what is happening. If the keys in a compound index have string and numeric values the order is reversed in the index.
You can repro it without any data in the collection. Here is the output you requested ( ClassId is represented by the key "Cid", ModifiedDate date by the key "3")

> use Rescore
switched to db Rescore
> db.Collection1.ensureIndex(

{"Cid":1,"3":1}

)
{
"createdCollectionAutomatically" : true,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
> db.Collection1.getIndexes()
[
{
"v" : 1,
"key" :

{ "_id" : 1 }

,
"name" : "id",
"ns" : "Rescore.Collection1"
},
{
"v" : 1,
"key" :

{ "3" : 1, "Cid" : 1 }

,
"name" : "3_1_Cid_1",
"ns" : "Rescore.Collection1"
}
]
> db.Collection1.stats()
{
"ns" : "Rescore.Collection1",
"count" : 0,
"size" : 0,
"numExtents" : 1,
"storageSize" : 8192,
"nindexes" : 2,
"lastExtentSize" : 8192,
"paddingFactor" : 1,
"paddingFactorNote" : "paddingFactor is unused and unmaintained in 2.8.
It remains hard coded to 1.0 for compatibility only.",
"userFlags" : 1,
"totalIndexSize" : 16352,
"indexSizes" :

{ "_id_" : 8176, "3_1_Cid_1" : 8176 }

,
"ok" : 1
}
>

Comment by J Rassi [ 01/Dec/14 ]

Please post the verbatim output of running the following at the mongo shell:

db.collection1.ensureIndex({ClassId:1, ModifiedDate:1})
db.collection1.getIndexes()
db.collection1.stats()

And, does the issue persist after restarting the shell?

Comment by Arun Devaraj [ 01/Dec/14 ]

I am using the MongoDb shell for index creation.

Comment by J Rassi [ 01/Dec/14 ]

Are you creating this index using the mongo shell, or are you using the ensureIndex() method from a MongoDB driver? If the latter, which driver are you using?

Generated at Thu Feb 08 03:40:49 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.