Is there a key order issue in BSON MongoDB?

I know that some commands need to have the hashmap / dictionary streamlined, but is the actual BSON document in MongoDB really valid and will the index work?

eg.

db.people.ensureIndex({LName:1, FName:1});

Will it work with both:

{LName:"abc", FName:"def"}, 
{FName:"ghi", LName:"jkl"} 

?

thank

+5
source share
1 answer

The order of the document properties does not affect indexing.

You can verify this yourself by running this query:

db.people.find({LName: "abc"}).explain()

and then this query:

db.people.find({LName: "jkl"}).explain()

you should see that MongoDB will use the index in both cases (the property cursorshould be something like "BtreeCursor LName_1_FName_1").

+2
source

All Articles