7

Using Python 3.4 I'm parsing a document read from MongoDB (3.0.2) - I perform various tests and generate JSON/BSON of the form below:

{  
     'FixedH': False,
     'Mstereo': True,
     'RecMet': False,
     'Sstereo': True,
     'bond': False,
     'charge': False,
     'isotope': False,
     'length': 223,
     'nocomponents': 1,
     'nolayers': 6,
     'stereo': True
}

If I try and write this back to MongoDB (from the shell) I get the following error:

ReferenceError: False is not defined at (shell):1:175

If I manually convert my booleans (False --> false) so that they are all lower case the error disappears and the document is written to the collection in MongoDB.

I'm guessing I'm not the first to encounter this problem but I can't find any published workarounds. How can I get around this case sensitivity mis-match?

1
  • 2
    Its not a question of case sensitivity. Its a question of language. PyMongo is Python. Shell is JS. So to insert this document from shell you have to convert it to JS where python's 'False' is false Commented May 17, 2015 at 11:51

2 Answers 2

5

Are you inserting the documents from mongo shell? Mongo shell won't accept 'False' or 'True' as boolean values. You should use a Python Mongo driver client. Pymongo should work. Check the below sample:

import pymongo
client = pymongo.MongoClient('localhost', 27017)
db = client.testdatabase
col = db.testcollection
col.insert({'FixedH': False,'Mstereo': True,'RecMet': False,'Sstereo': True,'bond': False,
            'charge': False, 'isotope': False,'length': 223,'nocomponents': 1,
            'nolayers': 6,'stereo': True})
cursor = col.find()
print 'Found', cursor.count()
print cursor.next()
client.close()
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for that - Ironically I was having a different problem with pymongo - so decided to see if my documents would insert from the Mongo shell to see if it was a fault with my document. I guess not! Now, I just have to figure out if it is my python code that is wrong or a pymongo 2.8, mongo 3 mismatch. :-)
1

Call json_str = json.dumps(YOUR_OBJECT) and then insert it into MongoDB from shell

Comments

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.