转自: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. BZOJ 1379 模拟退火

    模拟退火的第一题~ //By SiriusRen #include <cmath> #include <cstdio> #include <algorithm> u ...

  2. POJ 1274 二分图匹配

    匈牙利算法 裸题 //By SiriusRen #include <cstdio> #include <cstring> #include <algorithm> ...

  3. 08:Challenge 1

    总时间限制:  10000ms 单个测试点时间限制:  1000ms 内存限制:  262144kB 描述 给一个长为N的数列,有M次操作,每次操作是以下两种之一: (1)修改数列中的一个数 (2)求 ...

  4. C#中Request.Cookies 和 Response.Cookies 的区别分析

    .NET中提供了读写Cookie的多种方法,Request.Cookies 是客户端通过 Cookie 标头形式由客户端传输到服务器的 Cookie:Response.Cookies 在服务器上创建并 ...

  5. codeforces 400 C Inna and Huge Candy Matrix【模拟】

    题意:给出一个矩形的三种操作,顺时针旋转,逆时针旋转,对称,给出原始坐标,再给出操作数,问最后得到的坐标 画一下模拟一下操作就可以找到规律了 #include<iostream> #inc ...

  6. Unity经验之谈

    1.全屏与非全屏之间的切换 if (Input.GetMouseButtonDown(1)) { Screen.fullScreen = !Screen.fullScreen; } 2.Camera适 ...

  7. Python安装selenium启动浏览器

    1:在Python运行火狐或谷歌的浏览器是需要下载相对应的驱动 例如:你想在Python中使用代码命令打开firefox的网页 如果没有安装驱动,直接运行的话会出下面的错误 所以我们要安装相对应的浏览 ...

  8. COGS——T2084. Asm.Def的基本算法

    http://cogs.pro/cogs/problem/problem.php?pid=2084 ★☆   输入文件:asm_algo.in   输出文件:asm_algo.out   简单对比时间 ...

  9. easyui combobox keyhandler使用

    easyui combobox keyhandler使用 在combo组件中有属性:   keyHandler : { up : function() { console.log('upupup'); ...

  10. easyui combobox 取值

    easyui combobox 取值 var zhudaoci = $.trim($('#spanZhudaociId').combobox('getValue')); 学习了:http://blog ...