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
原文:http://www.cnblogs.com/wenhai/p/4980955.html