Hướng dẫn push nested array mongodb - đẩy mảng lồng nhau mongodb

Tôi muốn thêm dữ liệu mới của tôi

Tài liệu của tôi là:

{
  "username": "erkin",
  "email": "",
  "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"
        }
      ]
    }
  ]
}

Tôi muốn thêm nhạc vào phần danh sách phát này:

{
  "username": "erkin",
  "email": "",
  "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"
        }
      ]
    }
  ]
}

Đây là những gì tôi đã thử:

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

Tài liệu về nhà → Hướng dẫn sử dụng MongoDBMongoDB Manual

{
  "username": "erkin",
  "email": "",
  "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"
        }
      ]
    }
  ]
}
5

Toán tử

{
  "username": "erkin",
  "email": "",
  "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"
        }
      ]
    }
  ]
}
5 nối một giá trị được chỉ định vào một mảng.
{
  "username": "erkin",
  "email": "",
  "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"
        }
      ]
    }
  ]
}
5
operator appends a specified value to an array.

Toán tử

{
  "username": "erkin",
  "email": "",
  "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"
        }
      ]
    }
  ]
}
5 có biểu mẫu:
{
  "username": "erkin",
  "email": "",
  "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"
        }
      ]
    }
  ]
}
5
operator has the form:

{ $push: { : , ... } }

Để chỉ định

{
  "username": "erkin",
  "email": "",
  "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"
        }
      ]
    }
  ]
}
8 trong tài liệu nhúng hoặc trong một mảng, hãy sử dụng ký hiệu DOT.

Bắt đầu từ MongoDB 5.0, cập nhật các nhà khai thác xử lý các trường tài liệu với tên dựa trên chuỗi theo thứ tự từ vựng. Các trường có tên số được xử lý theo thứ tự số. Xem Cập nhật hành vi của các nhà khai thác để biết chi tiết.

Nếu trường không có trong tài liệu để cập nhật,

{
  "username": "erkin",
  "email": "",
  "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"
        }
      ]
    }
  ]
}
5 sẽ thêm trường mảng có giá trị làm phần tử của nó.
{
  "username": "erkin",
  "email": "",
  "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"
        }
      ]
    }
  ]
}
5
adds the array field with the value as its element.

Nếu trường không phải là một mảng, hoạt động sẽ thất bại.not an array, the operation will fail.

Nếu giá trị là một mảng,

{
  "username": "erkin",
  "email": "",
  "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"
        }
      ]
    }
  ]
}
5 nối toàn bộ mảng làm một phần tử duy nhất. Để thêm từng phần tử của giá trị, hãy sử dụng công cụ sửa đổi
$users->update(
  array(
    '_id' => new MongoId (Session::get('id')),
    'playlists._id' => $playlistId
  ),
  array(
    '$push' => array('playlists.musics' => array(
      'name' => 'newrecord',
      'duration' => '3.00'
    ))
  )
);
1 với
{
  "username": "erkin",
  "email": "",
  "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"
        }
      ]
    }
  ]
}
5. Ví dụ, hãy xem thêm một giá trị vào các mảng trong nhiều tài liệu. Để biết danh sách các công cụ sửa đổi có sẵn cho
{
  "username": "erkin",
  "email": "",
  "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"
        }
      ]
    }
  ]
}
5, xem các sửa đổi.
{
  "username": "erkin",
  "email": "",
  "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"
        }
      ]
    }
  ]
}
5
appends the whole array as a single element. To add each element of the value separately, use the
$users->update(
  array(
    '_id' => new MongoId (Session::get('id')),
    'playlists._id' => $playlistId
  ),
  array(
    '$push' => array('playlists.musics' => array(
      'name' => 'newrecord',
      'duration' => '3.00'
    ))
  )
);
1 modifier with
{
  "username": "erkin",
  "email": "",
  "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"
        }
      ]
    }
  ]
}
5
. For an example, see Append a Value to Arrays in Multiple Documents. For a list of modifiers available for
{
  "username": "erkin",
  "email": "",
  "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"
        }
      ]
    }
  ]
}
5
, see Modifiers.

