Chèn mongodb nếu không tồn tại

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

Show

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ại

Tô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. com

ghi 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ông

Bạ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ọn

db.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" })
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ấp

Trong 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 định

Nế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ọc

cú 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" })
09

Bâ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" })
04

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

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à ________ 109

Nế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

  1. 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
    db.collection.find({"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" })
    
    03 sẽ cập nhật tài liệu/tài liệu
  2. 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
    db.collection.find({"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" })
    
    03 sẽ chèn một tài liệu mới vào bộ sưu tập

cú pháp

db.collection.update({"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" },{$set:{age:33}}, { upsert: true })
1

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 })
46

db.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;

findandmodify method

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" })
09

Nế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

  1. 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
    db.collection.find({"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" })
    
    02 sẽ cập nhật tài liệu/tài liệu đó
  2. Nếu không có tài liệu nào đáp ứng tiêu chí truy vấn đã chỉ định, hàm
    db.collection.find({"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" })
    
    02 sẽ thêm một tài liệu mới vào bộ sưu tập

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ất

cú pháp

db.collection.update({"name" : "Robot", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" },{$set:{age:33}}, { upsert: true })
0

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ẽ 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 })
46

db.collection.update({"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" },{$set:{age:33}}, { upsert: true })
4

Vì 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" }
99

update method

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

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" }
90

Giá 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" }
90

Ví 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();
                    });
55

db.collection.update({"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" },{$set:{age:33}}, { upsert: true })
9

Hà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();
                    });
59

operator expression

db.collection.find({"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }) 10 Với tài liệu thay thế trong MongoDB

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ới

Lư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" }
9

replacement document

db.collection.find({"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" }) 10 Với Aggregation Pipeline trong MongoDB

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" })
115

Giả 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ập

Tà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è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.update({"name" : "John", "surname" : "Connor", "role" : "commander", "character" : "cautious", "hobby" : "kill terminators" },{$set:{age:33}}, { upsert: true })
46

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" }
6

aggregation pipeline

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

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ợp

Tuy 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ừ MongoDB

Hì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();
                    });
5

dotted query

Vì 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

Làm cách nào để chèn giá trị trong MongoDB?

insert() Trong MongoDB, phương thức insert() chèn một hoặc nhiều tài liệu vào bộ sưu tập . Nó nhận hai tham số, tham số đầu tiên là tài liệu hoặc mảng của tài liệu mà chúng ta muốn chèn và các tham số còn lại là tùy chọn. Sử dụng phương pháp này, bạn cũng có thể tạo một bộ sưu tập bằng cách chèn tài liệu.

Làm cách nào để chèn id trong MongoDB?

Nếu tài liệu không chỉ định trường _id, thì MongoDB sẽ thêm trường _id và gán một ObjectId() duy nhất cho tài liệu trước khi chèn. Most drivers create an ObjectId and insert the _id field, but the mongod will create and populate the _id if the driver or application does not.

Sự khác biệt giữa cập nhật và Upsert trong MongoDB là gì?

Hay nói cách khác, upsert là sự kết hợp giữa cập nhật và chèn (cập nhật + chèn = upsert). Nếu giá trị của tùy chọn này được đặt thành true và tài liệu hoặc các tài liệu được tìm thấy khớp với truy vấn đã chỉ định, thì thao tác cập nhật sẽ cập nhật tài liệu hoặc các tài liệu phù hợp.

Làm cách nào để sử dụng $set trong MongoDB?

Toán tử $set thay thế giá trị của một trường bằng giá trị đã chỉ định . Biểu thức toán tử $set có dạng như sau. { $set. {