我正在使用MongoDB和PHP,使用Mongo元素结构:
{
"_id": {
"$oid": "5b123457848393"
},
"userName": "Bob Dole",
"userID": "ab9283829a",
"lastLogin": "1527873728",
"photos": [
{
"fileName": "IMG_001.jpg"
},
{
"fileName": "IMG_002.jpg"
},
{
"fileName": "IMG_003.mp4"
},
{
"fileName": "IMG_004.jpg"
}
]
}
理想情况下,我想利用 userID 和 fileName 属性来更新 photos 数组中的条目,添加新的元数据不存在。例如,我希望我的更新条目看起来像这样;
{
"_id": {
"$oid": "5b123457848393"
},
"userName": "Bob Dole",
"userID": "ab9283829a",
"lastLogin": "1527873728",
"photos": [
{
"fileName": "IMG_001.jpg",
"cameraType": "Canon",
"cameraModel": "5D"
},
{
"fileName": "IMG_002.jpg"
},
{
"fileName": "IMG_003.mp4"
},
{
"fileName": "IMG_004.jpg"
}
]
}
尽管我付出了最大的努力,但我未能确定如何更新只是用户ID为ab9283829a且fileName为IMG_001.jpg的条目。我的尝试;
$updateResult = $collection->updateOne(
["eventID" => "ab9283829a"],
['$set' => ["photos" => ["photo" => "IMG_001.jpg", "cameraType" => "Canon", "cameraModel" => "5D"]]]
);