nodejs对mongodb数据库的增删改查操作(转载)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了nodejs对mongodb数据库的增删改查操作(转载),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5452字,纯文字阅读大概需要8分钟。
内容图文
![nodejs对mongodb数据库的增删改查操作(转载)](/upload/InfoBanner/zyjiaocheng/476/07fb7ea076da4957a5cadaf67c5e256f.jpg)
2 var server = new mongodb.Server(‘localhost‘,27017,{auto_reconnect:true});
3 var db = new mongodb.Db(‘mydb‘,server,{safe:true});
4 db.open(function(err,db){
5 if(!err)
6 {
7 console.log(‘connect‘);
8 }else{
9 console.log(err);
10 }
11
12 });
如果最终显示connect则说明成功。
对mongodb的collection的操作
有两种方法链接collection,分别为:
db.collection(‘mycoll‘,function(err,coll){});
db.createCollection(‘mycoll‘,function(err,coll){});
这两种方法还有第二个可选参数{safe:true},这个参数的作用对于第一种方法,如果加上了这个参数,那么当collection不存在的时候则报错,对于第二种方法,则当collection存在的时候报错
示例:
1 var mongodb = require(‘mongodb‘);
3 var db = new mongodb.Db(‘mydb‘,server,{safe:true});
4 db.open(function(err,db){
5 if(!err)
6 {
7 console.log(‘connect‘);
8 db.collection(‘mycoll‘,{safe:true},function(err,collection){
9 if(err){
10 console.log(err);
11 }
12 });
13
14 }else{
15 console.log(err);
16 }
17
18 });
结果如图所示:
示例:
1 var mongodb = require(‘mongodb‘);
3 var db = new mongodb.Db(‘mydb‘,server,{safe:true});
4 db.open(function(err,db){
5 if(!err)
6 {
7 console.log(‘connect‘);
8 db.createCollection(‘mycoll‘,{safe:true},function(err,collection){
9 if(err){
10 console.log(err);
11 }
12 });
13
14 }else{
15 console.log(err);
16 }
17
18 });
结果如图所示:
删除collection则使用dropCollection函数即可:
示例:
2 var server = new mongodb.Server(‘localhost‘,27017,{auto_reconnect:true});
3 var db = new mongodb.Db(‘mydb‘,server,{safe:true});
4 db.open(function(err,db){
5 if(!err)
6 {
7 console.log(‘connect‘);
8 db.dropCollection(‘mycoll‘,{safe:true},function(err,result){
9 console.log(result);
10 });
11
12 }else{
13 console.log(err);
14 }
15
16 });
结果如图所示:
对collection进行增删改查
向collection添加数据使用insert函数
示例:
2 var server = new mongodb.Server(‘localhost‘,27017,{auto_reconnect:true});
3 var db = new mongodb.Db(‘mydb‘,server,{safe:true});
4 db.open(function(err,db){
5 if(!err)
6 {
7 db.collection(‘mycoll‘,{safe:true},function(err,collection){
8 var tmp1 = {title:‘hello‘,number:1};
9 collection.insert(tmp1,{safe:true},function(err,result){
10 console.log(result);
11 });
12 });
13 }else{
14 console.log(err);
15 }
16
17 });
结果如图:
对数据进行更新:
示例:
2 var server = new mongodb.Server(‘localhost‘,27017,{auto_reconnect:true});
3 var db = new mongodb.Db(‘mydb‘,server,{safe:true});
4 db.open(function(err,db){
5 if(!err)
6 {
7 db.collection(‘mycoll‘,{safe:true},function(err,collection){
8 collection.update({title:‘hello‘},{$set:{number:3}},{safe:true},function(err,result){
9 console.log(result);
10 });
11
12 }else{
13 console.log(err);
14 }
15
16 });
17
结果如图所示:
对数据进行删除使用remove函数
示例:
2 var server = new mongodb.Server(‘localhost‘,27017,{auto_reconnect:true});
3 var db = new mongodb.Db(‘mydb‘,server,{safe:true});
4 db.open(function(err,db){
5 if(!err)
6 {
7 db.collection(‘mycoll‘,{safe:true},function(err,collection){
8 collection.remove({title:‘hello‘},{safe:true},function(err,result){
9 console.log(result);
10 });
11
12 }else{
13 console.log(err);
14 }
15
16 });
结果如图:
如果remove没有任何的参数,则删除全部。
查找操作,查找操作有两个方法一个是find,一个是findOne
示例:
1 var mongodb = require(‘mongodb‘);
3 var db = new mongodb.Db(‘mydb‘,server,{safe:true});
4 db.open(function(err,db){
5 if(!err)
6 {
7 db.collection(‘mycoll‘,{safe:true},function(err,collection){
8 var tmp1 = {title:‘hello‘};
9 var tmp2 = {title:‘world‘};
10 collection.insert([tmp1,tmp2],{safe:true},function(err,result){
11 console.log(result);
12 });
13 collection.find().toArray(function(err,docs){
14 console.log(‘find‘);
15 console.log(docs);
16 });
17 collection.findOne(function(err,doc){
18 console.log(‘findOne‘);
19 console.log(doc);
20 });
21 });
nodejs对mongodb数据库的增删改查操作(转载)
标签:.net targe tool png target images connect org collect
本文系统来源:http://www.cnblogs.com/sexintercourse/p/6485381.html
内容总结
以上是互联网集市为您收集整理的nodejs对mongodb数据库的增删改查操作(转载)全部内容,希望文章能够帮你解决nodejs对mongodb数据库的增删改查操作(转载)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。