01demo-mongodb

var mongodb=require('mongodb');
var server=new mongodb.Server('127.0.0.1',27017,{});
var client=new mongodb.Db('mydatabase1',server,{w:1});//数据库名?存储位置?
client.open(function(err){
	if(err){throw err;}
	client.collection('test_insert',function(err,collection){//表名?
		if(err) throw err;
		console.log(888888);
		collection.insert(
			{
				"title":"i like you",
				"body":"it's true"
			},
			{safe:true},
			function(err,documents){//安全模式表明数据库操作在回调之前完成
				if(err) throw err;
				console.log('document id is'+ JSON.stringify(documents));
		});
		collection.find({"title":"i like you"}).toArray(
			function(err,results){
				if(err) throw err;
				console.log(results);
			}
		);
	});
});

  

原文地址:https://www.cnblogs.com/hhweb/p/6364593.html