mongoose. updating embedded document in array

前端 未结 3 680
鱼传尺愫
鱼传尺愫 2021-01-02 05:06

In the a official mongoose site I\'ve found how can I remove embedded document by _id in array:

post.comments.id(my_id).remove();
post.save(function (err) {
         


        
3条回答
  •  长情又很酷
    2021-01-02 05:34

    It shoud look something like this:

        YOURSCHEMA.update(
            { _id: "DocumentObjectid" , "ArrayName.id":"ArrayElementId" },
            { $set:{ "ArrayName.$.TheParameter":"newValue" } },
            { upsert: true }, 
            function(err){
    
            }
        );
    

    In this exemple I'm searching an element with an id parameter, but it could be the actual _id parameter of type objectId.

    Also see: MongooseJS Doc - Updating Set and Similar SO question

提交回复
热议问题