Mongodb's built-in update function, db.collection.update() is very customizable, has options for updating multiple documents at once and doesn't require getting anything beforehand.
You can use it like so:
db.collection.update(
<query>,
<update>,
{
upsert: <boolean>,
multi: <boolean>,
writeConcern: <document>
}
)
So in this case:
db.collection.update(
{username:'u_name'}, //1
{$set:jsonData}, // 2
{
multi: true, // 3
}
)
(
- the query searches for documents that have
- $set is IMPORTANT! If you do not use $set, your entire document will be erased and updated to a document containing only your new values. (it will delete all the other fields)
- Update multiple documents at once.
Keep in mind this is a guideline and you'll have to modify the above code a bit.