Bạn có thể có chức năng mong muốn bằng cách khớp với các thuộc tính tài liệu đầy đủ, ngoại trừ thuộc tính bạn muốn cập nhật. Ví dụ bên dưới
Chèn tài liệu duy nhất vào bộ sưu tập
db.collection.insert[{ name: "John", surname: "Connor", age: 32, role: "commander", character: "cautious", hobby: "kill terminators" }]
Hiện nay
db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
lợi nhuận
{ "_id" : ObjectId["52fc9a02d342217e6262aa28"], "name" : "John", "surname" : "Connor", "age" : 32, "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }
Tuyên bố cập nhật
db.collection.update[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" },{$set:{age:33}}, { upsert: true }]
cập nhật thuộc tính 'tuổi' của tài liệu hiện có
{ "_id" : ObjectId["52fc9a02d342217e6262aa28"], "name" : "John", "surname" : "Connor", "age" : 33, "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }
Nếu bạn chạy chính xác câu lệnh cập nhật thay đổi tên [do đó, truy vấn tìm sẽ không tìm thấy tài liệu hiện có]
db.collection.update[{"name" : "Robot", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" },{$set:{age:33}}, { upsert: true }]
Tài liệu "Robot Connor" sẽ được tạo với tuổi 33 và bạn sẽ có hai tài liệu trong bộ sưu tập của mình
db.collection.find[]
{ "_id" : ObjectId["52fc9a02d342217e6262aa28"], "name" : "John", "surname" : "Connor", "age" : 33, "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }
{ "_id" : ObjectId["52fc9a5f56641f5e1b883677"], "age" : 33, "character" : "cautious", "hobby" : "kill terminators", "name" : "Robot", "role" : "commander", "surname" : "Connor" }
Ghi chú. Tôi không tin rằng các thuộc tính bổ sung trên tham số truy vấn cập nhật có tác động đến hiệu suất
Có tổng cộng 100 [100 id khác nhau]. Tôi đã thêm chúng vào cơ sở dữ liệu mongodb của mình bằng cầy mangut nhưng vấn đề là nó luôn thêm [tôi đã có 5000 người dùng [50 lần lặp lại 100 người dùng]
Tôi muốn thêm nếu
db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
04 không tồn tại hoặc cập nhật nếu nó tồn tạiTôi đang làm gì sai?
db.collection["users"].insertMany[users, function [error, response] {
if [error] throw error;
console.log["Number of users inserted: " + response.insertedCount];
db.close[];
}];
- stackoverflow. comghi bàn. 0
Nhìn vào tài liệu mongodb, có vẻ như ________ 106 không phải là thứ bạn muốn sử dụng ở đây. Bạn có thể muốn sử dụng
db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
07 vì nó hỗ trợ db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
08 trong khi db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
06 thì khôngBạn sẽ có thể sử dụng một cái gì đó như thế này
________số 8Điều này đang sử dụng một bộ lọc trống, vì vậy sẽ chèn nếu mục không tồn tại hoặc cập nhật nếu có
ghi bàn. 0
db.collection.updateMany[
,
,
{
upsert:
}
]
Phương thức updateMany[] nhận các tham số sau
Bộ lọc
Tiêu chí lựa chọn cho bản cập nhật. Bộ chọn truy vấn tương tự như trong phương thức find[] có sẵn.
- Chỉ định một tài liệu trống { } để cập nhật tất cả các tài liệu trong bộ sưu tập
Cập nhật
Các sửa đổi sẽ được áp dụng.
- Sử dụng Toán tử cập nhật như $set, $unset hoặc $rename
Upsert
Tùy chọn. Khi đúng, updateMany[] hoặc.
Tạo một tài liệu mới nếu không có tài liệu nào phù hợp với bộ lọc. Để biết thêm chi tiết, xem hành vi upsert. Cập nhật tài liệu phù hợp với bộ lọc. Để tránh nhiều uperts, hãy đảm bảo rằng các trường bộ lọc được lập chỉ mục duy nhất
Trong bài viết này, việc chèn bản ghi vào bộ sưu tập MongoDB được thảo luận ngắn gọn. Các cách khác nhau để chèn các bản ghi này cũng được giải thích
Ngoài ra,
db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
10 và db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
11 được đề cập ngắn gọndb.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
10 trong MongoDB
db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
10 là một tùy chọn MongoDB được sử dụng cho các hoạt động cập nhật như db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
02, db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
03, v.v. Hay nói cách khác, db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
04 là kết quả của việc kết hợp giữa cập nhật và chèn [cập nhật + chèn = db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
04]Nếu giá trị của tùy chọn là
db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
06 và tài liệu hoặc các tài liệu khớp với truy vấn đã chỉ định được xác định, hành động cập nhật sẽ cập nhật tài liệu hoặc các tài liệu phù hợp. Ngoài ra, giả sử giá trị của tùy chọn này là db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
06 và không có tài liệu hoặc tài liệu nào khớp với tài liệu được cung cấpTrong trường hợp đó, tùy chọn này sẽ tạo một tài liệu mới trong bộ sưu tập với các trường được chỉ định trong hoạt động. Giá trị tùy chọn hoạt động của
db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
04 là db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
09 theo mặc địnhNếu giá trị
db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
04 trong bộ sưu tập được chia sẻ là db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
06, thì bạn phải đưa toàn bộ khóa được chia sẻ vào bộ lọccú pháp
Giá trị tùy chọn
db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
04 là db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
06 hoặc db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
09Bây giờ bạn sẽ học cách sử dụng tùy chọn
db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
04db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
10 Với phương thức db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
03 trong MongoDB
db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
Với chức năng
db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
03, bạn có thể sử dụng tùy chọn db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
04. Giá trị mặc định của tùy chọn này trong phương pháp này là ________ 109Nếu bạn đặt giá trị của tùy chọn này thành
db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
06, quy trình sẽ tiến hành một trong các thao tác sau- Nếu một tài liệu hoặc các tài liệu được tìm thấy phù hợp với tiêu chí truy vấn đã cho, phương thức
03 sẽ cập nhật tài liệu/tài liệudb.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
- Nếu không có tài liệu/tài liệu nào phù hợp với tiêu chí truy vấn đã cho, thì phương thức
03 sẽ chèn một tài liệu mới vào bộ sưu tậpdb.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
cú pháp
db.collection.update[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" },{$set:{age:33}}, { upsert: true }]
1Bằng cách điều chỉnh giá trị của tùy chọn
db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
04 thành db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
06, bây giờ bạn sẽ chèn một tài liệu mới vào bộ sưu tập db.collection.update[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" },{$set:{age:33}}, { upsert: true }]
46db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
0Ở đây, giá trị của tùy chọn
db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
04 được đặt thành db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
06; Với chức năng
db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
02, bạn có thể sử dụng tùy chọn db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
04. Giá trị mặc định của tham số này trong chức năng này là db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
09Nếu bạn đặt giá trị của tùy chọn này thành
db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
06, quy trình sẽ thực hiện một trong các thao tác sau- Nếu một hoặc nhiều tài liệu được tìm thấy phù hợp với tiêu chí truy vấn đã cho, thì phương thức
02 sẽ cập nhật tài liệu/tài liệu đódb.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
- Nếu không có tài liệu nào đáp ứng tiêu chí truy vấn đã chỉ định, hàm
02 sẽ thêm một tài liệu mới vào bộ sưu tậpdb.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
Tạo một chỉ mục duy nhất trên trường tên để ngăn MongoDB thêm cùng một tài liệu nhiều lần. Ví dụ: nếu nhiều tài liệu yêu cầu cùng một bản cập nhật với
db.collection.update[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" },{$set:{age:33}}, { upsert: true }]
99, thì chỉ một hành động { "_id" : ObjectId["52fc9a02d342217e6262aa28"], "name" : "John", "surname" : "Connor", "age" : 33, "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }
90 sẽ chèn thành công tài liệu mới có chỉ mục duy nhấtcú pháp
db.collection.update[{"name" : "Robot", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" },{$set:{age:33}}, { upsert: true }]
0Bằng cách thay đổi giá trị của tùy chọn
db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
04 thành db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
06, bạn sẽ chèn một tài liệu mới vào bộ sưu tập db.collection.update[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" },{$set:{age:33}}, { upsert: true }]
46db.collection.update[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" },{$set:{age:33}}, { upsert: true }]
4Vì giá trị cho tùy chọn
db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
04 được đặt thành db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
06, nên hàm db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
02 sẽ chèn một tài liệu mới có hai trường [{ "_id" : ObjectId["52fc9a02d342217e6262aa28"], "name" : "John", "surname" : "Connor", "age" : 33, "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }
97 và { "_id" : ObjectId["52fc9a02d342217e6262aa28"], "name" : "John", "surname" : "Connor", "age" : 33, "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }
98] vì không có tài liệu nào khớp với tên { "_id" : ObjectId["52fc9a02d342217e6262aa28"], "name" : "John", "surname" : "Connor", "age" : 33, "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }
99db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
10 Với các biểu thức toán tử trong MongoDB
db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
Giả sử không có tài liệu nào từ bộ sưu tập nhất định khớp với bộ lọc. Trong trường hợp đó, tham số
{ "_id" : ObjectId["52fc9a02d342217e6262aa28"], "name" : "John", "surname" : "Connor", "age" : 33, "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }
90 là một tài liệu có toán tử { "_id" : ObjectId["52fc9a02d342217e6262aa28"], "name" : "John", "surname" : "Connor", "age" : 33, "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }
90Giá trị của tùy chọn
db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
04 là db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
06; Hoặc, nói cách khác, khi tùy chọn
db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
04 là db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
06 và không có tài liệu nào phù hợp với bộ lọc được cung cấp, thao tác { "_id" : ObjectId["52fc9a02d342217e6262aa28"], "name" : "John", "surname" : "Connor", "age" : 33, "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }
90 sẽ tạo một tài liệu mới trong bộ sưu tập đã cho, với các trường được chỉ định trong tài liệu db.collection.find[]
{ "_id" : ObjectId["52fc9a02d342217e6262aa28"], "name" : "John", "surname" : "Connor", "age" : 33, "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }
{ "_id" : ObjectId["52fc9a5f56641f5e1b883677"], "age" : 33, "character" : "cautious", "hobby" : "kill terminators", "name" : "Robot", "role" : "commander", "surname" : "Connor" }
66 và { "_id" : ObjectId["52fc9a02d342217e6262aa28"], "name" : "John", "surname" : "Connor", "age" : 33, "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }
90Ví dụ
Bằng cách thay đổi giá trị của tùy chọn
db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
04 thành db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
06, bạn sẽ đưa một tài liệu mới vào bộ sưu tập db.collection["users"].insertMany[users, function [error, response] {
if [error] throw error;
console.log["Number of users inserted: " + response.insertedCount];
db.close[];
}];
55db.collection.update[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" },{$set:{age:33}}, { upsert: true }]
9Hàm
db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
02 tạo một tài liệu mới với trường điều kiện truy vấn db.collection["users"].insertMany[users, function [error, response] {
if [error] throw error;
console.log["Number of users inserted: " + response.insertedCount];
db.close[];
}];
57 và áp dụng các hành động db.collection["users"].insertMany[users, function [error, response] {
if [error] throw error;
console.log["Number of users inserted: " + response.insertedCount];
db.close[];
}];
58 và db.collection["users"].insertMany[users, function [error, response] {
if [error] throw error;
console.log["Number of users inserted: " + response.insertedCount];
db.close[];
}];
59db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
10 Với tài liệu thay thế trong MongoDB
db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
Giả sử không có tài liệu nào từ bộ sưu tập được cung cấp đáp ứng bộ lọc và tham số
{ "_id" : ObjectId["52fc9a02d342217e6262aa28"], "name" : "John", "surname" : "Connor", "age" : 33, "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }
90 bao gồm tài liệu thay thế và giá trị của tài liệu db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
04 được đặt thành db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
06. Hành động { "_id" : ObjectId["52fc9a02d342217e6262aa28"], "name" : "John", "surname" : "Connor", "age" : 33, "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }
90 chèn một tài liệu mới vào bộ sưu tập, với các trường được chỉ định trong tài liệu thay thếMongoDB không tạo trường
db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
105 duy nhất cho tài liệu mới nếu tài liệu thay thế bao gồm trường db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
105. Ngoài ra, nếu tài liệu thay thế thiếu trường db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
105, MongoDB sẽ tạo trường db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
105 mới cho tài liệu mớiLưu ý rằng các giá trị trường
db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
105 riêng biệt trong tham số truy vấn và tài liệu thay thế không được phép. Nếu bạn làm như vậy, bạn sẽ gặp phải vấn đềVí dụ
Bằng cách điều chỉnh giá trị của tùy chọn
db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
04 thành db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
06, bây giờ bạn sẽ chèn một tài liệu mới vào bộ sưu tập db.collection["users"].insertMany[users, function [error, response] {
if [error] throw error;
console.log["Number of users inserted: " + response.insertedCount];
db.close[];
}];
55{ "_id" : ObjectId["52fc9a02d342217e6262aa28"], "name" : "John", "surname" : "Connor", "age" : 33, "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }
9db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
10 Với Aggregation Pipeline trong MongoDB
db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
Quy trình tổng hợp là quy trình nhiều giai đoạn, trong đó các tài liệu được chấp nhận làm đầu vào và được tạo dưới dạng tập hợp các tài liệu kết quả
Các tài liệu kết quả sau đó được lấy làm đầu vào và được tạo ở bước tiếp theo [nếu có] cho đến giai đoạn cuối cùng. Số lượng giai đoạn trong quy trình có thể nằm trong khoảng từ
db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
114 đến db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
115Giả sử không có tài liệu nào phù hợp với bộ lọc đã chỉ định và tham số
{ "_id" : ObjectId["52fc9a02d342217e6262aa28"], "name" : "John", "surname" : "Connor", "age" : 33, "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }
90 bao gồm một đường dẫn tổng hợp và giá trị của tùy chọn db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
04 được đặt thành db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
06. Trong trường hợp đó, hoạt động { "_id" : ObjectId["52fc9a02d342217e6262aa28"], "name" : "John", "surname" : "Connor", "age" : 33, "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }
90 sẽ chèn một tài liệu mới vào bộ sưu tậpTài liệu mới này được hình thành bằng cách sử dụng mệnh đề bình đẳng trong tham số
db.collection.find[]
{ "_id" : ObjectId["52fc9a02d342217e6262aa28"], "name" : "John", "surname" : "Connor", "age" : 33, "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }
{ "_id" : ObjectId["52fc9a5f56641f5e1b883677"], "age" : 33, "character" : "cautious", "hobby" : "kill terminators", "name" : "Robot", "role" : "commander", "surname" : "Connor" }
66, sau đó đường dẫn được áp dụng cho nó để tạo tài liệu cần chènVí dụ
Bằng cách điều chỉnh giá trị của tùy chọn
db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
04 thành db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
06, bây giờ bạn sẽ chèn một tài liệu mới vào bộ sưu tập db.collection.update[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" },{$set:{age:33}}, { upsert: true }]
46db.collection.find[]
{ "_id" : ObjectId["52fc9a02d342217e6262aa28"], "name" : "John", "surname" : "Connor", "age" : 33, "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }
{ "_id" : ObjectId["52fc9a5f56641f5e1b883677"], "age" : 33, "character" : "cautious", "hobby" : "kill terminators", "name" : "Robot", "role" : "commander", "surname" : "Connor" }
6db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
10 Với truy vấn db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
105 có dấu chấm trong MongoDB
db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
Bạn đã thấy cách hàm
db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
02 có thể thay đổi dữ liệu trong bộ sưu tập tùy thuộc vào truy vấn và cách tùy chọn db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
04 có thể thêm trường mới nếu không tìm thấy tài liệu phù hợpTuy nhiên,
db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
04 với truy vấn db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
105 có dấu chấm là một ngoại lệ và việc cố gắng chèn tài liệu theo cách này sẽ dẫn đến lỗi từ MongoDBHình minh họa
Hãy xem thao tác cập nhật sau. Bản cập nhật sẽ không thành công trong khi tạo tài liệu để chèn do thao tác
{ "_id" : ObjectId["52fc9a02d342217e6262aa28"], "name" : "John", "surname" : "Connor", "age" : 33, "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }
90 chỉ định db.collection.update[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" },{$set:{age:33}}, { upsert: true }]
99 và truy vấn cung cấp các điều kiện trên trường db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
105 bằng cách sử dụng ký hiệu dấu chấm db.collection["users"].insertMany[users, function [error, response] {
if [error] throw error;
console.log["Number of users inserted: " + response.insertedCount];
db.close[];
}];
5Vì vậy, bài viết này đã thảo luận về vấn đề chèn bản ghi vào các trường trống trong MongoDB.
db.collection.find[{"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }]
10 được giải thích ngắn gọn với các kịch bản khác nhau