May 03, 2013

Using same key twice in MongoDB

In MongoDB course one of my colleague has run a query with two occurrences of same key. Then I thought query will fail on validation. But the way that MongoDB behaves is different on this scenario.

> db.test.insert ({name:1})
> db.test.insert ({name:2})
> db.test.insert ({name:3})
> db.test.insert ({name:4})
> db.test.find({name:1,name:2})
{ "_id" : ObjectId("518342a1f193f1de8aef117e"), "name" : 2 }
> db.test.find({name:3,name:1})
{ "_id" : ObjectId("5183429ef193f1de8aef117d"), "name" : 1 }

Then I ran some insert statements.

> db.test.insert ({name:"Dedunu",name:"Dinesh"})
> db.test.find()
{ "_id" : ObjectId("51833fabeadc78fbf0f7232d"), "name" : "Dinesh" }

I think this behavior is inherited from JavaScript. 

No comments:

Post a Comment