banner ad

Nodejs and mongodb

| April 11, 2013 | 0 Comments
0 Flares 0 Flares ×

First your MongoDB correct installation, Installation Reference: http://docs.mongodb.org/manual/tutorial/install-mongodb-on-debian-or-ubuntu-linux

Then download NodeJS MongoDB driver

npm install mongodb

Write a test :

var mongodb = require('mongodb');
 var server = new mongodb.Server('localhost',27017,{auto_reconnect:true});
 var db = new mongodb.Db('mydb',server,{safe:true});
 db.open(function(err,db){
     if(!err)
     {   
         console.log('connect');
     }else{
         console.log(err);
     }   
 
 });
var mongodb = require('mongodb');
var server = new mongodb.Server('localhost',27017,{auto_reconnect:true});
 var db = new mongodb.Db('mydb',server,{safe:true});
 db.open(function(err,db){
     if(!err)
     {   
       console.log('connect');
       db.collection('mycoll',{safe:true},function(err,collection){
           if(err){
               console.log(err);
           }   
       }); 
 
     }else{
         console.log(err);
     }   
 });
var mongodb = require('mongodb');
 var server = new mongodb.Server('localhost',27017,{auto_reconnect:true});
 var db = new mongodb.Db('mydb',server,{safe:true});
 db.open(function(err,db){
     if(!err)
     {   
       console.log('connect');
       db.dropCollection('mycoll',{safe:true},function(err,result){
          console.log(result);
       }); 
 
     }else{
         console.log(err);
     }

Insert data for mongodb

var mongodb = require('mongodb');
 var server = new mongodb.Server('localhost',27017,{auto_reconnect:true});
 var db = new mongodb.Db('mydb',server,{safe:true});
 db.open(function(err,db){
     if(!err)
     {   
       db.collection('mycoll',{safe:true},function(err,collection){
           var tmp1 = {title:'hello',number:1};
           collection.insert(tmp1,{safe:true},function(err,result){
               console.log(result);
           }); 
     });
     }else{
         console.log(err);
     }   
 
 });

update mongodb

var mongodb = require('mongodb');
 var server = new mongodb.Server('localhost',27017,{auto_reconnect:true});
 var db = new mongodb.Db('mydb',server,{safe:true});
 db.open(function(err,db){
     if(!err)
     {
       db.collection('mycoll',{safe:true},function(err,collection){
           collection.update({title:'hello'},{$set:{number:3}},{safe:true},function(err,result){
               console.log(result);
           });
 
     }else{
         console.log(err);
     }
 
 });

remove

var mongodb = require('mongodb');
 var server = new mongodb.Server('localhost',27017,{auto_reconnect:true});
 var db = new mongodb.Db('mydb',server,{safe:true});
 db.open(function(err,db){
     if(!err)
     {
       db.collection('mycoll',{safe:true},function(err,collection){
           collection.remove({title:'hello'},{safe:true},function(err,result){
               console.log(result);
           });
         
     }else{
         console.log(err);
     }         
                                 
 });

find and findone

var server = new mongodb.Server('localhost',27017,{auto_reconnect:true});
 var db = new mongodb.Db('mydb',server,{safe:true});
 db.open(function(err,db){
     if(!err)
     {   
       db.collection('mycoll',{safe:true},function(err,collection){
           var tmp1 = {title:'hello'};
           var tmp2 = {title:'world'};
           collection.insert([tmp1,tmp2],{safe:true},function(err,result){
               console.log(result);
           }); 
           collection.find().toArray(function(err,docs){
               console.log('find');
               console.log(docs);
           }); 
           collection.findOne(function(err,doc){
               console.log('findOne');
               console.log(doc);
           }); 
       });
Download PDF
0 Flares Twitter 0 Facebook 0 Google+ 0 Pin It Share 0 LinkedIn 0 Reddit 0 StumbleUpon 0 0 Flares ×

Tags: ,

Category: Uncategorized

About the Author ()

My name is John Link.I am 26 years old. My major is Computer science and technology. I am a junior programmer with Python.

Leave a Reply

Your email address will not be published. Required fields are marked *

0 Flares Twitter 0 Facebook 0 Google+ 0 Pin It Share 0 LinkedIn 0 Reddit 0 StumbleUpon 0 0 Flares ×