我想添加新的数据我嵌套数组
我的文件是:
{
"username": "erkin",
"email": "[email protected]",
"password": "b",
"playlists": [
{
"_id": 58,
"name": "asdsa",
"date": "09-01-15",
"musics": [
{
"name": "INNA - Cola Song (feat. J Balvin)",
"duration": "3.00"
},
{
"name": "blabla",
"duration": "3.00"
}
]
}
]
}
字符串
我想在此播放列表部分添加音乐:
{
"username": "erkin",
"email": "[email protected]",
"password": "b",
"playlists": [
{
"_id": 58,
"name": "asdsa",
"date": "09-01-15",
"musics": [
{
"name": "INNA - Cola Song (feat. J Balvin)",
"duration": "3.00"
},
{
"name": "blabla",
"duration": "3.00"
},
{
"name": "new",
"duration": "3.00"
}
]
}
]
}
型
以下是我尝试的:
$users->update(
array(
'_id' => new MongoId (Session::get('id')),
'playlists._id' => $playlistId
),
array(
'$push' => array('playlists.musics' => array(
'name' => 'newrecord',
'duration' => '3.00'
))
)
);
型
4条答案
按热度按时间zf9nrax11#
可能类似于这样,其中ID是您的ObjectId。第一个{}是标识文档所必需的。只要您的集合中有另一个唯一标识符,就不需要使用ObjectId。
字符串
fslejnso2#
这对我来说很有效!
“播放列表.$[].musics”:
字符串
https://docs.mongodb.com/manual/reference/operator/update/positional-filtered/#position-nested-arrays-filtered
bvhaajcl3#
我建议你使用
arrayFilters
,因为它支持多个嵌套文档和更清晰。字符串
uelo1irk4#
2022年更新:
完整片段:
字符串
推:
型
推入嵌套:
型