我想从文档中删除信息但代码运行且没有错误发生。但它不会删除记录。 我喜欢这样的数据
{
"id": "12345",
"info": [
{
"sno":1
"name": "XYZ",
"email": "xyz@example.com"
},
{
"sno":2
"name": "XYZ",
"email": "xyz@example.com"
}
]
}
我要删除id = 12345和info.sno = 2的数据 我的PHP代码ID
<?
$m=new Mongo();
$db=$m->database;
$cond=array("id"=>'12345');
$data=array('$pull'=>array("info.sno"=>2));
//I used before this $data=array('$pull'=>array("info"=>array("sno"=>2)));
echo json_encode($data);
$db->info->update($cond,$data);
$st=$db->Command(array("getlasterror"=>1));
?>
我运行mongo db命令,如:
db.info.update({"id":12345},{'$pull':{"info":{"sno":2}}});
答案 0 :(得分:2)
您的注释掉的行是正确的:
test> db.foo.findOne()
{
"_id" : ObjectId("4f74737cc3a51043d26f4b90"),
"id" : "12345",
"info" : [
{
"sno" : 1,
"name" : "XYZ",
"email" : "xyz@example.com"
},
{
"sno" : 2,
"name" : "XYZ",
"email" : "xyz@example.com"
}
]
}
test> db.foo.update({"id":"12345"}, {"$pull":{info:{sno:2}}})
test> db.foo.findOne()
{
"_id" : ObjectId("4f74737cc3a51043d26f4b90"),
"id" : "12345",
"info" : [
{
"sno" : 1,
"name" : "XYZ",
"email" : "xyz@example.com"
}
]
}