简体   繁体   中英

How to fetch data of multiple users in one query in mongodb?

i have a collection of user data.

UserCollection = [{
"Name":"User1",
"Age":24,
"Gender":"Male"},{
"Name":"User2",
"Age":25,
"Gender":"Male"},{
"Name":"User3",
"Age":26,
"Gender":"Female"}]

i need to find details of User1 and User2 with one query. currently i am doing this in python...

list = ["User1","User2"]
for user in list:
  data = db.UserCollection.find({"Name":user})
  for userData in data:
     list2.append(userData)

I m not sure why you wanna do this as it is not a very generic approach and you could hit a snag down the road but just to solve your current issue try the following:

 db.UserCollection.find( { name: { $in: [ "User1", "User2" ] } } )

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