With a compound index like the following
db.data.ensureIndex({ userId: 1, myObject: 1 })
Will the following find use this index?
db.data.find({
userId: 1,
myObject: {
a:'test',
b:'test2'
}
})
I know in Javascript that objects don't preserve their order so is this the same in Mongo?
So what will happen if I have documents with objects in different orders like this.
{
_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 like above?
EDIT: In the documentation, http://docs.mongodb.org/manual/core/index-single/ it says "When performing equality matches on subdocuments, field order matters and the subdocuments must match exactly."
And in their example, the following would work
db.factories.find( { metro: { city: "New York", state: "NY" } } )
but if the metro fields were the other way around it wont work
db.factories.find( { metro: { state: "NY", city: "New York" } } )
So how do you preserve the property order in Javascript/Node when the language itself doesn't support it?