[英]MongoDB find then update - PHP
I have a document that looks like 我有一个看起来像的文件
{
_id: ObjectId("someId"),
recipients: [
{
email: "emailaddress"
},
{
email: "email2"
}
]
}
I am trying to update the first recipient object, and add another property but to no avail. 我正在尝试更新第一个收件人对象,并添加另一个属性,但无济于事。
{
_id: ObjectId("someId"),
recipients: [
{
email: "emailaddress",
new_prop: "something"
}
]
}
Is there a way to do this with findOneAndUpdate? 有没有办法用findOneAndUpdate做到这一点? I am using PHP
我正在使用PHP
I did some research. 我做了一些研究。 I'm new to MongoDB so I'm relying on my fellow developers to validate this code.
我是MongoDB的新手,所以我依靠其他开发人员来验证此代码。 I'm thinking this will do the trick:
我认为这可以解决问题:
$mongoID = new MongoID('yourID');
$newdata = array('$set' => array("recipients.0.new_prop" => "Something"));
$collection->update(array("_id" => $mongoID), $newdata);
It might help to visit this page: https://docs.mongodb.com/manual/reference/operator/update/set/ 访问此页面可能会有所帮助: https : //docs.mongodb.com/manual/reference/operator/update/set/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.