freespace
freespace

Reputation: 16701

pymongo returning different data than MongoDB Shell

I am playing around with monogdb for an assignment, and I have ran into the following issue: the document returned by pymongo doesn't match what is returned by mongoc. A simple test.py:

from pymongo import MongoClient
from bson.objectid import ObjectId

client = MongoClient()
db = client['dev_database']
print db.trials.find_one(ObjectId('522f975dc91e273451569942'))
print list(db.trials.find({'_id':ObjectId('522f975dc91e273451569942')}))

Doing it both ways just in case. This returns:

{u'nurse_id': u'522f975dc91e273451569941', u'question_ids': [], u'name': u'Test Trial', u'clinician_id': u'522f975dc91e273451569940', u'arms': {u'Med1': u'', u'Placebo': u''}, u'participant_ids': [], u'keywords': [u'abc', u'123'], u'_id': ObjectId('522f975dc91e273451569942')}
[{u'nurse_id': u'522f975dc91e273451569941', u'question_ids': [], u'name': u'Test Trial', u'clinician_id': u'522f975dc91e273451569940', u'arms': {u'Med1': u'', u'Placebo': u''}, u'participant_ids': [], u'keywords': [u'abc', u'123'], u'_id': ObjectId('522f975dc91e273451569942')}]

And if I do the same thing inside MongoDB Shell:

> use dev_database
switched to db dev_database
> db.trials.find('522f975dc91e273451569942')
{ "_id" : "522f975dc91e273451569942", "nurse_id" : "522f975dc91e273451569941", "question_ids" : [ ], "name" : "Test Trial", "clinician_id" : "522f975dc91e273451569940", "arms" : { "Med1" : "", "Placebo" : "" }, "participant_ids" : [
        "52325b93c91e274e81f4bdda",
        "52325b93c91e274e81f4bddb",
        "52325b93c91e274e81f4bddc",
        "52325b93c91e274e81f4bddd",
        "52325b93c91e274e81f4bdde"
], "keywords" : [ "abc", "123" ] }
>

As you can see, test.py returns a document where participant_ids is an empty list, but MongoDB Shell says otherwise.

I have no idea why this is, and it seems like I must be make a simple, but fundamental mistake somewhere.

Upvotes: 2

Views: 190

Answers (2)

freespace
freespace

Reputation: 16701

The problem turned out to be that my code saved a duplicate of this document where _id was of type str. e.g.

> db.trials.find()
{ "_id" : ObjectId("522f975dc91e273451569942"), "nurse_id" : "522f975dc91e273451569941", "question_ids" : [ ], "name" : "Test Trial", "clinician_id" : "522f975dc91e273451569940", "arms" : { "Med1" : "", "Placebo" : "" }, "participant_ids" : [ ], "keywords" : [ "abc", "123" ] }
{ "_id" : "522f975dc91e273451569942", "nurse_id" : "522f975dc91e273451569941", "question_ids" : [ ], "name" : "Test Trial", "clinician_id" : "522f975dc91e273451569940", "arms" : { "Med1" : "", "Placebo" : "" }, "participant_ids" : [
        "52325b93c91e274e81f4bdda",
        "52325b93c91e274e81f4bddb",
        "52325b93c91e274e81f4bddc",
        "52325b93c91e274e81f4bddd",
        "52325b93c91e274e81f4bdde"
], "keywords" : [ "abc", "123" ] }

MongoDB Shell always returns the second result, while the test code returns the first. The difference being that my code explicitly casts to ObjectId, while MongoDB Shell seems happy to try and find a match using str first.

The fact that MongoDB Shell's result lacked the ObjectId(...) in the _id field should have warned me something was amiss.

Upvotes: 2

Alexander Perechnev
Alexander Perechnev

Reputation: 2837

What would be the output of the next code?

from pymongo import MongoClient
from bson.objectid import ObjectId

client = MongoClient()
db = client['dev_database']
print json.dumps(db.trials.find_one(ObjectId('522f975dc91e273451569942')))

Upvotes: 0

Related Questions