Mulagala
Mulagala

Reputation: 8661

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

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

i am using

python 2.7.5
mongodb 2.4.6

Upvotes: 0

Views: 334

Answers (1)

ma08
ma08

Reputation: 3734

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. I followed this question.

Upvotes: 1

Related Questions