Explore Courses Blog Tutorials Interview Questions
0 votes
in Web Technology by (50.2k points)

I have a partner collection and I am using pymongo to retrieve the data

When I query the collection with MongoDB, I see the following result

db.partner.find({'unique_key': 'c89dbe313932008febde61cdd2a071a1d'},{})

{ "_id" : ObjectId("4eb463cb158acb554e8c9c11"), "unique_key" : "c89dbe313932008febde61cdd2a071a1d", "name" : "ABC", "primary_key" : 12 }  

But when I query via pymongo, here is what I do

for document in collection.find(find, criteria):

    print document  

where find = {'unique_key': 'c89dbe313932008febde61cdd2a071a1d'} and

      criteria = {}

Here is what I see in result:

{u'_id': ObjectId('4eb463cb158acb554e8c9c11')}  

and I don't get name and primary_key in result, am I missing something?

Thank you

1 Answer

0 votes
by (108k points)
edited by

In pymongo find(), you have set the criteria as an empty dictionary so it means that it will only return the _id of the particular document, not the other fields of the specified collection. Whereas in mongo find(), you have specified the filter that means the _id of the document so it will return all the fields that the particular document is having. If you want all the fields of the document then set the second parameter of the find() that is the projection parameter as empty or you can also omit this parameter. But if you want to get a specific field of the document then don’t leave that parameter empty.

Are you interested in learning MongoDB? Check out this MongoDB Certification training course.

Also, have a look at this professional Full Stack Developer Training, designed to enhance your skills in the field.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Oct 18, 2019 in Web Technology by Sammy (47.6k points)

Browse Categories