Speed up development with full-stack environments for every branch.

Learn More

Insert a record in MongoDB using MongoDB Native [Node.js]

5419 Runs 29758 Views 11726 Copies


sundip 66

published 4 years ago

    * This example uses the node MongoDB module to connect to the local
    * mongodb database on this virtual machine
    * More here: http://mongodb.github.io/node-mongodb-native/markdown-docs/insert.html
    //require node modules (see package.json)
    var MongoClient = require('mongodb').MongoClient
        , format = require('util').format;
    //connect away
    MongoClient.connect('mongodb://', function(err, db) {
      if (err) throw err;
      console.log("Connected to Database");
      //simple json record
    	var document = {name:"David", title:"About MongoDB"};
    	//insert record
    	db.collection('test').insert(document, function(err, records) {
    		if (err) throw err;
    		console.log("Record added as "+records[0]._id);
    Please login/signup to get access to the terminal.

    Your session has timed out.

    Dismiss (the page may not function properly).