When setting up a compound index like the one below
db.data.ensureIndex({ userId: 1, myObject: 1 })
Will the index be used when running the following query?
db.data.find({
userId: 1,
myObject: {
a:'test',
b:'test2'
}
})
It is known that in Javascript, objects do not preserve their order. Is this the same case in MongoDB?
What happens when documents have objects with different property orders like the examples below?
{
_id: ObjectId,
userId:1,
myObject: {
b: 'test',
a: 'test2'
}
},
{
_id: ObjectId,
userId:1,
myObject: {
b: 'test',
a: 'test2'
}
},
{
_id: ObjectId,
userId:2,
myObject: {
b: 'test',
a: 'test2'
}
}
Does the order of the properties matter when indexing an object as shown above?
EDIT: According to the documentation at http://docs.mongodb.org/manual/core/index-single/, it states "When performing equality matches on subdocuments, field order matters and the subdocuments must match exactly."
Also, in the example provided, the following query would work
db.factories.find( { metro: { city: "New York", state: "NY" } } )
However, if the metro fields were reversed, it would not work
db.factories.find( { metro: { state: "NY", city: "New York" } } )
So how can you maintain the property order in Javascript/Node when the language itself does not support it?