Bắt đầu từ MongoDB 5.0,

$users->update(
  array(
    '_id' => new MongoId (Session::get('id')),
    'playlists._id' => $playlistId
  ),
  array(
    '$push' => array('playlists.musics' => array(
      'name' => 'newrecord',
      'duration' => '3.00'
    ))
  )
);
4 không còn gây ra lỗi khi bạn sử dụng toán tử cập nhật như
{
  "username": "erkin",
  "email": "",
  "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"
        }
      ]
    }
  ]
}
5 với biểu thức toán hạng trống (
$users->update(
  array(
    '_id' => new MongoId (Session::get('id')),
    'playlists._id' => $playlistId
  ),
  array(
    '$push' => array('playlists.musics' => array(
      'name' => 'newrecord',
      'duration' => '3.00'
    ))
  )
);
6). Một bản cập nhật trống dẫn đến không có thay đổi và không có mục nhập oplog được tạo (có nghĩa là hoạt động là không có op).
{
  "username": "erkin",
  "email": "",
  "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"
        }
      ]
    }
  ]
}
5
with an empty operand expression (
$users->update(
  array(
    '_id' => new MongoId (Session::get('id')),
    'playlists._id' => $playlistId
  ),
  array(
    '$push' => array('playlists.musics' => array(
      'name' => 'newrecord',
      'duration' => '3.00'
    ))
  )
);
6 ). An empty update results in no changes and no oplog entry is created (meaning that the operation is a no-op).

Bạn có thể sử dụng toán tử

{
  "username": "erkin",
  "email": "",
  "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"
        }
      ]
    }
  ]
}
5 với các sửa đổi sau:
{
  "username": "erkin",
  "email": "",
  "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"
        }
      ]
    }
  ]
}
5
operator with the following modifiers:

bổ nghĩa

Sự mô tả

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

Nối nhiều giá trị vào trường mảng.

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

Giới hạn số lượng các yếu tố mảng. Yêu cầu sử dụng công cụ sửa đổi

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

{ $push: { : , ... } }

1

Đơn đặt hàng các yếu tố của mảng. Yêu cầu sử dụng công cụ sửa đổi

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

{ $push: { : , ... } }

3

Chỉ định vị trí trong mảng để chèn các yếu tố mới. Yêu cầu sử dụng công cụ sửa đổi

$users->update(
  array(
    '_id' => new MongoId (Session::get('id')),
    'playlists._id' => $playlistId
  ),
  array(
    '$push' => array('playlists.musics' => array(
      'name' => 'newrecord',
      'duration' => '3.00'
    ))
  )
);
1. Không có công cụ sửa đổi

{ $push: { : , ... } }

3,
{
  "username": "erkin",
  "email": "",
  "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"
        }
      ]
    }
  ]
}
5 nối các phần tử vào cuối mảng.
{
  "username": "erkin",
  "email": "",
  "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"
        }
      ]
    }
  ]
}
5
appends the elements to the end of the array.

Khi được sử dụng với bộ sửa đổi, toán tử

{
  "username": "erkin",
  "email": "",
  "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"
        }
      ]
    }
  ]
}
5 có biểu mẫu:
{
  "username": "erkin",
  "email": "",
  "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"
        }
      ]
    }
  ]
}
5
operator has the form:

{ $push: { : { : , ... }, ... } }

Việc xử lý hoạt động

{
  "username": "erkin",
  "email": "",
  "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"
        }
      ]
    }
  ]
}
5 với các công cụ sửa đổi xảy ra theo thứ tự sau, bất kể thứ tự xuất hiện bộ điều chỉnh:
{
  "username": "erkin",
  "email": "",
  "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"
        }
      ]
    }
  ]
}
5
operation with modifiers occur in the following order, regardless of the order in which the modifiers appear:

  1. Cập nhật mảng để thêm các phần tử ở đúng vị trí.

  2. Áp dụng sắp xếp, nếu được chỉ định.

  3. Cắt các mảng, nếu được chỉ định.

  4. Lưu trữ mảng.

