Tài liệu về nhà → Hướng dẫn sử dụng MongoDB → MongoDB Manual
db.students.insertOne[ { "_id" : 1, "scores" : [ 100 ] } ]
0 Trình sửa đổi db.students.insertOne[ { "_id" : 1, "scores" : [ 100 ] } ]
0 Chỉ định vị trí trong mảng mà toán tử db.students.insertOne[ { "_id" : 1, "scores" : [ 100 ] } ]
2 chèn các phần tử. Không có công cụ sửa đổi db.students.insertOne[ { "_id" : 1, "scores" : [ 100 ] } ]
0, toán tử db.students.insertOne[ { "_id" : 1, "scores" : [ 100 ] } ]
2 chèn các phần tử vào cuối mảng. Xem $ đẩy sửa đổi để biết thêm thông tin.The db.students.insertOne[ { "_id" : 1, "scores" : [ 100 ] } ]
0 modifier specifies the location in the array at which the
db.students.insertOne[ { "_id" : 1, "scores" : [ 100 ] } ]
2 operator inserts elements. Without the
db.students.insertOne[ { "_id" : 1, "scores" : [ 100 ] } ]
0 modifier, the db.students.insertOne[ { "_id" : 1, "scores" : [ 100 ] } ]
2 operator inserts elements to the end of the array. See $push modifiers for more
information.Để sử dụng công cụ sửa đổi
db.students.insertOne[ { "_id" : 1, "scores" : [ 100 ] } ]
0, nó phải xuất hiện với công cụ sửa đổi db.students.insertOne[ { "_id" : 1, "scores" : [ 100 ] } ]
6.db.students.insertOne[ { "_id" : 1, "scores" : [ 100 ] } ]
0 modifier, it must appear with the db.students.insertOne[ { "_id" : 1, "scores" : [ 100 ] } ]
6 modifier.{ $push: { : { $each: [ , , ... ], $position: } } }
db.students.insertOne[ { "_id" : 1, "scores" : [ 100 ] } ]
7 chỉ ra vị trí trong mảng, dựa trên chỉ mục dựa trên không:Một số không âm tương ứng với vị trí trong mảng, bắt đầu từ đầu mảng. Nếu giá trị của
7 lớn hơn hoặc bằng chiều dài của mảng, công cụ sửa đổi ____10 không có hiệu lực vàdb.students.insertOne[ { "_id" : 1, "scores" : [ 100 ] } ]
2 sẽ thêm các phần tử vào cuối mảng.db.students.insertOne[ { "_id" : 1, "scores" : [ 100 ] } ]
0 modifier has no effect anddb.students.insertOne[ { "_id" : 1, "scores" : [ 100 ] } ]
2 adds elements to the end of the array.db.students.insertOne[ { "_id" : 1, "scores" : [ 100 ] } ]
Một số âm tương ứng với vị trí trong mảng, đếm từ [nhưng không bao gồm] phần tử cuối cùng của mảng. Ví dụ:
1 chỉ ra vị trí ngay trước phần tử cuối cùng trong mảng. Nếu bạn chỉ định nhiều phần tử trong mảngdb.students.updateOne[ { _id: 1 }, { $push: { scores: { $each: [ 50, 60, 70 ], $position: 0 } } } ]
6, phần tử được thêm vào cuối cùng nằm ở vị trí được chỉ định từ cuối. Nếu giá trị tuyệt đối củadb.students.insertOne[ { "_id" : 1, "scores" : [ 100 ] } ]
7 lớn hơn hoặc bằng chiều dài của mảng,db.students.insertOne[ { "_id" : 1, "scores" : [ 100 ] } ]
2 sẽ thêm các phần tử vào đầu mảng.db.students.insertOne[ { "_id" : 1, "scores" : [ 100 ] } ]
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.
Tạo bộ sưu tập
db.students.updateOne[ { _id: 1 }, { $push: { scores: { $each: [ 50, 60, 70 ], $position: 0 } } } ]
5:db.students.insertOne[ { "_id" : 1, "scores" : [ 100 ] } ]
Hoạt động sau đây cập nhật trường
db.students.updateOne[ { _id: 1 }, { $push: { scores: { $each: [ 50, 60, 70 ], $position: 0 } } } ]
6 để thêm các phần tử db.students.updateOne[ { _id: 1 }, { $push: { scores: { $each: [ 50, 60, 70 ], $position: 0 } } } ]
7, db.students.updateOne[ { _id: 1 }, { $push: { scores: { $each: [ 50, 60, 70 ], $position: 0 } } } ]
8 và db.students.updateOne[ { _id: 1 }, { $push: { scores: { $each: [ 50, 60, 70 ], $position: 0 } } } ]
9 vào đầu mảng:db.students.updateOne[ { _id: 1 }, { $push: { scores: { $each: [ 50, 60, 70 ], $position: 0 } } } ]
Hoạt động kết quả trong tài liệu cập nhật sau:
{ "_id" : 1, "scores" : [ 50, 60, 70, 100 ] }
Thêm một tài liệu vào bộ sưu tập
db.students.updateOne[ { _id: 1 }, { $push: { scores: { $each: [ 50, 60, 70 ], $position: 0 } } } ]
5:db.students.insertOne[ { "_id" : 2, "scores" : [ 50, 60, 70, 100 ] } ]
Hoạt động sau đây cập nhật trường
db.students.updateOne[ { _id: 1 }, { $push: { scores: { $each: [ 50, 60, 70 ], $position: 0 } } } ]
6 để thêm các phần tử { "_id" : 1, "scores" : [ 50, 60, 70, 100 ] }
2 và { "_id" : 1, "scores" : [ 50, 60, 70, 100 ] }
3 tại chỉ số mảng của { "_id" : 1, "scores" : [ 50, 60, 70, 100 ] }
4:db.students.updateOne[ { _id: 2 }, { $push: { scores: { $each: [ 20, 30 ], $position: 2 } } } ]
Hoạt động kết quả trong tài liệu cập nhật sau:
{ "_id" : 2, "scores" : [ 50, 60, 20, 30, 70, 100 ] }
{ "_id" : 1, "scores" : [ 50, 60, 70, 100 ] }
can
accept a negative array index value to indicate the position starting from the end, counting from [but not including] the last element of the array. For example,
db.students.updateOne[ { _id: 1 }, { $push: { scores: { $each: [ 50, 60, 70 ], $position: 0 } } } ]
1 indicates the position just before the last element in the array.Thêm một tài liệu vào bộ sưu tập
db.students.updateOne[ { _id: 1 }, { $push: { scores: { $each: [ 50, 60, 70 ], $position: 0 } } } ]
5:db.students.insertOne[ { "_id" : 3, "scores" : [ 50, 60, 20, 30, 70, 100 ] } ]
Hoạt động sau đây cập nhật trường
db.students.updateOne[ { _id: 1 }, { $push: { scores: { $each: [ 50, 60, 70 ], $position: 0 } } } ]
6 để thêm các phần tử { "_id" : 1, "scores" : [ 50, 60, 70, 100 ] }
2 và { "_id" : 1, "scores" : [ 50, 60, 70, 100 ] }
3 tại chỉ số mảng của { "_id" : 1, "scores" : [ 50, 60, 70, 100 ] }
4:db.students.insertOne[ { "_id" : 1, "scores" : [ 100 ] } ]
0 to add db.students.insertOne[ { "_id" : 2, "scores" : [ 50, 60, 70, 100 ] } ]
0 at the position two places before the last element, and then db.students.insertOne[ { "_id" : 2, "scores" : [ 50, 60, 70, 100 ] } ]
1 at the position two places before the last element.db.students.insertOne[ { "_id" : 1, "scores" : [ 100 ] } ]
0 có thể chấp nhận giá trị chỉ số mảng âm để chỉ ra vị trí bắt đầu từ cuối, đếm từ [nhưng không bao gồm] phần tử cuối cùng của mảng. Ví dụ: db.students.updateOne[ { _id: 1 }, { $push: { scores: { $each: [ 50, 60, 70 ], $position: 0 } } } ]
1 chỉ ra vị trí ngay trước phần tử cuối cùng trong mảng.
db.students.insertOne[ { "_id" : 1, "scores" : [ 100 ] } ]
db.students.updateOne[ { _id: 1 }, { $push: { scores: { $each: [ 50, 60, 70 ], $position: 0 } } } ]
Thêm tài liệu sau vào bộ sưu tập
db.students.updateOne[ { _id: 1 }, { $push: { scores: { $each: [ 50, 60, 70 ], $position: 0 } } } ]
5:db.students.updateOne[ { _id: 3 }, { $push: { scores: { $each: [ 90, 80 ], $position: -2 } } } ]
Hoạt động kết quả trong tài liệu cập nhật sau:
{ "_id" : 3, "scores" : [ 50, 60, 20, 30, 90, 80, 70, 100 ] }