如何使用yii2更新名称为 TEST1 的成员年龄。?
使用下面的代码来更新,但我在那里指定索引,我想要指出索引。
User :: updateAll(['$ set'=> ['Addresses.0.members.0.age'=> 100]],['IN','Addresses.members.name ',['TEST1']]);
{
"_id" : ObjectId("595209b65312f48195fb2e01"),
"username" : "Test name",
"Addresses" : [
{
"address_no" : 1,
"Address" : "Test house",
"City" : "test city",
"State" : "Test state",
"Mobile" : "9999999",
"members" : [
{
"name" : "TEST1",
"age" : 35
},
{
"name" : "TEST2",
"age" : 30
},
]
},
{
"address_no" : 2,
"Address" : "2B, Test place",
"City" : "Test city",
"State" : "Test State",
"Pincode" : "12345",
"Phone" : "1234568789",
"Mobile" : 9999999999
}
],
"Beneficiaries" : [
{
"beneficiary_id" : 1,
"Name" : "Test1",
"Age" : "28",
"Sex" : "F"
}
],
"auth_key" : "esd8d89ds89ds89ds89ds",
}
答案 0 :(得分:0)
有position operator $来做这种工作
{
"Addresses.members.name" : "TEST2",
},
{
$set: {
"Addresses.$.members.0.age" : 40
}
}
这里我指定了第一个索引,因为它最多支持一个级别深度。
将来可能会发布新功能来解决此问题:https://jira.mongodb.org/browse/SERVER-831
答案 1 :(得分:0)
Yii::$app->mongodb->getCollection('user')->update(['_id' => $id, 'members.name' => 'Test1'], ['$set' => [
'members.$.age' => 100,
]]);