嵌套数组中的MongoDB$Push

嵌套数组中的MongoDB$Push

我想要添加新的数据,我的嵌套数组

我的文件是:

{
  "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"
        }
      ]
    }
  ]}

我想在这个播放列表中添加音乐:

{
  "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"
        }
      ]
    }
  ]}

以下是我尝试过的:

$users->update(
  array(
    '_id' => new MongoId (Session::get('id')),
    'playlists._id' => $playlistId  ),
  array(
    '$push' => array('playlists.musics' => array(
      'name' => 'newrecord',
      'duration' => '3.00'
    ))
  ));


隔江千里
浏览 961回答 2
2回答

慕桂英546537

可能是这样的,ID是你的对象。第一个{}是标识文档所必需的。只要集合中有另一个唯一标识符,就不需要使用objectid。db.collection.update(     { "_id": ID, "playlists._id": "58"},     { "$push":          {"playlists.$.musics":              {                 "name": "test name",                 "duration": "4.00"             }         }     })
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

MongoDB