Hướng dẫn how do i push an array object in mongodb? - làm cách nào để tôi đẩy một đối tượng mảng trong mongodb?

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

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

2 Trình khai thác

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

2 có giá trị được chỉ định vào một mảng.
Hướng dẫn how do i push an array object in mongodb? - làm cách nào để tôi đẩy một đối tượng mảng trong mongodb?

The

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

2 operator appends a specified value to an array.

Toán tử

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

2 có biểu mẫu:

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

2
operator has the form:

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

Để chỉ định

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

5 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,

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

2 sẽ thêm trường mảng có giá trị làm phần tử của nó.

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

2
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,

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

2 nối toàn bộ mảng là 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

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

8 với

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

2. 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

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

2, xem các sửa đổi.

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

2
appends the whole array as a single element. To add each element of the value separately, use the

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

8 modifier with

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

2
. For an example, see Append a Value to Arrays in Multiple Documents. For a list of modifiers available for

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

2
, see Modifiers.

Bắt đầu từ MongoDB 5.0,

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

1 không còn gây ra lỗi khi bạn sử dụng toán tử cập nhật như

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

2 với biểu thức toán hạng trống (

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

3). 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).

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

2
with an empty operand expression (

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

3 ). 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ử

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

2 với các sửa đổi sau:

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

2
operator with the following modifiers:

bổ nghĩa

Sự mô tả

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

8

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

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

6

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

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

8.

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

8

Đơ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

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

8.

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

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

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

8. Không có công cụ sửa đổi
db.students.updateOne(
{ _id: 1 },
{ $push: { scores: 89 } }
)
0,

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

2 nối các phần tử đến cuối mảng.

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

2
appends the elements to the end of the array.

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

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

2 có biểu mẫu:

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

2
operator has the form:

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

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

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

2 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:

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

2
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

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

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

Ví dụ sau đây bổ sung

db.students.updateOne(
{ _id: 1 },
{ $push: { scores: 89 } }
)
7 vào mảng
db.students.updateOne(
{ _id: 1 },
{ $push: { scores: 89 } }
)
8:

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

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

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

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

2 sau đây bổ sung

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

1 vào mảng
db.students.updateOne(
{ _id: 1 },
{ $push: { scores: 89 } }
)
8 trong mỗi tài liệu:

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

2
operation appends

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

1 to the
db.students.updateOne(
{ _id: 1 },
{ $push: { scores: 89 } }
)
8 array in each document:

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

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

db.students.updateOne(
{ _id: 1 },
{ $push: { scores: 89 } }
)
8 bao gồm

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

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

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

[
{ _id: 1, scores: [ 44, 78, 38, 80, 89, 95 ] },
{ _id: 2, scores: [ 45, 78, 38, 80, 89, 95 ] },
{ _id: 3, scores: [ 46, 78, 38, 80, 89, 95 ] },
{ _id: 4, scores: [ 47, 78, 38, 80, 89, 95 ] }
]

Sử dụng

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

2 với công cụ sửa đổi

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

8 để nối nhiều giá trị vào trường mảng.

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

2
with the

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

8 modifier to append multiple values to the array field.

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

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

7 vào mảng
db.students.updateOne(
{ _id: 1 },
{ $push: { scores: 89 } }
)
8 cho tài liệu trong đó trường

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

9 bằng
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 ] }
] )
0:

________số 8

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

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

db.students.insertOne(
{
"_id" : 5,
"quizzes" : [
{ "wk": 1, "score" : 10 },
{ "wk": 2, "score" : 8 },
{ "wk": 3, "score" : 5 },
{ "wk": 4, "score" : 6 }
]
}
)

Hoạt động

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

2 sau đây sử dụng:

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

2
operation uses:

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

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

    8 để thêm nhiều tài liệu vào mảng
    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 ] }
    ] )
    4,

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

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

    8 để sắp xếp tất cả các phần tử của mảng
    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 ] }
    ] )
    4 đã sửa đổi theo trường
    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 ] }
    ] )
    7 theo thứ tự giảm dần và

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

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

    6 chỉ giữ ba phần tử được sắp xếp đầu tiên của mảng
    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 ] }
    ] )
    4.first three sorted elements of the
    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 ] }
    ] )
    4 array.

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

0

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

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

1

Mẹo