简体   繁体   中英

How to check if document exist with particular field in mongodb

I have an array of users and I have one collection named users. Now I want to find the documents which has user_id equals to users from Array. And if that user id doesn't exist then no action taken. I'll loop through array to get one user id at a time. Now my concern is how to find the document with that particular user_id in MongoDB

Output I want:

Array a= [1,2,3,4,5]
document with user_id=1
document with user_id=2
document with user_id=3 like that

Thanks for help!!!

You can use $in operator. For more information https://docs.mongodb.com/manual/reference/operator/query/in/

For example

db.collection.find({user_id:{$in:[1,2,3,4,5]}})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM