user94628
user94628

Reputation: 3731

Inserting new data into a record in MongoDB

I'm trying to insert data collected from a html form into an existing document. These will all be new fields and these are a python dict. However when I perform the insert or update it seems to override the original data.

I initially store the data as:

self.settings['db'].userInfo.insert(userData)

Then performed the update/insert as:

self.settings['db'].userInfo.update({'_id':DBid},locData)

My record now just holds data for the latter update.

How can I keep the original data in the document and just add in new data later on?

EDIT

I was able to add new values into the document by:

self.settings['db'].userInfo.update({'_id':DBid},{"$set":userData})

I used the same dict as before now and used the $set operator. This appended the document with the new fields and thier values.

Upvotes: 1

Views: 2000

Answers (1)

Brian Pantalone
Brian Pantalone

Reputation: 31

You should be doing another insert, not an update.

An update changes the original value... That's what it's designed to do, so another insert will add a new record to the collection.

To keep all the old fields of the document, and add some more use the Operator $set.

$set frm Mongo Example using $set

Upvotes: 3

Related Questions