Ghi chú
Định hướng
Trang sau đề cập đến toán tử cập nhật $set
. Đối với giai đoạn tổng hợp $set
, có sẵn trong MongoDB 4.2, xem $set
$set
. For the aggregation stage $set
, available starting in MongoDB 4.2, see $set
$set
Trình điều khiển $set
thay thế giá trị của một trường bằng giá trị được chỉ định.The $set
operator replaces the value of a field with the specified value.
Biểu thức toán tử $set
có dạng sau:$set
operator expression has the following form:
{ $set: { : , ... } }
Để chỉ định
db.products.insertOne[ |
{ |
_id: 100, |
quantity: 250, |
instock: true, |
reorder: false, |
details: { model: "14QQ", make: "Clothes Corp" }, |
tags: [ "apparel", "clothing" ], |
ratings: [ { by: "Customer007", rating: 4 } ] |
} |
] |
4 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 tồn tại, $set
sẽ thêm một trường mới với giá trị được chỉ định, với điều kiện là trường mới không vi phạm ràng buộc loại. Nếu bạn chỉ định một đường dẫn chấm cho một trường không tồn tại, $set
sẽ tạo các tài liệu nhúng khi cần thiết để thực hiện đường dẫn chấm đến trường.$set
will add a new field with the
specified value, provided that the new field does not violate a type constraint. If you specify a dotted path for a non-existent field, $set
will create the embedded documents as needed to fulfill the dotted path to the field.
Nếu bạn chỉ định nhiều cặp giá trị trường, $set
sẽ cập nhật hoặc tạo từng trường.$set
will update or create each field.
Bắt đầu từ MongoDB 5.0,
db.products.insertOne[ |
{ |
_id: 100, |
quantity: 250, |
instock: true, |
reorder: false, |
details: { model: "14QQ", make: "Clothes Corp" }, |
tags: [ "apparel", "clothing" ], |
ratings: [ { by: "Customer007", rating: 4 } ] |
} |
] |
8 không còn gây ra lỗi khi bạn sử dụng toán tử cập nhật như $set
với biểu thức toán hạng trống [db.products.updateOne[ |
{ _id: 100 }, |
{ $set: |
{ |
quantity: 500, |
details: { model: "2600", make: "Fashionaires" }, |
tags: [ "coats", "outerwear", "clothing" ] |
} |
} |
] |
0]. 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].$set
with an empty operand expression [ db.products.updateOne[ |
{ _id: 100 }, |
{ $set: |
{ |
quantity: 500, |
details: { model: "2600", make: "Fashionaires" }, |
tags: [ "coats", "outerwear", "clothing" ] |
} |
} |
] |
0 ]. An empty update results in no changes and no oplog entry is created [meaning that the operation is a no-op].Tạo bộ sưu tập
db.products.updateOne[ |
{ _id: 100 }, |
{ $set: |
{ |
quantity: 500, |
details: { model: "2600", make: "Fashionaires" }, |
tags: [ "coats", "outerwear", "clothing" ] |
} |
} |
] |
1:db.products.insertOne[ |
{ |
_id: 100, |
quantity: 250, |
instock: true, |
reorder: false, |
details: { model: "14QQ", make: "Clothes Corp" }, |
tags: [ "apparel", "clothing" ], |
ratings: [ { by: "Customer007", rating: 4 } ] |
} |
] |
Đối với tài liệu khớp với các tiêu chí
db.products.updateOne[ |
{ _id: 100 }, |
{ $set: |
{ |
quantity: 500, |
details: { model: "2600", make: "Fashionaires" }, |
tags: [ "coats", "outerwear", "clothing" ] |
} |
} |
] |
2 bằng db.products.updateOne[ |
{ _id: 100 }, |
{ $set: |
{ |
quantity: 500, |
details: { model: "2600", make: "Fashionaires" }, |
tags: [ "coats", "outerwear", "clothing" ] |
} |
} |
] |
3, thao tác sau sử dụng toán tử $set
để cập nhật giá trị của trường db.products.updateOne[ |
{ _id: 100 }, |
{ $set: |
{ |
quantity: 500, |
details: { model: "2600", make: "Fashionaires" }, |
tags: [ "coats", "outerwear", "clothing" ] |
} |
} |
] |
5, trường db.products.updateOne[ |
{ _id: 100 }, |
{ $set: |
{ |
quantity: 500, |
details: { model: "2600", make: "Fashionaires" }, |
tags: [ "coats", "outerwear", "clothing" ] |
} |
} |
] |
6 và trường db.products.updateOne[ |
{ _id: 100 }, |
{ $set: |
{ |
quantity: 500, |
details: { model: "2600", make: "Fashionaires" }, |
tags: [ "coats", "outerwear", "clothing" ] |
} |
} |
] |
7.$set
operator to update the value of the db.products.updateOne[ |
{ _id: 100 }, |
{ $set: |
{ |
quantity: 500, |
details: { model: "2600", make: "Fashionaires" }, |
tags: [ "coats", "outerwear", "clothing" ] |
} |
} |
] |
5 field, db.products.updateOne[ |
{ _id: 100 }, |
{ $set: |
{ |
quantity: 500, |
details: { model: "2600", make: "Fashionaires" }, |
tags: [ "coats", "outerwear", "clothing" ] |
} |
} |
] |
6 field, and the db.products.updateOne[ |
{ _id: 100 }, |
{ $set: |
{ |
quantity: 500, |
details: { model: "2600", make: "Fashionaires" }, |
tags: [ "coats", "outerwear", "clothing" ] |
} |
} |
] |
7 field.db.products.updateOne[ |
{ _id: 100 }, |
{ $set: |
{ |
quantity: 500, |
details: { model: "2600", make: "Fashionaires" }, |
tags: [ "coats", "outerwear", "clothing" ] |
} |
} |
] |
Hoạt động cập nhật:
Giá trị của
db.products.updateOne[ |
{ _id: 100 }, |
{ $set: |
{ |
quantity: 500, |
details: { model: "2600", make: "Fashionaires" }, |
tags: [ "coats", "outerwear", "clothing" ] |
} |
} |
] |
5 đến db.products.updateOne[ |
{ _id: 100 }, |
{ $set: |
{ |
quantity: 500, |
details: { model: "2600", make: "Fashionaires" }, |
tags: [ "coats", "outerwear", "clothing" ] |
} |
} |
] |
9Trường
db.products.updateOne[ |
{ _id: 100 }, |
{ $set: |
{ |
quantity: 500, |
details: { model: "2600", make: "Fashionaires" }, |
tags: [ "coats", "outerwear", "clothing" ] |
} |
} |
] |
6 với tài liệu nhúng mớiTrường
db.products.updateOne[ |
{ _id: 100 }, |
{ $set: |
{ |
quantity: 500, |
details: { model: "2600", make: "Fashionaires" }, |
tags: [ "coats", "outerwear", "clothing" ] |
} |
} |
] |
7 với mảng mới
{ |
_id: 100, |
quantity: 500, |
instock: true, |
reorder: false, |
details: { model: '2600', make: 'Fashionaires' }, |
tags: [ 'coats', 'outerwear', 'clothing' ], |
ratings: [ { by: 'Customer007', rating: 4 } ] |
} |
Để chỉ định
db.products.insertOne[ |
{ |
_id: 100, |
quantity: 250, |
instock: true, |
reorder: false, |
details: { model: "14QQ", make: "Clothes Corp" }, |
tags: [ "apparel", "clothing" ], |
ratings: [ { by: "Customer007", rating: 4 } ] |
} |
] |
4 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.products.updateOne[ |
{ _id: 100 }, |
{ $set: { "details.make": "Kustom Kidz" } } |
] |
Nếu trường không tồn tại, $set
sẽ thêm một trường mới với giá trị được chỉ định, với điều kiện là trường mới không vi phạm ràng buộc loại. Nếu bạn chỉ định một đường dẫn chấm cho một trường không tồn tại, $set
sẽ tạo các tài liệu nhúng khi cần thiết để thực hiện đường dẫn chấm đến trường.
{ |
_id: 100, |
quantity: 500, |
instock: true, |
reorder: false, |
details: { model: '2600', make: 'Kustom Kidz' }, |
tags: [ 'coats', 'outerwear', 'clothing' ], |
ratings: [ { by: 'Customer007', rating: 4 } ] |
} |
Để chỉ định
db.products.insertOne[ |
{ |
_id: 100, |
quantity: 250, |
instock: true, |
reorder: false, |
details: { model: "14QQ", make: "Clothes Corp" }, |
tags: [ "apparel", "clothing" ], |
ratings: [ { by: "Customer007", rating: 4 } ] |
} |
] |
4 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.products.updateOne[ |
{ _id: 100 }, |
{ $set: |
{ |
"tags.1": "rain gear", |
"ratings.0.rating": 2 |
} |
} |
] |
Nếu trường không tồn tại, $set
sẽ thêm một trường mới với giá trị được chỉ định, với điều kiện là trường mới không vi phạm ràng buộc loại. Nếu bạn chỉ định một đường dẫn chấm cho một trường không tồn tại, $set
sẽ tạo các tài liệu nhúng khi cần thiết để thực hiện đường dẫn chấm đến trường.
{ |
_id: 100, |
quantity: 500, |
instock: true, |
reorder: false, |
details: { model: '2600', make: 'Kustom Kidz' }, |
tags: [ 'coats', 'rain gear', 'clothing' ], |
ratings: [ { by: 'Customer007', rating: 2 } ] |
} |
Nếu bạn chỉ định nhiều cặp giá trị trường, $set
sẽ cập nhật hoặc tạo từng trường.
Bắt đầu từ MongoDB 5.0, db.products.insertOne[ |
{ |
_id: 100, |
quantity: 250, |
instock: true, |
reorder: false, |
details: { model: "14QQ", make: "Clothes Corp" }, |
tags: [ "apparel", "clothing" ], |
ratings: [ { by: "Customer007", rating: 4 } ] |
} |
] |
8 không còn gây ra lỗi khi bạn sử dụng toán tử cập nhật như $set
với biểu thức toán hạng trống [db.products.updateOne[ |
{ _id: 100 }, |
{ $set: |
{ |
quantity: 500, |
details: { model: "2600", make: "Fashionaires" }, |
tags: [ "coats", "outerwear", "clothing" ] |
} |
} |
] |
0]. 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].