千家信息网

Node与Mongodb实战

发表于:2026-11-01 作者:千家信息网编辑
千家信息网最后更新 2026年11月01日,在听陈鸿宇的《理想三旬》。好听~上两篇博文中提到了在Mac下Mongodb的安装与连接,这次我们来看看如何通过Node操作Mongodb。PS:本文中的数据集合采用上篇博文中'test'库中的"myc
千家信息网最后更新 2026年11月01日Node与Mongodb实战

在听陈鸿宇的《理想三旬》。好听~


上两篇博文中提到了在Mac下Mongodb的安装与连接,这次我们来看看如何通过Node操作Mongodb。


PS:本文中的数据集合采用上篇博文中'test'库中的"mycollection"。


一:安装MongoDB包

使用Node中的mongodb模块,需要先安装哦~打开终端,输入如下命令~

npm install mongodb

二:数据库连接与断开

1:引入mongodb模块

var mongo = require("mongodb");

2:创建MongoDB数据库的服务器对象

var server = new mongo.Server(host,port,[options]);

说明:host:服务器所在地址,默认本地localhost;port:服务器端口号,默认27017;options:可选配置参数。

3:创建MongoDB的db对象

var db = new mongo.Db(databasename,server,[options]);

说明:databasename:数据库名,这里我们使用上篇的"test"数据库;server:服务器对象;options:可选配置参数。

4:执行db的open方法,连接数据库

db.open(callback(err,db));

说明:callback回调方法,如果连接失败,将抛出err错误,连接数据库成功,会返回db对象。

5:执行db的close方法,断开数据库连接

db.close();

说明:关闭数据库连接时,将会触发监听的close事件,该事件有err和db两参数,意义同上。

function(err,db){

//回调方法

}

附:代码片段。将下述代码保存在testMongo.js文件中。

var mongo = require("mongodb");

var host = "localhost";

var port = "27017";


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


var db = new mongo.Db("test",server,{safe:true});


db.open(function(err,db){

if(err){

throw err;

console.log("连接数据库出错");

}else{

console.log("成功建立数据库连接");

db.close();

}

});


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

if(err){

throw err;

console.log("连接数据库出错");

}else{

console.log("关闭数据库连接")

}

})

新开终端,输入"mongod"打开mongodb。

新开终端,输入"node testMongo.js",看到如下结果,就成功啦。

testNode node testMongo.js

成功建立数据库连接

关闭数据库连接

三:数据集合

MongoDb操作的是数据集合!!!数据的操作就是数据集合的操作。

db.collection(collectionname,[options],callback(err,collection));

说明:collectionname:数据库中数据集合名字,此处是上节的"mycollection";options:可选配置参数。callback:连接的回调方法,会有连接出错的err参数和连接成功的collecction参数。

附:代码片段。保存下述文件到testMongo.js中。

var mongo = require("mongodb");

var host = "localhost";

var port = "27017";


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


var db = new mongo.Db("test",server,{safe:true});


db.open(function(err,db){

if(err){

throw err;

console.log("连接数据库出错");

}else{

console.log("成功建立数据库连接");

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

if(err){

throw err;

console.log("连接数据集合出错");

}else{

console.log("成功连接数据集合");

db.close();

}

});

}

});


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

if(err){

throw err;

console.log("连接数据库出错");

}else{

console.log("关闭数据库连接")

}

})

新开终端,输入"node testMongo.js",看到如下结果,就成功啦。

testNode node testMongo.js

成功建立数据库连接

成功连接数据集合

关闭数据库连接

四:MongoDb-增

数据集合的insert方法,实现添加数据的操作。

collection.insert(docs,[options],[callback(err,docs)])

说明:docs:要插入的数据;options:可选配置参数。可选callback:插入的回调方法,插入出错的err参数和插入成功时的docs(插入的数据)参数。

附:代码片段。保存下述文件到testMongo.js中。--在test库的mycollection数据集合中插入5条Cailala

var mongo = require("mongodb");

var host = "localhost";

var port = "27017";


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


var db = new mongo.Db("test",server,{safe:true});


