Remove sub-document from Mongo with mongoose

后端 未结 6 540
情歌与酒
情歌与酒 2020-12-16 01:12

I am trying to remove an item from a collection that is stored in a mongoose document. My document looks like this:

{
  \"__v\": 3,
  \"_id\": \"5221040475f         


        
6条回答
  •  一向
    一向 (楼主)
    2020-12-16 01:55

    You can simply use $pull to remove a sub-document.

        Collection.update({
        _id: parentDocumentId
      }, {
        $pull: {
          subDocument: {
            _id: SubDocumentId
          }
        }
      });
    

    This will find your parent document against given ID and then will remove the element from subDocument which matched the given criteria.

提交回复
热议问题