How does mongoose add or remove documents to array attributes?

  mongodb, question

Like this example below, what should I do if I want to continue adding or deleting documents to the lists array? Thank you!

{
 "_id" : ObjectId("590a77315a7ae88824b296cf"),
 "user" : "yejia@qq.com",
 "password" : "5475442343",
 "lists" : [
 {
 "create_at" : "1234",
 "update_at" : "1234",
 "title" : "yejia",
 "list_id" : 123
 }
 ]
 }

Pull and push are the same as mongodnode.jsdriver.

Refer to the following usage:

Model.update({ }, {’$pull’:{ } } );

Model.update({ }, {’$push’:{ } } );

For reference

Love MongoDB! Have Fun!


The syntax of pull and push in Mongoose is as follows:

var schema = new mongoose.Schema({ user: 'string', password: 'string', lists: [ {create_at: 'string', update_at: 'string', title: 'string', list_id: 'number'}]});
 
 var User = mongoose.model('User', schema,'test');
 
 //delete
 User.update({"user" : "yejia@qq.com"},
 {'$pull':{ lists : { list_id : 123 }}}, function(err, data){
 if(err) {
 console.log(err);  }
 console.log(data);
 });
 
 //Add
 User.update({"user" : "yejia@qq.com"},
 {'$push':{ lists : { create_at: '111', update_at: '222', title: 'test', list_id: 8888}}}, function(err, data){
 if(err) {
 console.log(err);  }
 console.log(data);
 });
 
 //Print Results
 User.find({},function(err,result){
 console.log(result);
 });