Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inserting new attribute to a document using MongoDB ( Python )

Tags:

python

mongodb

I'm new with MOngoDB ( coming from CouchDB ) and I am having issues with adding new attributes to my documents in MongDB using the MonDB python driver.

For example, I have the following document:

{ '_id':123456, 'text':"this is nice" } 

And I want to insert a new attribute, for example:

{ '_id':123456, 'text':"this is nice", 'created_time':datetime.datetime.now() } 

How do I go about adding the created_time attribute to my document?

Thanks!

like image 923
DjangoRocks Avatar asked Feb 20 '11 09:02

DjangoRocks


People also ask

How can I insert a new field in an existing MongoDB collection document?

To add field or fields to embedded documents (including documents in arrays) use the dot notation. See example. To add an element to an existing array field with $addFields , use with $concatArrays .

How manually insert data in MongoDB?

The insert() Method To insert data into MongoDB collection, you need to use MongoDB's insert() or save() method.


1 Answers

db.collection.update({'_id' : ObjectId(...)},                       {'$set' : {'create_time' : datetime(..) }}) 
like image 122
Andreas Jung Avatar answered Sep 22 '22 00:09

Andreas Jung