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

ε祈祈猫儿з 提交于 2019-12-07 02:32:17

问题


I have a collection with records like this:

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

I would like to search by the the 'admin' array.

How can I find all documents included for example ObjectId("50ae3bdb50b3d6f014000279") in the sub-array.

Thank you.


回答1:


You can match against array fields like admins the same as you would a non-array field:

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


来源:https://stackoverflow.com/questions/13515728/mongo-how-to-find-by-objectid-that-is-stored-in-a-subarray

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!