db.open(function(err,db){

if(err){

throw err;

console.log("连接数据库出错");

}else{

console.log("成功建立数据库连接");

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

if(err){

throw err;

console.log("连接数据集合出错");

}else{

console.log("成功连接数据集合");

//insert

for(var i = 1;i<6;i++){

collection.insert({'name':'Cailala'+i},function(err,docs){

console.log(docs);

db.close();

});

}

}

});

}

});


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

if(err){

throw err;

console.log("连接数据库出错");

}else{

console.log("关闭数据库连接")

}

})

新开终端,输入"node testMongo.js",看到如下结果,就成功啦。

testNode node testMongo.js

成功建立数据库连接

成功连接数据集合

{ result: { ok: 1, n: 1 },

ops: [ { name: 'Cailala1', _id: 57a1fb5292764dbc5736dcd9 } ],

insertedCount: 1,

insertedIds: [ 57a1fb5292764dbc5736dcd9 ] }

关闭数据库连接

{ result: { ok: 1, n: 1 },

ops: [ { name: 'Cailala2', _id: 57a1fb5292764dbc5736dcda } ],

insertedCount: 1,

insertedIds: [ 57a1fb5292764dbc5736dcda ] }

{ result: { ok: 1, n: 1 },

ops: [ { name: 'Cailala3', _id: 57a1fb5292764dbc5736dcdb } ],

insertedCount: 1,

insertedIds: [ 57a1fb5292764dbc5736dcdb ] }

{ result: { ok: 1, n: 1 },

ops: [ { name: 'Cailala4', _id: 57a1fb5292764dbc5736dcdc } ],

insertedCount: 1,

insertedIds: [ 57a1fb5292764dbc5736dcdc ] }

{ result: { ok: 1, n: 1 },

ops: [ { name: 'Cailala5', _id: 57a1fb5292764dbc5736dcdd } ],

insertedCount: 1,

insertedIds: [ 57a1fb5292764dbc5736dcdd ] }

五:MongoDb-查

数据集合的find方法,实现查询数据的操作。

collection.find(selector,[options]).toArray(callback(err,docs))

说明:selector:查询条件;options:可选配置参数。find方法返回的是Cursor游标对象,该对象的toArray方法将返回查询到的所有数据文档,参数callback:查询的回调方法,查询出错的err参数和查询成功时的docs(查询出的数据)参数。

附:代码片段。保存下述文件到testMongo.js中。--查询数据集合中name是Cailala1的数据

var mongo = require("mongodb");

var host = "localhost";

var port = "27017";


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


var db = new mongo.Db("test",server,{safe:true});


db.open(function(err,db){

if(err){

throw err;

console.log("连接数据库出错");

}else{

console.log("成功建立数据库连接");

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

if(err){

throw err;

console.log("连接数据集合出错");

}else{

console.log("成功连接数据集合");

//find

collection.find({name:"Cailala1"},{fields:{name:1,_id:0}}).toArray(function(err,docs){

if(err) throw err;

else

console.log(docs);

db.close();

});

}

});

}

});


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

if(err){

throw err;

console.log("连接数据库出错");

}else{

console.log("关闭数据库连接")

}

})

新开终端,输入"node testMongo.js",看到如下结果,就成功啦。

testNode node testMongo.js

成功建立数据库连接

成功连接数据集合

[ { name: 'Cailala1' } ]

关闭数据库连接

六:MongoDb-改

数据集合的update方法,实现修改数据的操作。

collection.update(selector,documents,[optios],[callback(err,resu)])

说明:selector:需要更新的数据文档;documents:用于更新的文档;options:可选配置参数;可选callback:修改的回调方法,修改出错的err参数和修改成功时的result(成功修改的数据条数)参数。

附:代码片段。保存下述文件到testMongo.js中。--修改name是Cailala1为liujinhuan,并查询输出。

var mongo = require("mongodb");

var host = "localhost";

var port = "27017";


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


var db = new mongo.Db("test",server,{safe:true});


