如何使用php从mongodb文件中删除或删除数组数据
问题描述:
Here are the JSON i want to delete { points: 55, bonus: 20 } from points
{
_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 }
]
}
I want to see this result
{
_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 }
]
}
以下是我要删除的JSON {points:55,bonus:20}来自 p> \ n
{
_id:3,
name:“ahn”,
age:22,
type:2,
status:“A”,
收藏夹:{artist :“Cassatt”,食物:“蛋糕”},
完成:[6],
徽章:[“blue”,“red”],
points:[
{points:81,bonus:8} ,
{points:55,bonus:20},
{points:56,bonus:25}
]
}
code> pre>
我想要 看到这个结果 p>
{
_id:3,
name:“ahn”,
age:22,
type:2,
status:“A “,
收藏:{艺术家:”卡萨特“,食物:”蛋糕“},
完成:[6],
徽章:[”蓝色“,”红色“],
点数:[
{ 积分:81,奖励:8},
{点数:56,奖励:25}
]
}
code> pre>
div>
答
You can use $pull of MongoDB
db.collection.update(
{ },
{ $pull: { points: { points: 55, bonus: 20 } } },
)
{multi: true}: adding this in above query will delete all entries matching { points: 55, bonus: 20 }
答
collection->update(array("_id"=>$document["_id"]),array('$pull'=>array("points"=>array("points"=>55,"bonus"=>20)));
In PHP using this code to remove documents from document