本文介绍了MongoDB $推入嵌套数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在嵌套数组中添加新数据
I want add new data my nested array
我的文档是:
{ "username": "erkin", "email": "erkin-07@hotmail", "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" } ] } ] }我要在此播放列表部分添加音乐
I want add music in this playlist section:
{ "username": "erkin", "email": "erkin-07@hotmail", "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' )) ) );推荐答案
大概是这样的,其中ID是您的ObjectId.第一个{}是识别您的文档所必需的.只要您的集合中有另一个唯一标识符,就不需要使用ObjectId.
Probably something like this where ID is your ObjectId. The first {} are necessary to identify your document. It is not required to use an ObjectId as long as you have another unique identifier in your collection.
db.collection.update( { "_id": ID, "playlists._id": "58"}, { "$push": {"playlists.$.musics": { "name": "test name", "duration": "4.00" } } } )更多推荐
MongoDB $推入嵌套数组
发布评论