首页 > 数据库技术 > 详细

mongodb 在Linux下和nodejs的增删改查

时间:2015-11-20 17:17:29      阅读:306      评论:0      收藏:0      [点我收藏+]

技术分享mondodb 使用:

[root@mongodb01 ~]# mongo

show dbs

use database

show collections

1、新增

在collection中新增一条数据:db.customers.insert({ "userId" : "testcustomer"})

1     Customer.create(customerEntity, function (error) {
2         if (error) {
3             callback(error);
4         } else {
5             callback(null);
6         }
7     });

 

2、删除

 在collection中删除一条数据:db.customers.remove({"userId" : "testcustomer"})

1     var conditions = {userId: userId};
2     Customer.remove(conditions, function (error) {
3         if (error) {
4             callback(error);
5         } else {
6             callback(null);
7         }
8     });

 

3、修改

在collection中修改某一条数据: db.customers.update({"userId" : "ordinary"},{‘$set‘:{"usermail" : "2012@qq.cn"}})

 1     var userId = customerEntity.userId;
 2     var conditions = {userId: userId};
 3     var update = {$set: customerEntity};
 4     var options = {upsert: false};
 5     Customer.update(conditions, update, options, function (error) {
 6         if (error) {
 7             callback(error);
 8         } else {
 9             callback(null);
10         }
11     });

 

 

4、查找:

查找collection的所有数据: db.customers.find()

 1     Customer.find(function (error, result) {
 2         if (error) {
 3             callback(error);
 4         } else {
 5             var customerNum = result.length;
 6             var data = [];
 7             for (var i = 0; i < customerNum; i++) {
 8                 data[i] = _packageResultAsCustomerModel(result[i]);
 9             }
10             callback(null, data);
11         }
12     });

 

查找collection的某个数据:db.customers.find({"userId" : "ordinary"})

 1     var condition = {userId: userId};
 2     Customer.find(condition, function (error, result) {
 3         if (error) {
 4             callback(error);
 5         } else {
 6             if (result.length === 0) {    
 7                 callback(null, null);
 8             } else {
 9                 var data = _packageResultAsCustomerModel(result[0]);
10                 callback(null, data);
11             }
12         }
13     });

 关于node.js的mongoose方法的具体用法参考文献:https://cnodejs.org/topic/548e54d157fd3ae46b233502

mongodb 在Linux下和nodejs的增删改查

原文:http://www.cnblogs.com/wenhai/p/4980955.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!