O'Reilly logo

JavaScript JSON Cookbook by Ray Rischpater

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Updating a document in MongoDB with Node.js

Updating a document in a collection is easy; simply use the collection's update method and pass the data you want to update.

How to do it...

Here's a simple example:

var mongo = require('mongodb').MongoClient; var url = 'mongodb://localhost:27017/test'; var update = function(collection, callback) { collection.update({ call:'kf6gpe-7' }, { $set: { lat: 39.0, lng: -121.0, another: true } }, function(error, result) { console.log('Updated with error ' + error); console.log(result); callback(result); }); }; mongo.connect(url, function(error, db) { console.log("mongo.connect returned " + error); // Get the documents collection var collection = db.collection('documents'); update(collection, function(result) { ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required