var MongoClient = require('mongodb').MongoClient;
var DB_CONN_STR = 'mongodb://localhost:27017/admin'; //# 数据库为 admin
var insertData = function(db, callback) {
//连接到表 site
var collection = db.collection('admin');
//插入数据
var data = [{
"name": "唐荣",
"url": "www.baidu.com",
"age":18,
"sex":"man"
}, {
"name": "菜鸟工具",
"url": "c.runoob.com"
}];
collection.insert(data, function(err, result) {
if(err) {
console.log('Error:' + err);
return;
}
callback(result);
});
//查询数据
var whereStr = {"name":'菜鸟教程'};
collection.find(whereStr).toArray(function(err, result) {
if(err){
console.log('Error:'+ err);
return;
}
callback(result);
});
//更新数据
var whereStr = {"name":'菜鸟教程'};
var updateStr = {$set: { "url" : "888888888888" }};
collection.update(whereStr,updateStr, function(err, result) {
if(err){
console.log('Error:'+ err);
return;
}
callback(result);
});
//删除数据
var whereStr = {"name":'菜鸟教程'};
collection.remove(whereStr, function(err, result) {
if(err){
console.log('Error:'+ err);
return;
}
callback(result);
});
}
MongoClient.connect(DB_CONN_STR, function(err, db) {
console.log("连接成功!");
insertData(db, function(result) {
console.log(result);
db.close();
});
});