Mongo: how to find the ObjectId that is stored in a subarray?

I have a collection with the following entries:

{ "_id" : ObjectId("50ae3bdb50b3d6f01400027a"), "admins": [ObjectId("50ae3bdb50b3d6f014000279"), ObjectId("50ae3bdb50b3d6f01400027e")] } 

I would like to search the admin array.

How can I find all documents included, for example, ObjectId ("50ae3bdb50b3d6f014000279") in the subrange.

Thanks.

+7
source share
1 answer

You can map fields of an array of type admins in the same way as a field without an array:

 db.coll.find({admins: ObjectId("50ae3bdb50b3d6f014000279")}) 
+12
source

All Articles