简体   繁体   English

如何在pymongo游标对象[mongo db]上进行查询

[英]How to make a query on pymongo cursor object [mongo db]

I have a requirement to find list of member records in a organization, In my organization there are 100 records(documents) in my collection.I have retrieved those hundred records with the following query 我需要查找组织中的成员记录列表,在我的组织中,我的集合中有100条记录(文档)。我已通过以下查询检索到了这100条记录

result = db.mycollection.find({'organization':'organizationName'})

now i want to retrieve only a list of members in the organization,now i have a list of member names like this 现在我只想检索组织中的成员列表,现在我有一个这样的成员名称列表

list1 = ['username1','username2','username3',....'username10']

now i want to get the details of the members who are in the list with the result(pymonto cursor object) variable.I don't want to make another query on database.Is it possible to get in that way.Thanks in advance 现在我想使用result(pymonto cursor object)变量result(pymonto cursor object)列表中成员的详细信息。我不想在数据库上进行其他查询。是否有可能以这种方式获得。

i am using 我在用

python 2.7.5
mongodb 2.4.6

I hope this is acceptable: Combine both the queries 我希望这是可以接受的:合并两个查询

db.mycollection.find({'organization':'organizationName','name':{'$in':list1}})

This is much more effecient than Manually searching for objects using application logic: 这比使用应用程序逻辑手动搜索对象要有效得多:

result2=[];
for ob in result:
  if ob.get('name') in list1:
    result2.append(ob)

I hope this works, I am not sure about the pymongo syntax. 我希望这行得通,我不确定pymongo语法。 I followed this question . 我遵循了这个问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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