如何使用 php 从文档 i n mongodb 中删除或删除数组数据

How to delete or remove array data from document i n mongodb using php

这是我要从积分

中删除的JSON { 积分: 55, 奖金: 20 }
{
       _id: 3,
       name: "ahn",
       age: 22,
       type: 2,
       status: "A",
       favorites: { artist: "Cassatt", food: "cake" },
       finished: [ 6 ],
       badges: [ "blue", "red" ],
       points: [
          { points: 81, bonus: 8 },
          { points: 55, bonus: 20 },
          { points: 56, bonus: 25 }
       ]
}

我想看到这个结果

{
       _id: 3,
       name: "ahn",
       age: 22,
       type: 2,
       status: "A",
       favorites: { artist: "Cassatt", food: "cake" },
       finished: [ 6 ],
       badges: [ "blue", "red" ],
       points: [
          { points: 81, bonus: 8 },
          { points: 56, bonus: 25 }
       ]
}

您可以使用 MongoDB 的 $pull

    db.collection.update(
      { },
      { $pull: { points:  { points: 55, bonus: 20 } } },
    )

{multi: true}:在上面的查询中添加这个将删除所有匹配 { points: 55, bonus: 20 }

的条目
collection->update(array("_id"=>$document["_id"]),array('$pull'=>array("points"=>array("points"=>55,"bonus"=>20)));

在PHP中使用此代码从文档

中删除文档