转自:https://www.cnblogs.com/sexintercourse/p/6485381.html

首先要确保mongodb的正确安装,安装参照:http://docs.mongodb.org/manual/tutorial/install-mongodb-on-debian-or-ubuntu-linux

另外可以参考我的另一篇博文

http://www.cnblogs.com/sexintercourse/p/5774310.html

指导mongo和nodejs的开发

然后下载nodejs的mongodb的driver

npm install mongodb

编写一个测试的程序:


 1 var mongodb = require('mongodb');
 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');


 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.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');


 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.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函数即可:

示例:


 1 var mongodb = require('mongodb');
 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函数

示例:


 1 var mongodb = require('mongodb');
 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 });

结果如图:

对数据进行更新:

示例:


 1 var mongodb = require('mongodb');
 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函数

示例:


 1 var mongodb = require('mongodb');
 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');


 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'};
 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       });  
 

69.nodejs对mongodb数据库的增删改查操作的更多相关文章

  1. nodejs对mongodb数据库的增删改查操作(转载)

    首先要确保mongodb的正确安装,安装参照:http://docs.mongodb.org/manual/tutorial/install-mongodb-on-debian-or-ubuntu-l ...

  2. 利用koa实现mongodb数据库的增删改查

    概述 使用koa免不了要操纵数据库,现阶段流行的数据库是mongoDB,所以我研究了一下koa里面mongoDB数据库的增删改查,记录下来,供以后开发时参考,相信对其他人也有用. 源代码请看:我的gi ...

  3. PHP程序中使用PDO对象实现对数据库的增删改查操作的示例代码

    PHP程序中使用PDO对象实现对数据库的增删改查操作(PHP+smarty) dbconn.php <?php //------------------------使用PDO方式连接数据库文件- ...

  4. python web.py操作mysql数据库,实现对数据库的增删改查操作

    使用web.py框架,实现对mysql数据库的增删改查操作: 该示例代码中连接的是本地数据库testdb,user表,表结构比较简单,只有两个字段:mobile和passwd,类型均为字符型 实际应用 ...

  5. TP5.1:数据库的增删改查操作(基于面向对象操作)

    我们现实中对数据库的增删改查操作,都是使用模型类进行操作的(表名::),也就是面向对象操作,只有底层的代码用的是数据库操作(Db::table('表名')) 下面我将贴出模型类进行的增删改查操作,通过 ...

  6. 通过jdbc连接MySql数据库的增删改查操作

    一.获取数据库连接 要对MySql数据库内的数据进行增删改查等操作,首先要获取数据库连接 JDBC:Java中连接数据库方式 具体操作如下: 获取数据库连接的步骤: 1.先定义好四个参数 String ...

  7. TP5.1:数据库的增删改查操作(基于数据库操作)

    1.在app/index/controller文件夹下创建一个文件,名为:Operation 注意:起名一定要避开关键字,例如:mysql,curd等等,如果使用关键字起名,会造成报错! 在Opera ...

  8. 使用nodejs连接mysql数据库实现增删改查

      首先要有数据库 使用xampp 或者 phpstudy 可以傻瓜式安装 新建一个项目文件夹 之后在这个目录下初始化package.json (npm init) 先在项目中安装mysql 和 ex ...

  9. greendao对SQLite数据库的增删改查操作

    利用greendao操作数据库时,都是以对象或者对象的list来进行增删改查的操作,操作的结果都是用一个list来接收的!!! 1.增加一条记录 Stu stu01=new Stu();stu01.s ...

随机推荐

  1. <Sicily>Greatest Common Divisors

    一.题目描述 A common divisor for two positive numbers is a number which both numbers are divisible by. It ...

  2. webpack(构建一个前端项目)详解--升级

    升级一个正式的项目结构 分离webpack.config.js文件: 新建一个webpack.config.base.js任何环境依赖的wbpack //public webpack const pa ...

  3. Mac安装composer安装Yii2项目

    [注释:]本人原创,如需转载请注明来源链接! 通过安装Composer方式安装Yii 如果还没有安装 Composer,你可以按 getcomposer.org 中的方法安装. 在 Linux 和 M ...

  4. gitHub上如何设置或者取消电子邮箱提醒

    原文链接:点我 我们正常注册的gitHub一般应该都是电子邮箱的方式,在注册账号时可能选择或者默认给了各种提醒,但是gitHub的邮箱提醒真的就比较烦人了,特别是最近团队开发项目,什么动态都有提醒,就 ...

  5. 移动端(手机端)页面自适应解决方案—rem布局篇

    移动端(手机端)页面自适应解决方案-rem布局 假设设计妹妹给我们的设计稿尺寸为750 * 1340.结合网易.淘宝移动端首页html元素上的动态font-size属性.设计稿尺寸.前端与设计之间协作 ...

  6. XML和Schema命名空间详解

    来源:https://blog.csdn.net/wanghuan203/article/details/9204337 XML和Schema具有无关平台,技术厂商,简单,规范统一等特点,极具开放性, ...

  7. Java实现断点续传。

    http://www.cnblogs.com/liaojie970/p/5013790.html

  8. 洛谷 P1747 好奇怪的游戏

    P1747 好奇怪的游戏 题目背景 <爱与愁的故事第三弹·shopping>娱乐章. 调调口味来道水题. 题目描述 爱与愁大神坐在公交车上无聊,于是玩起了手机.一款奇怪的游戏进入了爱与愁大 ...

  9. 洛谷 P3467 [POI2008]PLA-Postering

    P3467 [POI2008]PLA-Postering 题目描述 All the buildings in the east district of Byteburg were built in a ...

  10. Bitmap缓存机制

    Bitmap缓存机制 载入一个bitmap到UI里面比較简单直接.可是,假设我们一次载入大量的bitmap数据的时候就变得复杂了.很多情况下(比方这些组件:ListVIew,GridView或者Vie ...