Node.js连接mongodb实例代码
前面介绍了node.js操作mysql以及redis的内容,这里继续学习操作mongodb的内容。
安装驱动
安装命令
cnpm install mongodb
安装成功
数据库操作
因为mongodb是文档类型的数据库,与mysql有很大不同,mongodb会自动创建数据库和集合,并不需要提前进行手动创建。
Insert
var MongoClient = require("mongodb").MongoClient; var DB_CONN_STR = "mongodb://sa:sa@localhost:27017/test_db"; var insertData = function (db, callback) { //连接数据表my_firsrt_collection var collection = db.collection("my_firsrt_collection"); var data = [{ "name": "wolfy1", "age": 20 }, { "name": "wolfy2", "age": 20 }] collection.insert(data, function (err, result) { if (err) { console.log("error:" + err); return; }; callback(result); }); }; MongoClient.connect(DB_CONN_STR, function (err, db) { if (err) { console.log("连接失败:" + err); return; }; console.log("连接成功"); insertData(db,function(result){ console.log(result); db.close(); }); });
结果
查询数据
var MongoClient = require("mongodb").MongoClient; var DB_CONN_STR = "mongodb://sa:sa@localhost:27017/test_db"; //查询 var queryData = function (db, callback) { //连接数据表my_firsrt_collection var collection = db.collection("my_firsrt_collection"); //查询条件 var where = { "name": "wolfy1" }; collection.find(where).toArray(function (err, result) { if (err) { console.log("error:" + err); return; }; callback(result); }); }; MongoClient.connect(DB_CONN_STR, function (err, db) { if (err) { console.log("连接失败:" + err); return; }; console.log("连接成功"); queryData(db, function (result) { console.log(result); db.close(); }); });
结果
更新数据
var MongoClient = require("mongodb").MongoClient; var DB_CONN_STR = "mongodb://sa:sa@localhost:27017/test_db"; //查询 var updateData = function (db, callback) { //连接数据表my_firsrt_collection var collection = db.collection("my_firsrt_collection"); //查询条件 var where = { "name": "wolfy1" }; var updateStr={$set:{"address":"http://www.cnblogs.com/wolf-sun/"}}; collection.update(where,updateStr,function (err, result) { if (err) { console.log("error:" + err); return; }; callback(result); }); }; MongoClient.connect(DB_CONN_STR, function (err, db) { if (err) { console.log("连接失败:" + err); return; }; console.log("连接成功"); updateData(db, function (result) { console.log(result); db.close(); }); });
结果
删除数据
var MongoClient = require("mongodb").MongoClient; var DB_CONN_STR = "mongodb://sa:sa@localhost:27017/test_db"; //删除 var deleteData = function (db, callback) { //连接数据表my_firsrt_collection var collection = db.collection("my_firsrt_collection"); //查询条件 var where = { "name": "wolfy1" }; collection.remove(where, function (err, result) { if (err) { console.log("error:" + err); return; }; callback(result); }); }; MongoClient.connect(DB_CONN_STR, function (err, db) { if (err) { console.log("连接失败:" + err); return; }; console.log("连接成功"); deleteData(db, function (result) { console.log(result); db.close(); }); });
结果
相关推荐
lbyd0 2020-11-17
zhushenghan 2020-11-09
sunnnyduan 2020-10-16
sdmzhu 2020-09-01
mkhhxxttxs 2020-09-16
xiaohai 2020-09-16
apexlj 2020-08-16
BigYellow 2020-11-16
sushuanglei 2020-11-12
我心似明月 2020-11-09
不要皱眉 2020-10-14
xiaohai 2020-09-29
songxiugongwang 2020-09-22
萌亖 2020-09-17
LuckyLXG 2020-09-08
newcome 2020-09-09
jaylong 2020-08-19
大秦铁骑 2020-08-19