以前咱们的集合已经建立成功,咱们就先来进行第一步操做 —— 查询。javascript
查询分不少种类型,如条件查询,过滤查询等等,今天只学习了最基本的find查询。java
举例:数据库
1.find查询: obj.find(查询条件,callback);数组
Model.find({},function(error,docs){ //若没有向find传递参数,默认的是显示全部文档 }); Model.find({ "age": 28 }, function (error, docs) { if(error){ console.log("error :" + error); }else{ console.log(docs); //docs: age为28的全部文档 } });
Model提供了一个create方法来对数据进行保存。下面咱们来看一下示例:学习
1. Model.create(文档数据, callback))对象
Model.create({ name:"model_create", age:26}, function(error,doc){ if(error) { console.log(error); } else { console.log(doc); } });
刚刚学习了model的create方法,那接下来就开始学习基于entity的保存方法吧。以下示例:blog
1. Entity.save(文档数据, callback))ip
var Entity = new Model({name:"entity_save",age: 27}); Entity.save(function(error,doc) { if(error) { console.log(error); } else { console.log(doc); } });
学习了数据的保存,接下来咱们就开始学习对数据的更新吧!rem
1.示例:obj.update(查询条件,更新对象,callback);文档
var conditions = {name : 'test_update'}; var update = {$set : { age : 16 }}; TestModel.update(conditions, update, function(error){ if(error) { console.log(error); } else { console.log('Update success!'); } });
有了数据的保存、更新,就差删除了,下面咱们就来学习它吧!
1.示例:obj.remove(查询条件,callback);
var conditions = { name: 'tom' }; TestModel.remove(conditions, function(error){ if(error) { console.log(error); } else { console.log('Delete success!'); } });
本章咱们讲述了针对数据库的几个操做方法,经过调用相关方法来对数据进行改变,无论是新增、删除、修改仍是查查询,你均可以办到。
简单回顾:
1. 查询:find查询返回符合条件一个、多个或者空数组文档结果。
2. 保存:model调用create方法,entity调用的save方法。
3. 更新:obj.update(查询条件,更新对象,callback),根据条件更新相关数据。
4. 删除:obj.remove(查询条件,callback),根据条件删除相关数据。