你的位置:首页 > Java教程

[Java教程]8.nodejs权威指南


1. MongoDB

var mongo = require('mongodb');

var host = '127.0.0.1';

var port = mongo.Connecton.DEFAULT_PORT;

var server = new mongo.Server(host,port,{auto_reconnect:true});

var db = new mongo.Db('node-mongo-examples',server,{safe:true});

db.open(function(err,db){

      db.collection('users',function(err,collection){

             collection.insert({name:'chuck'},function(err,docs){

                   

             });

             collection.find({name:'book'}).toArray(function(err,docs){

                   

             });

             collection.findOne({name:'book'},function(err,docs){

                   

             });

             collection.update({name:'chuck'},{$set:{name:'w'}});

             collection.remove({name:'chuck'});

      });

});

db.on('close',function(err,db){

      console.log('close');

});