See also:
Records can be inserted to a collection with insert
collection.insert(docs[[, options], callback])
Where
For example
var document = {name:"David", title:"About MongoDB"};
collection.insert(document, {w: 1}, function(err, records){
console.log("Record added as "+records[0]._id);
});
If trying to insert a record with an existing _id value, then the operation yields in error.
collection.insert({_id:1}, {w:1}, function(err, doc){
// no error, inserted new document, with _id=1
collection.insert({_id:1}, {w:1}, function(err, doc){
// error occured since _id=1 already existed
});
});
Shorthand for insert/update is save - if _id value set, the record is updated if it exists or inserted if it does not; if the _id value is not set, then the record is inserted as a new one.
collection.save({_id:"abc", user:"David"},{w:1}, callback)
callback gets two parameters - an error object (if an error occured) and the record if it was inserted or 1 if the record was updated.
Updates can be done with update
collection.update(criteria, update[[, options], callback]);
Where
There are several option values that can be used with an update
If the replacement object is a document, the matching documents will be replaced (except the _id values if no _id is set).
collection.update({_id:"123"}, {author:"Jessica", title:"Mongo facts"});
The example above will replace the document contents of id=123 with the replacement object.
To update only selected fields, $set operator needs to be used. Following replacement object replaces author value but leaves everything else intact.
collection.update({_id:"123"}, {$set: {author:"Jessica"}});
See MongoDB documentation for all possible operators.
To update and retrieve the contents for one single record you can use findAndModify.
collection.findAndModify(criteria[, sort[, update[, options]]], callback)
Where
Options object can be used for the following options:
var MongoClient = require('mongodb').MongoClient
, format = require('util').format;
MongoClient.connect('mongodb://127.0.0.1:27017/test', function(err, db) {
if(err) throw err;
db.collection('test').findAndModify(
{hello: 'world'}, // query
[['_id','asc']], // sort order
{$set: {hi: 'there'}}, // replacement, replaces only the field "hi"
{}, // options
function(err, object) {
if (err){
console.warn(err.message); // returns error if no matching object found
}else{
console.dir(object);
}
});
});