MongoDB finding an item in an array and how to index properly -
i have documents stored so:
{ ... "users" : [ { "id" : "123456", "username" : "john", "address" : "fake st", } ], ... } what best way of being able retrieve documents username "john". also, proper ways of indexing performance considering inside array. want index "users", or there better way? inside database 50+ million documents.
to find documents contain @ least 1 "john" in users array, use db.collection.find({"users.username":"john"});
to make faster, create index db.collection.createindex({"users.username":1});. indexes in mongodb can reach inside arrays , index individual array entries (this called multikey index).
Comments
Post a Comment