How to remove array element in mongodb?

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

Here is array structure

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


        
6条回答
  •  南方客
    南方客 (楼主)
    2020-11-22 07:32

    Try the following query:

    collection.update(
      { _id: id },
      { $pull: { 'contact.phone': { number: '+1786543589455' } } }
    );
    

    It will find document with the given _id and remove the phone +1786543589455 from its contact.phone array.

    You can use $unset to unset the value in the array (set it to null), but not to remove it completely.

提交回复
热议问题