首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Mongodb $推入嵌套数组

Mongodb $推入嵌套数组
EN

Stack Overflow用户
提问于 2019-02-21 06:27:01
回答 1查看 0关注 0票数 0

我想在嵌套数组中添加新数据

我的文件是:

代码语言:javascript
复制
{
  "username": "erkin",
  "email": "erkin-07@hotmail.com",
  "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"
        }
      ]
    }
  ]
}

我想在此播放列表部分添加音乐:

代码语言:javascript
复制
{
  "username": "erkin",
  "email": "erkin-07@hotmail.com",
  "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"
        }
      ]
    }
  ]
}

这是我尝试过的:

代码语言:javascript
复制
$users->update(
  array(
    '_id' => new MongoId (Session::get('id')),
    'playlists._id' => $playlistId
  ),
  array(
    '$push' => array('playlists.musics' => array(
      'name' => 'newrecord',
      'duration' => '3.00'
    ))
  )
);
EN

回答 1

Stack Overflow用户

发布于 2019-02-21 15:33:48

可能是这样的,ID就是你的ObjectId。第一个{}是识别您的文档所必需的。只要您的集合中有另一个唯一标识符,就不需要使用ObjectId。

代码语言:javascript
复制
db.collection.update(
    { "_id": ID, "playlists._id": "58"},
    { "$push": 
        {"playlists.$.musics": 
            {
                "name": "test name",
                "duration": "4.00"
            }
        }
    }
)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100003125

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档