>我有一个文档,其中包含这样的项目的子元素
"bar" : "547244fe10f0edd3128b4567",
"items" : [
{
"1" : {
"message" : "",
"display" : "true",
"type" : "text"
}
},
{
"2" : {
"id" : "234234",
"type" : "image",
"message" : "foo",
"display" : "true",
"created_at" : NumberLong(1416432114)
}
},
{
"3" : {
"message" : "",
"display" : "true",
"type" : "text"
}
},
我正在尝试更新其中一个子值
$foo['items']['1']['message'] = 'hello';
$story = InfoDB::where('_id', $id)->update($foo);
因此
"1" : {
"message" : "",
"display" : "true",
"type" : "text"
}
成为
"1" : {
"message" : "hello",
"display" : "true",
"type" : "text"
}
但是当我运行更新命令时,它会删除文档中的所有子项。
我是否必须更新整个文档?还是有其他功能?
我正在使用 https://github.com/jenssegers/laravel-mongodb
由于你们每个"项目"都是它自己的项目,因此有自己的数组索引,你必须对代码进行一些调整。所以只是尝试替换这段代码
$foo['items']['1']['message'] = 'hello';
有了这个
$foo['items'][0]['1']['message'] = 'hello';
这将调用第一项 (0),然后调用该项中的特定键 ("1")。