Tạo bộ sưu tập

{ $push: { : , ... } }

9:

db.students.insertOne( { _id: 1, scores: [ 44, 78, 38, 80 ] } )

Ví dụ sau đây bổ sung

{ $push: { : { : , ... }, ... } }

0 vào mảng

{ $push: { : { : , ... }, ... } }

1:

db.students.updateOne(
{ _id: 1 },
{ $push: { scores: 89 } }
)

Ví dụ đầu ra:

{ _id: 1, scores: [ 44, 78, 38, 80, 89 ] }

Thêm các tài liệu sau vào bộ sưu tập

{ $push: { : , ... } }

9:

db.students.insertMany( [
{ _id: 2, scores: [ 45, 78, 38, 80, 89 ] } ,
{ _id: 3, scores: [ 46, 78, 38, 80, 89 ] } ,
{ _id: 4, scores: [ 47, 78, 38, 80, 89 ] }
] )

Hoạt động

{
  "username": "erkin",
  "email": "",
  "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"
        }
      ]
    }
  ]
}
5 sau đây bổ sung

{ $push: { : { : , ... }, ... } }

4 vào mảng

{ $push: { : { : , ... }, ... } }

1 trong mỗi tài liệu:
{
  "username": "erkin",
  "email": "",
  "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"
        }
      ]
    }
  ]
}
5
operation appends

{ $push: { : { : , ... }, ... } }

4 to the

{ $push: { : { : , ... }, ... } }

1 array in each document:

db.students.updateMany(
{ },
{ $push: { scores: 95 } }
)

Để xác nhận rằng mỗi mảng

{ $push: { : { : , ... }, ... } }

1 bao gồm

{ $push: { : { : , ... }, ... } }

4, hãy chạy thao tác sau:

Hoạt động trả về các kết quả sau:

{
  "username": "erkin",
  "email": "",
  "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"
        }
      ]
    }
  ]
}
0

Sử dụng

{
  "username": "erkin",
  "email": "",
  "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"
        }
      ]
    }
  ]
}
5 với công cụ sửa đổi
$users->update(
  array(
    '_id' => new MongoId (Session::get('id')),
    'playlists._id' => $playlistId
  ),
  array(
    '$push' => array('playlists.musics' => array(
      'name' => 'newrecord',
      'duration' => '3.00'
    ))
  )
);
1 để nối nhiều giá trị vào trường mảng.
{
  "username": "erkin",
  "email": "",
  "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"
        }
      ]
    }
  ]
}
5
with the
$users->update(
  array(
    '_id' => new MongoId (Session::get('id')),
    'playlists._id' => $playlistId
  ),
  array(
    '$push' => array('playlists.musics' => array(
      'name' => 'newrecord',
      'duration' => '3.00'
    ))
  )
);
1 modifier to append multiple values to the array field.

Ví dụ sau đây nối thêm từng phần tử của

db.students.insertOne( { _id: 1, scores: [ 44, 78, 38, 80 ] } )

0 vào mảng

{ $push: { : { : , ... }, ... } }

1 cho tài liệu trong đó trường

db.students.insertOne( { _id: 1, scores: [ 44, 78, 38, 80 ] } )

2 bằng

db.students.insertOne( { _id: 1, scores: [ 44, 78, 38, 80 ] } )

3:

{
  "username": "erkin",
  "email": "",
  "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"
        }
      ]
    }
  ]
}
1

Thêm tài liệu sau vào bộ sưu tập

{ $push: { : , ... } }

9:

{
  "username": "erkin",
  "email": "",
  "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"
        }
      ]
    }
  ]
}
2

Hoạt động

