Say I have a simple circuit:
var testSchema = new mongoose.Schema({ map: { type: [ mongoose.Schema.Types.Mixed ], default: [] }, ...possibly something else });
Now make sure that the pairs ( _id , map._id ) are unique.
testSchema.index({ _id: 1, 'map._id': 1 }, { unique: true });
A quick check with db.test.getIndexes() shows that it was created.
{ "v" : 1, "unique" : true, "key" : { "_id" : 1, "map._id" : 1 }, "name" : "_id_1_map._id_1", "ns" : "test.test", "background" : true, "safe" : null }
The problem is that this index is ignored, and I can easily create multiple subdocuments with the same map._id . I can execute the following query several times:
db.maps.update({ _id: ObjectId("some valid id") }, { $push: { map: { '_id': 'asd' } } });
and in the end we get the following:
{ "_id": ObjectId("some valid id"), "map": [ { "_id": "asd" }, { "_id": "asd" }, { "_id": "asd" } ] }
What's going on here? Why can I click on conflicting subdocuments?
indexing mongodb mongoose
Sebastian nowak
source share