嵌套数组中的 Mongodb $push

编程入门 行业动态 更新时间:2024-10-28 05:14:05
本文介绍了嵌套数组中的 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

本文发布于:2023-11-22 15:26:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1617969.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:嵌套   组中   push   Mongodb

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!