db.open(function(err,db){

if(err){

throw err;

console.log("连接数据库出错");

}else{

console.log("成功建立数据库连接");

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

if(err){

throw err;

console.log("连接数据集合出错");

}else{

console.log("成功连接数据集合");

// update

collection.update({name:"Cailala1"},{name:"liujinhuan"},function(err,res){

if(err){

throw err;

}else{

console.log("成功更新 "+JSON.parse(res).n+" 条数据");

collection.find({},{fields:{name:1,_id:0}}).toArray(function(err,docs){

if(err)

throw err;

else

console.log(docs);

db.close();

});

}

});

}

});

}

});


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

if(err){

throw err;

console.log("连接数据库出错");

}else{

console.log("关闭数据库连接")

}

})

新开终端,输入"node testMongo.js",看到如下结果,就成功啦。

testNode node testMongo.js

成功建立数据库连接

成功连接数据集合

成功更新 1 条数据

[ { name: 'liujinhuan' },

{ name: 'Cailala2' },

{ name: 'Cailala3' },

{ name: 'Cailala4' },

{ name: 'Cailala5' } ]

关闭数据库连接

七:MongoDb-删

数据集合的remove方法,实现删除数据的操作。

collection.remove([selector],[options],[callback])

说明:可选selector:删除的条件,不指定则删除全部;可选options:配置参数;可选callback:删除的回调方法,删除出错的err参数和删除成功时的result(成功修改的数据条数)参数。

附:代码片段。保存下述文件到testMongo.js中。

var mongo = require("mongodb");

var host = "localhost";

var port = "27017";


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


var db = new mongo.Db("test",server,{safe:true});


db.open(function(err,db){

if(err){

throw err;

console.log("连接数据库出错");

}else{

console.log("成功建立数据库连接");

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

if(err){

throw err;

console.log("连接数据集合出错");

}else{

console.log("成功连接数据集合");

//remove

collection.remove({name:"liujinhuan"},function(err,res){

if(err){

throw err;

}else{

console.log("成功删除 "+JSON.parse(res).n+" 条数据");

collection.find({},{fields:{name:1,_id:0}}).toArray(function(err,docs){

if(err)

throw err;

else

console.log(docs);

db.close();

});

}

});

}

});

}

});


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

if(err){

throw err;

console.log("连接数据库出错");

}else{

console.log("关闭数据库连接")

}

})

新开终端,输入"node testMongo.js",看到如下结果,就成功啦。

testNode node testMongo.js

成功建立数据库连接

成功连接数据集合

成功删除 1 条数据

[ { name: 'Cailala2' },

{ name: 'Cailala3' },

{ name: 'Cailala4' },

{ name: 'Cailala5' } ]

关闭数据库连接



以上就是总结的Node操作的Mongodb。其中的options都是一些可以配置的参数~这里只是基础的用法啦~欢迎吐槽


Mac不能上传结果的截图~又不能上传实例代码~好尴尬呀~


听不懂《理想三旬》。但还是在听~直到听吐为止吧~

数据 数据库 成功 参数 方法 查询 代码 终端 输入 配置 结果 新开 对象 文件 片段 服务器 更新 服务 文档 上篇 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 互联网科技有限公司 互联网黑科技 互联网科技有限公司经营范围 科技信息互联网 科技类互联网 麻工互联网科技 互联网 科技创新 互联网科技有限公司 翻译 甘肃拓世互联网科技有限公司 无锡溥仪互联网科技有限公司 互联网科技研发水平 银川新华互联网科技学校联系电话 产业互联网科技服务公司 广东鹅鹅鹅互联网科技有限公司 互联网科技接地气 普陀区互联网教育科技管理办法 同城互联网络科技 互联网科技大佬云集 广东猫王互联网科技有限公司电话 宁波允成互联网科技有限公司 科技赋能下的互联网金融 科技伦理 互联网公司 银川新华互联网科技职业技术培训学校图片 深圳育鹏互联网科技有限公司招聘 华夏恒生互联网科技013171 科技赋能视角下互联网保险发展 长沙渺峰互联网科技有限公司 广州三三互联网科技有限公司 杭州融税互联网科技有限公司 互联网金融金融科技
0