本文介绍了嵌套数组中的 Mongodb $push的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在我的嵌套数组中添加新数据
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 $push
发布评论