我有以下 MongoDB 对象:
{
"_id": ObjectId("508ec9f413f88da4a590e5ee"),
"beersAndStouts": {
"0": {
"description": "Guinness",
"price": "4.20"
},
"1": {
"description": "Heineken",
"price": "4.80"
},
"2": {
"description": "Carlsberg",
"price": "4.80"
}
},
"snacks": {
"0": {
"description": "King cheese and onion",
"price": "2.20"
},
"1": {
"description": "Tayto cheese and onion",
"price": "1.80"
}
},
"specialOffers": {
"0": {
"description": "Two for one vodka\/red bull",
"price": "8"
}
},
"uname": "Eamorr",
"wines": {
"0": {
"description": "Cabernet Sauvignon",
"price": "5.00"
},
"1": {
"description": "Chardonnay",
"price": "5.00"
}
}
}
我有以下 PHP 变量:
$category=$_POST['category']; //"beersAndStouts"
$description=$_POST['columnName']; //"Description"
$value=$_POST['value']; //"4.70"
$rowId=$_POST['rowId']; //2
我想把嘉士伯 (rowId=2) 的价格从 4.80 改成 4.70...
如何运行这样的查询?
这是我到目前为止所拥有的,但我现在真的被困住了......
$priceLists=$mongo->eamorr->priceLists;
$priceLists->update(array('uname'=>$uname),array('$set'=>array($category=>array($rowId=>xxx))));
更新:这是我使用的代码:
$mongo=new Mongo();
$priceLists=$mongo->eamorr->priceLists;
$priceList=$priceLists->findOne(array('uname'=>$uname));
$newCategory=$priceList[$category];
$newCategory[$rowId][$description]=$value;
$priceLists->update(array('uname'=>$uname),array('$set'=>array($category=>$newCategory)));
它不漂亮,但它有效,我不必改变我的结构。