Tài liệu về nhà → Hướng dẫn sử dụng MongoDB → MongoDB Manual
db.pigments.insertOne[ { _id: 1, colors: "blue, green, red" } ]
2 Trình khai thác db.pigments.insertOne[ { _id: 1, colors: "blue, green, red" } ]
2 thêm giá trị vào một mảng trừ khi giá trị đã có mặt, trong trường hợp đó db.pigments.insertOne[ { _id: 1, colors: "blue, green, red" } ]
2 không làm gì với mảng đó.The db.pigments.insertOne[ { _id: 1, colors: "blue, green, red" } ] db.pigments.insertOne[ { _id: 1, colors: "blue, green, red" } ]
2 operator adds a value to an array unless the value is already present, in which case
2 does nothing to that array.
Toán tử
db.pigments.insertOne[ { _id: 1, colors: "blue, green, red" } ]
2 có biểu mẫu:db.pigments.insertOne[ { _id: 1, colors: "blue, green, red" } ]
2 operator has the form:{ $addToSet: { : , ... } }
Để chỉ định
db.pigments.insertOne[ { _id: 1, colors: "blue, green, red" } ]
6 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.
db.pigments.insertOne[ { _id: 1, colors: "blue, green, red" } ] db.pigments.insertOne[ { _id: 1, colors: "blue, green, red" } ] db.pigments.insertOne[ { _id: 1, colors: "blue, green, red" } ]
2 Chỉ đảm bảo rằng không có mục trùng lặp được thêm vào tập hợp và không ảnh hưởng đến các yếu tố trùng lặp hiện có.
2 không đảm bảo một thứ tự cụ thể của các phần tử trong bộ sửa đổi. only ensures that there are no duplicate items added
to the set and does not affect existing duplicate elements.
2 does not guarantee a particular ordering of elements in the modified set.
Bắt đầu từ MongoDB 5.0,
db.pigments.insertOne[ { _id: 1, colors: "blue, green, red" } ]
9 không còn gây ra lỗi khi bạn sử dụng toán tử cập nhật như db.pigments.insertOne[ { _id: 1, colors: "blue, green, red" } ]
2 với biểu thức toán hạng trống [db.pigments.updateOne[ { _id: 1 }, { $addToSet: { colors: "mauve" } } ]
1]. 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].db.pigments.insertOne[ { _id: 1, colors: "blue, green, red" } ]
2 with an empty operand expression [ db.pigments.updateOne[ { _id: 1 }, { $addToSet: { colors: "mauve" } } ]
1 ]. An empty update results in no changes and no oplog entry is created [meaning that the operation is a no-op].Nếu bạn sử dụng
db.pigments.insertOne[ { _id: 1, colors: "blue, green, red" } ]
2 trên một trường không có trong tài liệu để cập nhật, db.pigments.insertOne[ { _id: 1, colors: "blue, green, red" } ]
2 sẽ tạo trường mảng với giá trị được chỉ định làm phần tử của nó.db.pigments.insertOne[ { _id: 1, colors: "blue, green, red" } ]
2 on a field that is absent from the document to update, db.pigments.insertOne[ { _id: 1, colors: "blue, green, red" } ]
2 creates the array field with the specified value as its element.Nếu bạn sử dụng
db.pigments.insertOne[ { _id: 1, colors: "blue, green, red" } ]
2 trên một trường không phải là một mảng, thao tác sẽ thất bại.db.pigments.insertOne[ { _id: 1, colors: "blue, green, red" } ]
2 on a field that is not an array, the operation will fail.Ví dụ: tạo bộ sưu tập
db.pigments.updateOne[ { _id: 1 }, { $addToSet: { colors: "mauve" } } ]
5:db.pigments.insertOne[ { _id: 1, colors: "blue, green, red" } ]
Trường
db.pigments.updateOne[ { _id: 1 }, { $addToSet: { colors: "mauve" } } ]
6 không phải là một mảng. Hoạt động db.pigments.insertOne[ { _id: 1, colors: "blue, green, red" } ]
2 sau đây không thành công:db.pigments.insertOne[ { _id: 1, colors: "blue, green, red" } ]
2 operation fails:db.pigments.updateOne[ { _id: 1 }, { $addToSet: { colors: "mauve" } } ]
Nếu giá trị là một mảng,
db.pigments.insertOne[ { _id: 1, colors: "blue, green, red" } ]
2 nối toàn bộ mảng là một phần tử duy nhất.db.pigments.insertOne[ { _id: 1, colors: "blue, green, red" } ]
2 appends the whole array as a single element.Tạo bộ sưu tập
db.pigments.updateOne[ { _id: 1 }, { $addToSet: { colors: "mauve" } } ]
9:
db.alphabet.insertOne[ { _id: 1, letters: ["a", "b"] } ]
Hoạt động sau đây nối cho mảng
db.alphabet.insertOne[ { _id: 1, letters: ["a", "b"] } ]
0 vào trường db.alphabet.insertOne[ { _id: 1, letters: ["a", "b"] } ]
1:db.alphabet.updateOne[ { _id: 1 }, { $addToSet: { letters: [ "c", "d" ] } } ]
Mảng
db.alphabet.insertOne[ { _id: 1, letters: ["a", "b"] } ]
0 được thêm vào dưới dạng một phần tử:{ _id: 1, letters: [ 'a', 'b', [ 'c', 'd' ] ] }
Mẹo
Nếu giá trị là một tài liệu, MongoDB xác định rằng tài liệu là một bản sao nếu một tài liệu hiện có trong mảng khớp với tài liệu cần thêm chính xác; tức là tài liệu hiện có có cùng các trường và giá trị chính xác và các trường theo cùng một thứ tự. Do đó, các vấn đề thứ tự trường và bạn không thể chỉ định rằng MongoDB chỉ so sánh một tập hợp con của các trường trong tài liệu để xác định xem tài liệu có phải là bản sao của một phần tử mảng hiện có hay không.
Tạo bộ sưu tập
db.alphabet.insertOne[ { _id: 1, letters: ["a", "b"] } ]
3:db.inventory.insertOne[ { _id: 1, item: "polarizing_filter", tags: [ "electronics", "camera" ] } ]
Hoạt động sau đây thêm phần tử
db.alphabet.insertOne[ { _id: 1, letters: ["a", "b"] } ]
4 vào mảng db.alphabet.insertOne[ { _id: 1, letters: ["a", "b"] } ]
5 vì db.alphabet.insertOne[ { _id: 1, letters: ["a", "b"] } ]
4 không tồn tại trong mảng:db.inventory.updateOne[ { _id: 1 }, { $addToSet: { tags: "accessories" } } ]
Hoạt động
db.pigments.insertOne[ { _id: 1, colors: "blue, green, red" } ]
2 sau đây không có hiệu lực vì db.alphabet.insertOne[ { _id: 1, letters: ["a", "b"] } ]
8 đã là một yếu tố của mảng db.alphabet.insertOne[ { _id: 1, letters: ["a", "b"] } ]
5:db.pigments.insertOne[ { _id: 1, colors: "blue, green, red" } ]
2 operation has no effect because db.alphabet.insertOne[ { _id: 1, letters: ["a", "b"] } ]
8 is already an element of the db.alphabet.insertOne[ { _id: 1, letters: ["a", "b"] } ]
5 array:db.inventory.updateOne[ { _id: 1 }, { $addToSet: { tags: "camera" } } ]
Bạn có thể sử dụng toán tử
db.pigments.insertOne[ { _id: 1, colors: "blue, green, red" } ]
2 với công cụ sửa đổi db.alphabet.updateOne[ { _id: 1 }, { $addToSet: { letters: [ "c", "d" ] } } ]
1. Trình sửa đổi db.alphabet.updateOne[ { _id: 1 }, { $addToSet: { letters: [ "c", "d" ] } } ]
1 cho phép toán tử db.pigments.insertOne[ { _id: 1, colors: "blue, green, red" } ]
2 thêm nhiều giá trị vào trường mảng.db.pigments.insertOne[ { _id: 1, colors: "blue, green, red" } ]
2 operator with the
db.alphabet.updateOne[ { _id: 1 }, { $addToSet: { letters: [ "c", "d" ] } } ]
1 modifier. The db.alphabet.updateOne[ { _id: 1 }, { $addToSet: { letters: [ "c", "d" ] } } ]
1 modifier allows the db.pigments.insertOne[ { _id: 1, colors: "blue, green, red" } ]
2 operator to add multiple values to the array
field.
Bộ sưu tập
db.alphabet.insertOne[ { _id: 1, letters: ["a", "b"] } ]
3 có tài liệu sau:{ _id: 2, item: "cable", tags: [ "electronics", "supplies" ] }
Sau đó, thao tác sau sử dụng toán tử
db.pigments.insertOne[ { _id: 1, colors: "blue, green, red" } ]
2 với công cụ sửa đổi db.alphabet.updateOne[ { _id: 1 }, { $addToSet: { letters: [ "c", "d" ] } } ]
1 để thêm nhiều phần tử vào mảng db.alphabet.insertOne[ { _id: 1, letters: ["a", "b"] } ]
5:db.pigments.insertOne[ { _id: 1, colors: "blue, green, red" } ]
2 operator with the db.alphabet.updateOne[ { _id: 1 }, { $addToSet: { letters: [ "c", "d" ] } } ]
1 modifier to add multiple elements to the db.alphabet.insertOne[ { _id: 1, letters: ["a", "b"] } ]
5 array:db.pigments.insertOne[ { _id: 1, colors: "blue, green, red" } ]
0Hoạt động chỉ thêm
db.alphabet.insertOne[ { _id: 1, letters: ["a", "b"] } ]
8 và db.alphabet.insertOne[ { _id: 1, letters: ["a", "b"] } ]
4 vào mảng db.alphabet.insertOne[ { _id: 1, letters: ["a", "b"] } ]
5. { _id: 1, letters: [ 'a', 'b', [ 'c', 'd' ] ] }
1 đã ở trong mảng:db.pigments.insertOne[ { _id: 1, colors: "blue, green, red" } ]
1