{
  "username": "erkin",
  "email": "",
  "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"
        }
      ]
    }
  ]
}
5 sau đây sử dụng:
{
  "username": "erkin",
  "email": "",
  "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"
        }
      ]
    }
  ]
}
5
operation uses:

  • Bộ sửa đổi

    $users->update(
      array(
        '_id' => new MongoId (Session::get('id')),
        'playlists._id' => $playlistId
      ),
      array(
        '$push' => array('playlists.musics' => array(
          'name' => 'newrecord',
          'duration' => '3.00'
        ))
      )
    );
    
    1 để thêm nhiều tài liệu vào mảng

    db.students.insertOne( { _id: 1, scores: [ 44, 78, 38, 80 ] } )

    7,

  • Công cụ sửa đổi

    { $push: { : , ... } }

    1 để sắp xếp tất cả các phần tử của mảng

    db.students.insertOne( { _id: 1, scores: [ 44, 78, 38, 80 ] } )

    7 đã sửa đổi theo trường
    db.students.updateOne(
    { _id: 1 },
    { $push: { scores: 89 } }
    )
    0 theo thứ tự giảm dần và

  • Bộ điều chỉnh

    $users->update(
      array(
        '_id' => new MongoId (Session::get('id')),
        'playlists._id' => $playlistId
      ),
      array(
        '$push' => array('playlists.musics' => array(
          'name' => 'newrecord',
          'duration' => '3.00'
        ))
      )
    );
    
    9 chỉ giữ ba phần tử được sắp xếp đầu tiên của mảng

    db.students.insertOne( { _id: 1, scores: [ 44, 78, 38, 80 ] } )

    7.first three sorted elements of the

    db.students.insertOne( { _id: 1, scores: [ 44, 78, 38, 80 ] } )

    7 array.

{
  "username": "erkin",
  "email": "",
  "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"
        }
      ]
    }
  ]
}
3

Sau khi hoạt động, chỉ có ba câu đố ghi điểm cao nhất trong mảng:

{
  "username": "erkin",
  "email": "",
  "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"
        }
      ]
    }
  ]
}
4

Mẹo

Làm cách nào để cập nhật một mảng trong MongoDB?

Bạn có thể sử dụng các phương thức updateOne () hoặc updatemany () để thêm, cập nhật hoặc xóa các phần tử mảng dựa trên các tiêu chí được chỉ định. Nên sử dụng phương thức Updatemany () để cập nhật nhiều mảng trong một bộ sưu tập.use the updateOne() or updateMany() methods to add, update, or remove array elements based on the specified criteria. It is recommended to use the updateMany() method to update multiple arrays in a collection.

Làm cách nào để cập nhật một mảng lồng nhau trong MongoDB?

Cập nhật các mảng lồng nhau kết hợp với toán tử vị trí $ [] $ [], kết hợp với toán tử $ [] tất cả các toán tử vị trí, có thể được sử dụng để cập nhật các mảng lồng nhau. Các bản cập nhật sau đây các giá trị lớn hơn hoặc bằng 8 trong các lớp lồng nhau.The $[] filtered positional operator, in conjunction with the $[] all positional operator, can be used to update nested arrays. The following updates the values that are greater than or equal to 8 in the nested grades.

$ Đặt trong MongoDB là gì?

$ Đặt tài liệu đầu ra có chứa tất cả các trường hiện có từ các tài liệu đầu vào và các trường mới được thêm vào. Giai đoạn $ Set là bí danh cho $ AddFields. Cả hai giai đoạn đều tương đương với giai đoạn dự án $, chỉ định rõ ràng tất cả các trường hiện có trong các tài liệu đầu vào và thêm các trường mới.outputs documents that contain all existing fields from the input documents and newly added fields. The $set stage is an alias for $addFields . Both stages are equivalent to a $project stage that explicitly specifies all existing fields in the input documents and adds the new fields.

$ Pull trong MongoDB là gì?

Toán tử Pull $ sẽ loại bỏ khỏi một mảng hiện có tất cả các phiên bản của giá trị hoặc giá trị khớp với một điều kiện được chỉ định. Toán tử Pull $ có biểu mẫu: {$ pull: {: ,,, ...}} để chỉ định một trong một tài liệu nhúng hoặc trong một mảng, sử dụng ký hiệu dấu chấm.removes from an existing array all instances of a value or values that match a specified condition. The $pull operator has the form: { $pull: { : , : , ... } } To specify a in an embedded document or in an array, use dot notation.