mongoose CRUD

mongoose 的增删改查

  1. 添加 Model.create(文档数据, callback))
    Model.create({ name:"model_create", age:26}, function(error,doc){
        if(error) {
            console.log(error);
        } else {
            console.log(doc);
        }
    });

     

  2. 删除 obj.remove(查询条件,callback);
    var conditions = { name: 'tom' };
     
    TestModel.remove(conditions, function(error){
        if(error) {
            console.log(error);
        } else {
            console.log('Delete success!');
        }
    });

     

  3. 更新 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!');
        }
    });
  4. 查询 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的所有文档
      }
    }); 

     

posted @ 2015-08-17 21:52  c-Sharp  阅读(144)  评论(0)    收藏  举报