Using MongoDB with NodeJS

http://blog.modulus.io/mongodb-tutorial

Install modules:
npm install mongodb –save
npm install assert –save

var MongoClient = require('mongodb').MongoClient,
assert = require('assert');

// Connection URL
var url = 'mongodb://localhost:27017/conFusion';

// Use connect method to connect to the Server
MongoClient.connect(url, function (err, db) {

    assert.equal(err,null);
    console.log("Connected correctly to server");
	
	var collection = db.collection("dishes");
	collection.insertOne({name: "Uthapizza", description: "test"}, function(err,result){
		assert.equal(err,null);
		console.log("After Insert:");
		console.log(result.ops);
			collection.find({}).toArray(function(err,docs){
				assert.equal(err,null);
				console.log("Found:");
				console.log(docs);
				db.dropCollection("dishes", function(err, result){
				   assert.equal(err,null);
				   db.close();
				});
			});
	});
});

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s