【问题标题】:How to delete or remove array data from document i n mongodb using php如何使用php从mongodb文档中删除或删除数组数据
【发布时间】:2018-12-07 02:31:01
【问题描述】:

这是我想从积分中删除 { points: 55, bonus: 20 } 的 JSON

{
       _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 }
       ]
}

【问题讨论】:

    标签: php arrays mongodb collections document


    【解决方案1】:
    collection->update(array("_id"=>$document["_id"]),array('$pull'=>array("points"=>array("points"=>55,"bonus"=>20)));
    

    在 PHP 中使用此代码从文档中删除文档

    【讨论】:

      【解决方案2】:

      你可以使用 MongoDB 的 $pull

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

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

      【讨论】:

        猜你喜欢
        • 2013-07-23
        • 2013-02-13
        • 1970-01-01
        • 1970-01-01
        • 2021-11-18
        • 2014-09-23
        • 1970-01-01
        • 2015-05-01
        • 1970-01-01
        相关资源
        最近更新 更多