How to remove array element in mongodb?

后端 未结 6 1445
情深已故
情深已故 2020-11-22 07:10

Here is array structure

contact: {
    phone: [
        {
            number: \"+1786543589455\",
            place: \"New Jersey\",
            createdAt: \         


        
6条回答
  •  伪装坚强ぢ
    2020-11-22 07:44

    You can simply use $pull to remove a sub-document. The $pull operator removes from an existing array all instances of a value or values that match a specified condition.

    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.

    Read more about pull here.

提交回复
热议问题