Tài liệu về nhà → Hướng dẫn sử dụng MongoDB → MongoDB Manual
$not
SYNTAX: db.inventory.find[ { item: { $not: /^p.*/ } } ]
0Syntax:
db.inventory.find[ { item: { $not: /^p.*/ } } ]
0db.inventory.find[ { item: { $not: /^p.*/ } } ] db.inventory.find[ { item: { $not: /^p.*/ } } ] db.inventory.find[ { item: { $not: /^p.*/ } } ] db.inventory.find[ { item: { $not: /^p.*/ } } ]$not
thực hiện hoạt động logic
2 trên
3 được chỉ định và chọn các tài liệu không khớp với
3. Điều này bao gồm các tài liệu không chứa
5. performs a logical
db.inventory.find[ { item: { $not: /^p.*/ } } ]
2 operation on the specified db.inventory.find[ { item: { $not: /^p.*/ } } ]
3 and selects the documents that do not match the db.inventory.find[ { item: { $not: /^p.*/ } } ]
3. This includes documents that do not contain the db.inventory.find[ { item: { $not: /^p.*/ } } ]
5.Xem xét các truy vấn sau:
db.inventory.find[ { price: { $not: { $gt: 1.99 } } } ]
Truy vấn này sẽ chọn tất cả các tài liệu trong bộ sưu tập
db.inventory.find[ { item: { $not: /^p.*/ } } ]
6 trong đó:Giá trị trường
7 nhỏ hơn hoặc bằngdb.inventory.find[ { item: { $not: /^p.*/ } } ]
8 hoặcordb.inventory.find[ { item: { $not: /^p.*/ } } ]
Trường
7 không tồn tạidb.inventory.find[ { item: { $not: /^p.*/ } } ]
db.inventory.find[ { item: { $not: { $regex: "^p.*" } } } ] db.inventory.find[ { item: { $not: { $regex: /^p.*/ } } } ]
0 khác với toán tử db.inventory.find[ { item: { $not: { $regex: "^p.*" } } } ] db.inventory.find[ { item: { $not: { $regex: /^p.*/ } } } ]
1. db.inventory.find[ { item: { $not: { $regex: "^p.*" } } } ] db.inventory.find[ { item: { $not: { $regex: /^p.*/ } } } ]
2 chỉ trả về các tài liệu trong đó trường db.inventory.find[ { item: { $not: /^p.*/ } } ]
7 tồn tại và giá trị của nó nhỏ hơn hoặc bằng db.inventory.find[ { item: { $not: /^p.*/ } } ]
8.Hãy nhớ rằng toán tử $not
chỉ ảnh hưởng đến các nhà khai thác khác và không thể kiểm tra các trường và tài liệu một cách độc lập. Vì vậy, sử dụng toán tử $not
để phân tách logic và toán tử
db.inventory.find[ { item: { $not: { $regex: "^p.*" } } } ] db.inventory.find[ { item: { $not: { $regex: /^p.*/ } } } ]
7 để kiểm tra nội dung của các trường trực tiếp.$not
operator only affects other operators and cannot check fields and documents independently. So, use the $not
operator for logical disjunctions and the
db.inventory.find[ { item: { $not: { $regex: "^p.*" } } } ] db.inventory.find[ { item: { $not: { $regex: /^p.*/ } } } ]
7 operator to test the contents of fields directly.Hoạt động của toán tử $not
phù hợp với hành vi của các nhà khai thác khác nhưng có thể mang lại kết quả bất ngờ với một số loại dữ liệu như mảng.$not
operator is consistent with the behavior of other operators but may yield unexpected results with some data types like
arrays.
Toán tử db.inventory.find[ { item: { $not: /^p.*/ } } ]$not
có thể thực hiện hoạt động logic
2 trên: operator can perform logical
db.inventory.find[ { item: { $not: /^p.*/ } } ]
2 operation on:Các đối tượng biểu thức chính quy [tức là
1]import re for noMatch in db.inventory.find[ { "item": { "$not": re.compile["^p.*"] } } ]: print noMatch Ví dụ: truy vấn sau chọn tất cả các tài liệu trong bộ sưu tập
6 trong đó giá trị trườngdb.inventory.find[ { item: { $not: /^p.*/ } } ]
3 không bắt đầu với chữ cáiimport re for noMatch in db.inventory.find[ { "item": { "$not": re.compile["^p.*"] } } ]: print noMatch
4.import re for noMatch in db.inventory.find[ { "item": { "$not": re.compile["^p.*"] } } ]: print noMatch db.inventory.find[ { item: { $not: /^p.*/ } } ]
5 Biểu thức toán tửimport re for noMatch in db.inventory.find[ { "item": { "$not": re.compile["^p.*"] } } ]: print noMatch Ví dụ: truy vấn sau chọn tất cả các tài liệu trong bộ sưu tập
6 trong đó giá trị trườngdb.inventory.find[ { item: { $not: /^p.*/ } } ]
3 không bắt đầu với chữ cáiimport re for noMatch in db.inventory.find[ { "item": { "$not": re.compile["^p.*"] } } ]: print noMatch
4.import re for noMatch in db.inventory.find[ { "item": { "$not": re.compile["^p.*"] } } ]: print noMatch db.inventory.find[ { item: { $not: { $regex: "^p.*" } } } ] db.inventory.find[ { item: { $not: { $regex: /^p.*/ } } } ]
5 Biểu thức toán tửimport re for noMatch in db.inventory.find[ { "item": { "$not": re.compile["^p.*"] } } ]: print noMatch Các đối tượng biểu thức chính quy của trình điều khiểnPyMongo query uses Python's
9 method to compile a regular expression:import re for noMatch in db.inventory.find[ { "item": { "$not": re.compile["^p.*"] } } ]: print noMatch import re for noMatch in db.inventory.find[ { "item": { "$not": re.compile["^p.*"] } } ]: print noMatch
Ví dụ: truy vấn Pymongo sau đây sử dụng phương thức import re for noMatch in db.inventory.find[ { "item": { "$not": re.compile["^p.*"] } } ]: print noMatch
9 của Python để biên dịch một biểu thức thông thường:
import re for noMatch in db.inventory.find[ { "item": { "$not": re.compile["^p.*"] } } ]: print noMatch
Tài liệu về nhà → Hướng dẫn sử dụng MongoDB → MongoDB Manual
db.inventory.find[ { item: { $not: { $regex: "^p.*" } } } ] db.inventory.find[ { item: { $not: { $regex: /^p.*/ } } } ]
7sYntax: db.inventory.insertMany[ [ { "item": "nuts", "quantity": 30, "carrier": { "name": "Shipit", "fee": 3 } }, { "item": "bolts", "quantity": 50, "carrier": { "name": "Shipit", "fee": 4 } }, { "item": "washers", "quantity": 10, "carrier": { "name": "Shipit", "fee": 1 } } ] ]
1Syntax:
db.inventory.insertMany[ [ { "item": "nuts", "quantity": 30, "carrier": { "name": "Shipit", "fee": 3 } }, { "item": "bolts", "quantity": 50, "carrier": { "name": "Shipit", "fee": 4 } }, { "item": "washers", "quantity": 10, "carrier": { "name": "Shipit", "fee": 1 } } ] ]
1db.inventory.find[ { item: { $not: /^p.*/ } } ] db.inventory.find[ { item: { $not: /^p.*/ } } ]
7 chọn các tài liệu trong đó giá trị của db.inventory.find[ { item: { $not: { $regex: "^p.*" } } } ] db.inventory.find[ { item: { $not: { $regex: /^p.*/ } } } ]
5 không bằng với
4 được chỉ định. Điều này bao gồm các tài liệu không chứa db.inventory.insertMany[ [ { "item": "nuts", "quantity": 30, "carrier": { "name": "Shipit", "fee": 3 } }, { "item": "bolts", "quantity": 50, "carrier": { "name": "Shipit", "fee": 4 } }, { "item": "washers", "quantity": 10, "carrier": { "name": "Shipit", "fee": 1 } } ] ]
5. selects the documents where the value of the
db.inventory.find[ { item: { $not: /^p.*/ } } ]
5 is not equal to the specified db.inventory.insertMany[ [ { "item": "nuts", "quantity": 30, "carrier": { "name": "Shipit", "fee": 3 } }, { "item": "bolts", "quantity": 50, "carrier": { "name": "Shipit", "fee": 4 } }, { "item": "washers", "quantity": 10, "carrier": { "name": "Shipit", "fee": 1 } } ] ]
4. This includes documents that do not contain the db.inventory.find[ { item: { $not: /^p.*/ } } ]
5.Để so sánh các giá trị loại BSON khác nhau, hãy xem thứ tự so sánh BSON được chỉ định.
Các ví dụ sau sử dụng bộ sưu tập
db.inventory.find[ { item: { $not: /^p.*/ } } ]
6. Tạo bộ sưu tập:db.inventory.insertMany[ [ { "item": "nuts", "quantity": 30, "carrier": { "name": "Shipit", "fee": 3 } }, { "item": "bolts", "quantity": 50, "carrier": { "name": "Shipit", "fee": 4 } }, { "item": "washers", "quantity": 10, "carrier": { "name": "Shipit", "fee": 1 } } ] ]
Chọn tất cả các tài liệu trong bộ sưu tập
db.inventory.find[ { item: { $not: /^p.*/ } } ]
6 trong đó db.inventory.insertMany[ [ { "item": "nuts", "quantity": 30, "carrier": { "name": "Shipit", "fee": 3 } }, { "item": "bolts", "quantity": 50, "carrier": { "name": "Shipit", "fee": 4 } }, { "item": "washers", "quantity": 10, "carrier": { "name": "Shipit", "fee": 1 } } ] ]
8 không bằng db.inventory.insertMany[ [ { "item": "nuts", "quantity": 30, "carrier": { "name": "Shipit", "fee": 3 } }, { "item": "bolts", "quantity": 50, "carrier": { "name": "Shipit", "fee": 4 } }, { "item": "washers", "quantity": 10, "carrier": { "name": "Shipit", "fee": 1 } } ] ]
9:db.inventory.find[ { quantity: { $ne: 20 } } ]
Truy vấn cũng sẽ chọn các tài liệu không có trường
db.inventory.insertMany[ [ { "item": "nuts", "quantity": 30, "carrier": { "name": "Shipit", "fee": 3 } }, { "item": "bolts", "quantity": 50, "carrier": { "name": "Shipit", "fee": 4 } }, { "item": "washers", "quantity": 10, "carrier": { "name": "Shipit", "fee": 1 } } ] ]
8.Ví dụ đầu ra:
{ _id: ObjectId["61ba667dfe687fce2f042420"], item: 'nuts', quantity: 30, carrier: { name: 'Shipit', fee: 3 } }, { _id: ObjectId["61ba667dfe687fce2f042421"], item: 'bolts', quantity: 50, carrier: { name: 'Shipit', fee: 4 } }, { _id: ObjectId["61ba667dfe687fce2f042422"], item: 'washers', quantity: 10, carrier: { name: 'Shipit', fee: 1 } }
Ví dụ sau đây đặt trường
db.inventory.find[ { item: { $not: /^p.*/ } } ]
7 dựa trên so sánh db.inventory.find[ { item: { $not: { $regex: "^p.*" } } } ] db.inventory.find[ { item: { $not: { $regex: /^p.*/ } } } ]
7 với trường trong một tài liệu nhúng.db.inventory.find[ { item: { $not: { $regex: "^p.*" } } } ] db.inventory.find[ { item: { $not: { $regex: /^p.*/ } } } ]
7 comparison against a field in an embedded document.db.inventory.updateMany[ { "carrier.fee": { $ne: 1 } }, { $set: { "price": 9.99 } } ]
Ví dụ đầu ra:
{ _id: ObjectId["61ba66e2fe687fce2f042423"], item: 'nuts', quantity: 30, carrier: { name: 'Shipit', fee: 3 }, price: 9.99 }, { _id: ObjectId["61ba66e2fe687fce2f042424"], item: 'bolts', quantity: 50, carrier: { name: 'Shipit', fee: 4 }, price: 9.99 }, { _id: ObjectId["61ba66e2fe687fce2f042425"], item: 'washers', quantity: 10, carrier: { name: 'Shipit', fee: 1 } }
Ví dụ sau đây đặt trường
db.inventory.find[ { item: { $not: /^p.*/ } } ]
7 dựa trên so sánh db.inventory.find[ { item: { $not: { $regex: "^p.*" } } } ] db.inventory.find[ { item: { $not: { $regex: /^p.*/ } } } ]
7 với trường trong một tài liệu nhúng.Hoạt động
db.inventory.find[ { quantity: { $ne: 20 } } ]
3 này tìm kiếm một tài liệu nhúng, db.inventory.find[ { quantity: { $ne: 20 } } ]
4, với một trường con có tên db.inventory.find[ { quantity: { $ne: 20 } } ]
5. Nó đặt db.inventory.find[ { quantity: { $ne: 20 } } ]
6 trong mỗi tài liệu trong đó db.inventory.find[ { quantity: { $ne: 20 } } ]
5 có giá trị không bằng 1 hoặc trường hợp con db.inventory.find[ { quantity: { $ne: 20 } } ]
5 không tồn tại.db.inventory.find[ { item: { $not: { $regex: "^p.*" } } } ] db.inventory.find[ { item: { $not: { $regex: /^p.*/ } } } ]
7 is not very selective since it often matches a large portion of the index. As a result, in many cases, a db.inventory.find[ { item: { $not: { $regex: "^p.*" } } } ] db.inventory.find[ { item: { $not: { $regex: /^p.*/ } } } ]
7 query with an index may perform no better than a
db.inventory.find[ { item: { $not: { $regex: "^p.*" } } } ] db.inventory.find[ { item: { $not: { $regex: /^p.*/ } } } ]
7 query that must scan all documents in a collection. See also Query Selectivity.Toán tử bất bình đẳng db.inventory.find[ { item: { $not: { $regex: "^p.*" } } } ] db.inventory.find[ { item: { $not: { $regex: /^p.*/ } } } ]
7 không chọn lọc lắm vì nó thường khớp với một phần lớn của chỉ số. Do đó, trong nhiều trường hợp, truy vấn db.inventory.find[ { item: { $not: { $regex: "^p.*" } } } ] db.inventory.find[ { item: { $not: { $regex: /^p.*/ } } } ]
7 có chỉ mục có thể không thực hiện không tốt hơn truy vấn db.inventory.find[ { item: { $not: { $regex: "^p.*" } } } ] db.inventory.find[ { item: { $not: { $regex: /^p.*/ } } } ]
7 phải quét tất cả các tài liệu trong một bộ sưu tập. Xem thêm Truy vấn chọn lọc.
db.inventory.find[ { item: { $not: { $regex: "^p.*" } } } ] db.inventory.find[ { item: { $not: { $regex: /^p.*/ } } } ]
db.inventory.find[ { item: { $not: { $regex: "^p.*" } } } ] db.inventory.find[ { item: { $not: { $regex: /^p.*/ } } } ]
db.inventory.find[ { item: { $not: { $regex: "^p.*" } } } ] db.inventory.find[ { item: { $not: { $regex: /^p.*/ } } } ]