PyMongo inserting BSON document to MongoDB -


i insert new document mongodb collection.

my first input string shown here:

{     "date" : isodate("2013-10-06t18:11:26.329z"),     "engines" : {},     "expiration_date" : isodate("2013-10-06t18:11:36.329z"),     "file_name" : "elad.elad",     "scan_status" : "test",     "task_id" : "4ce4ae9e-ef0a-476a-8189-92a5bfe328bd" } 

i'm creating bson.bson object string:

b=bson.bson(doc) 

i'm trying insert collection in mongodb:

collection.insert(b) 

but following error: typeerror: 'str' object not support item assignment

does know problem here?

you can directly convert string bson, either require json.loads or bson.encode method.

you can use following code :

import datetime pymongo import mongoclient  db = mongoclient().test collection = db.some  doc = {     "date" : datetime.datetime.strptime("2013-10-06t18:11:26.329z", "%y-%m-%dt%h:%m:%s.%fz"),     "engines" : {},     "expiration_date" : datetime.datetime.strptime("2013-10-06t18:11:36.329z","%y-%m-%dt%h:%m:%s.%fz"),     "file_name" : "elad.elad",     "scan_status" : "test",     "task_id" : "4ce4ae9e-ef0a-476a-8189-92a5bfe328bd" } collection.insert(doc) 

here using datetime object convert string date python compatible datetime


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -