Mongodb在PHP中替换2个数组的值
我有一个Mongo系列,看起来像这样Mongodb在PHP中替换2个数组的值,php,mongodb,Php,Mongodb,我有一个Mongo系列,看起来像这样 { "_id": ObjectId("0000000000000123"), "user_id": NumberLong(000010), "location": { "addresses": [{ "number": NumberLong(4410), "street": "Test Drive", "county": "New York County",
{
"_id": ObjectId("0000000000000123"),
"user_id": NumberLong(000010),
"location": {
"addresses": [{
"number": NumberLong(4410),
"street": "Test Drive",
"county": "New York County",
"city": "New York",
"state": "NY",
"zip": "00001",
"links": [{
"image": "http://www.google.com/test",
"datetime": " 11/24/1952"
}, {
"image": "http://www.google.com/test2",
"datetime": " 11/24/1990"
}
]
}
]
}
}
我需要能够搜索链接数组的文件与www.google.com,并能够改变它到www.yahoo.com
我尝试了几个版本的mongo更新,但没有成功
$search_string = new MongoRegex("/www.google.com/i");
$collection->update(
array('location.addresses.links.image' => $search_string),
array('$set' => array('location.addresses.$.links.$.image' => '')),
array("multiple" => true)
);
因此,在进一步挖掘之后,我发现updateposition操作符($)只能用于一层深度 所以我决定通过引用使用foreach语句来完成我所需要的工作
$collection = new MongoCollection($this->db, 'users');
$search_string = new MongoRegex("/www.google.com/i");
$cursor = $collection->find(array('location.addresses.links.image' => $search_string));
foreach ($cursor as $doc)
{
foreach ($doc['location']['addresses'] as &$address)
{
foreach($address['links'] as &$link)
{
$link['image'] = str_replace('www.google.com', 'www.yahoo.com', $offender['image']);
}
}
$collection->update(array("_id" => $doc['_id']), $doc);
}