2

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.

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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')))

4 Comments

Not surprisingly: TypeError: ObjectId('522f975dc91e273451569942') is not JSON serializable
Sorry, it was my mistake. Seems like .find() returns correct data, but "print list()" doesn't print arrays recursively. Try to read count of objects in participant_ids array. Miracles do not happen.
Just read one object from mongodb and make sure that his "participant_ids" field is empty. I believe it is not.
Miracles do not happen, but lists do print recursively. In any case, len(doc['participant_ids']) returns 0. Empty list is empty.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.