Tài liệu về nhà → Hướng dẫn sử dụng MongoDB → MongoDB Manual
db.inventory.find[ { $and: [ { price: { $ne: 1.99 } }, { price: { $exists: true } } ] } ]
5SYNTAX: db.inventory.find[ { $and: [ { price: { $ne: 1.99 } }, { price: { $exists: true } } ] } ]
6Syntax:
db.inventory.find[ { $and: [ { price: { $ne: 1.99 } }, { price: { $exists: true } } ] } ]
6
db.inventory.find[ { $and: [ { price: { $ne: 1.99 } }, { price: { $exists: true } } ] } ] db.inventory.find[ { $and: [ { price: { $ne: 1.99 } }, { price: { $exists: true } } ] } ] db.inventory.find[ { $and: [ { price: { $ne: 1.99 } }, { price: { $exists: true } } ] } ] db.inventory.find[ { price: { $ne: 1.99, $exists: true } } ]
5 thực hiện hoạt động logic
8 trên một mảng gồm một hoặc nhiều biểu thức [
9,
0, v.v.] và chọn các tài liệu đáp ứng tất cả các biểu thức. performs a logical
db.inventory.find[ { $and: [ { price: { $ne: 1.99 } }, { price: { $exists: true } } ] } ]
8 operation on an array of one or more expressions [db.inventory.find[ { $and: [ { price: { $ne: 1.99 } }, { price: { $exists: true } } ] } ]
9, db.inventory.find[ { price: { $ne: 1.99, $exists: true } } ]
0, and so on] and selects the documents that satisfy all the expressions.Ghi chú
MongoDB cung cấp một hoạt động
db.inventory.find[ { $and: [ { price: { $ne: 1.99 } }, { price: { $exists: true } } ] } ]
8 ngầm khi chỉ định danh sách các biểu thức được phân tách bằng dấu phẩy.
Để cho phép công cụ truy vấn tối ưu hóa các truy vấn,
db.inventory.find[ { $and: [ { price: { $ne: 1.99 } }, { price: { $exists: true } } ] } ]
5 xử lý các lỗi như sau:Nếu bất kỳ biểu thức nào được cung cấp cho
5 sẽ gây ra lỗi khi được đánh giá một mình,db.inventory.find[ { $and: [ { price: { $ne: 1.99 } }, { price: { $exists: true } } ] } ]
5 chứa biểu thức có thể gây ra lỗi nhưng không được đảm bảo lỗi.db.inventory.find[ { $and: [ { price: { $ne: 1.99 } }, { price: { $exists: true } } ] } ]
Một biểu thức được cung cấp sau biểu thức đầu tiên được cung cấp cho
5 có thể gây ra lỗi ngay cả khi biểu thức đầu tiên đánh giá làdb.inventory.find[ { $and: [ { price: { $ne: 1.99 } }, { price: { $exists: true } } ] } ]
6.db.inventory.find[ { price: { $ne: 1.99, $exists: true } } ]
Ví dụ: truy vấn sau luôn tạo ra lỗi nếu
db.inventory.find[ { price: { $ne: 1.99, $exists: true } } ]
7 là db.inventory.find[ { price: { $ne: 1.99, $exists: true } } ]
8:db.example.find[ { $expr: { $eq: [ { $divide: [ 1, "$x" ] }, 3 ] } } ]
Truy vấn sau, chứa nhiều biểu thức được cung cấp cho
db.inventory.find[ { $and: [ { price: { $ne: 1.99 } }, { price: { $exists: true } } ] } ]
5, có thể tạo ra lỗi nếu có bất kỳ tài liệu nào trong đó db.inventory.find[ { price: { $ne: 1.99, $exists: true } } ]
7 là db.inventory.find[ { price: { $ne: 1.99, $exists: true } } ]
8:db.example.find[ { $and: [ { x: { $ne: 0 } }, { $expr: { $eq: [ { $divide: [ 1, "$x" ] }, 3 ] } } ] } ]
Xem xét truy vấn này:
db.inventory.find[ { $and: [ { price: { $ne: 1.99 } }, { price: { $exists: true } } ] } ]
Truy vấn chọn tất cả các tài liệu trong bộ sưu tập
db.inventory.find[ { $and: [ { $or: [ { qty: { $lt : 10 } }, { qty : { $gt: 50 } } ] }, { $or: [ { sale: true }, { price : { $lt : 5 } } ] } ] } ]
2 trong đó:Giá trị trường
3 không bằngdb.inventory.find[ { $and: [ { $or: [ { qty: { $lt : 10 } }, { qty : { $gt: 50 } } ] }, { $or: [ { sale: true }, { price : { $lt : 5 } } ] } ] } ]
4 vàanddb.inventory.find[ { $and: [ { $or: [ { qty: { $lt : 10 } }, { qty : { $gt: 50 } } ] }, { $or: [ { sale: true }, { price : { $lt : 5 } } ] } ] } ] Trường
3 tồn tại.db.inventory.find[ { $and: [ { $or: [ { qty: { $lt : 10 } }, { qty : { $gt: 50 } } ] }, { $or: [ { sale: true }, { price : { $lt : 5 } } ] } ] } ]
Truy vấn có thể được viết lại với thao tác
db.inventory.find[ { $and: [ { price: { $ne: 1.99 } }, { price: { $exists: true } } ] } ]
8 ngầm kết hợp các biểu thức toán tử cho trường db.inventory.find[ { $and: [ { $or: [ { qty: { $lt : 10 } }, { qty : { $gt: 50 } } ] }, { $or: [ { sale: true }, { price : { $lt : 5 } } ] } ] } ]
3:db.inventory.find[ { price: { $ne: 1.99, $exists: true } } ]
Xem xét truy vấn này:
db.inventory.find[ { $and: [ { $or: [ { qty: { $lt : 10 } }, { qty : { $gt: 50 } } ] }, { $or: [ { sale: true }, { price : { $lt : 5 } } ] } ] } ]
Truy vấn chọn tất cả các tài liệu trong bộ sưu tập
db.inventory.find[ { $and: [ { $or: [ { qty: { $lt : 10 } }, { qty : { $gt: 50 } } ] }, { $or: [ { sale: true }, { price : { $lt : 5 } } ] } ] } ]
2 trong đó:Giá trị trường
3 không bằngdb.inventory.find[ { $and: [ { $or: [ { qty: { $lt : 10 } }, { qty : { $gt: 50 } } ] }, { $or: [ { sale: true }, { price : { $lt : 5 } } ] } ] } ]
4 vàanddb.inventory.find[ { $and: [ { $or: [ { qty: { $lt : 10 } }, { qty : { $gt: 50 } } ] }, { $or: [ { sale: true }, { price : { $lt : 5 } } ] } ] } ] Trường
3 tồn tại.or thedb.inventory.find[ { $and: [ { $or: [ { qty: { $lt : 10 } }, { qty : { $gt: 50 } } ] }, { $or: [ { sale: true }, { price : { $lt : 5 } } ] } ] } ]
3 field value is less thandb.inventory.find[ { $and: [ { $or: [ { qty: { $lt : 10 } }, { qty : { $gt: 50 } } ] }, { $or: [ { sale: true }, { price : { $lt : 5 } } ] } ] } ]
4.{ : { $eq: } }
Truy vấn có thể được viết lại với thao tác
db.inventory.find[ { $and: [ { price: { $ne: 1.99 } }, { price: { $exists: true } } ] } ]
8 ngầm kết hợp các biểu thức toán tử cho trường db.inventory.find[ { $and: [ { $or: [ { qty: { $lt : 10 } }, { qty : { $gt: 50 } } ] }, { $or: [ { sale: true }, { price : { $lt : 5 } } ] } ] } ]
3:Truy vấn chọn tất cả các tài liệu trong đó:
trả về kết quả sau: → MongoDB Manual
Specifies equality condition.
The { : { $eq: } }
7 operator matches documents where the value of a field equals the specified value.
{ : { $eq: } }
Một truy vấn rõ ràng sử dụng
{ : { $eq: } }
7 và biểu thức chính quy sẽ chỉ khớp với một đối tượng cũng là một biểu hiện thông thường. Truy vấn ví dụ sẽ không trả về bất cứ điều gì vì các giá trị trong trường { _id: 2, item: { name: "cd", code: "123" }, qty: 20, tags: [ "B" ] } { _id: 5, item: { name: "mn", code: "000" }, qty: 20, tags: [ [ "A", "B" ], "C" ] }
2 là chuỗi.{ : { $eq: } }
7 operator is equivalent to using the form { _id: 1, item: { name: "ab", code: "123" }, qty: 15, tags: [ "A", "B", "C" ] } { _id: 2, item: { name: "cd", code: "123" }, qty: 20, tags: [ "B" ] } { _id: 3, item: { name: "ij", code: "456" }, qty: 25, tags: [ "A", "B" ] } { _id: 4, item: { name: "xy", code: "456" }, qty: 30, tags: [ "B", "A" ] } { _id: 5, item: { name: "mn", code: "000" }, qty: 20, tags: [ [ "A", "B" ], "C" ] }
0 except when the { _id: 1, item: { name: "ab", code: "123" }, qty: 15, tags: [ "A", "B", "C" ] } { _id: 2, item: { name: "cd", code: "123" }, qty: 20, tags: [ "B" ] } { _id: 3, item: { name: "ij", code: "456" }, qty: 25, tags: [ "A", "B" ] } { _id: 4, item: { name: "xy", code: "456" }, qty: 30, tags: [ "B", "A" ] } { _id: 5, item: { name: "mn", code: "000" }, qty: 20, tags: [ [ "A", "B" ], "C" ] }
1 is a regular expression.
See below for examples.Biểu thức thường xuyên phù hợp
Một truy vấn có trận đấu ngầm với biểu thức chính quy tương đương với việc tạo một truy vấn với toán tử
db.inventory.find[ { qty: { $eq: 20 } } ]
2. Cả hai truy vấn này:trả về kết quả tương tự:Equals an Array Value.
Biểu thức
{ _id: 1, item: { name: "ab", code: "123" }, qty: 15, tags: [ "A", "B", "C" ] } { _id: 2, item: { name: "cd", code: "123" }, qty: 20, tags: [ "B" ] } { _id: 3, item: { name: "ij", code: "456" }, qty: 25, tags: [ "A", "B" ] } { _id: 4, item: { name: "xy", code: "456" }, qty: 30, tags: [ "B", "A" ] } { _id: 5, item: { name: "mn", code: "000" }, qty: 20, tags: [ [ "A", "B" ], "C" ] }
0 ngầm chỉ định một trận đấu trên { _id: 1, item: { name: "ab", code: "123" }, qty: 15, tags: [ "A", "B", "C" ] } { _id: 2, item: { name: "cd", code: "123" }, qty: 20, tags: [ "B" ] } { _id: 3, item: { name: "ij", code: "456" }, qty: 25, tags: [ "A", "B" ] } { _id: 4, item: { name: "xy", code: "456" }, qty: 30, tags: [ "B", "A" ] } { _id: 5, item: { name: "mn", code: "000" }, qty: 20, tags: [ [ "A", "B" ], "C" ] }
1. MongoDB dịch phù hợp ngầm thành một hình thức rõ ràng hơn.Khi
{ _id: 1, item: { name: "ab", code: "123" }, qty: 15, tags: [ "A", "B", "C" ] } { _id: 2, item: { name: "cd", code: "123" }, qty: 20, tags: [ "B" ] } { _id: 3, item: { name: "ij", code: "456" }, qty: 25, tags: [ "A", "B" ] } { _id: 4, item: { name: "xy", code: "456" }, qty: 30, tags: [ "B", "A" ] } { _id: 5, item: { name: "mn", code: "000" }, qty: 20, tags: [ [ "A", "B" ], "C" ] }
1 được sửa, giống như một chuỗi cụ thể, biểu thức tương đương với việc sử dụng toán tử { : { $eq: } }
7 db.inventory.find[ { qty: { $eq: 20 } } ]
0.Nếu
{ _id: 1, item: { name: "ab", code: "123" }, qty: 15, tags: [ "A", "B", "C" ] } { _id: 2, item: { name: "cd", code: "123" }, qty: 20, tags: [ "B" ] } { _id: 3, item: { name: "ij", code: "456" }, qty: 25, tags: [ "A", "B" ] } { _id: 4, item: { name: "xy", code: "456" }, qty: 30, tags: [ "B", "A" ] } { _id: 5, item: { name: "mn", code: "000" }, qty: 20, tags: [ [ "A", "B" ], "C" ] }
1 là một biểu thức chính quy, câu lệnh được mở rộng bằng toán tử db.inventory.find[ { qty: { $eq: 20 } } ]
2 db.inventory.find[ { qty: { $eq: 20 } } ]
3.Để biết ví dụ minh họa hành vi này, xem hành vi phù hợp của Regex.Regex Match Behaviour.
Luôn luôn sử dụng biểu mẫu rõ ràng
db.inventory.find[ { qty: { $eq: 20 } } ]
0 với đầu vào do người dùng cung cấp để tránh các vấn đề với các truy vấn được hình thành độc hại.Các ví dụ sau truy vấn chống lại bộ sưu tập
db.inventory.find[ { $and: [ { $or: [ { qty: { $lt : 10 } }, { qty : { $gt: 50 } } ] }, { $or: [ { sale: true }, { price : { $lt : 5 } } ] } ] } ]
2 với các tài liệu sau:{ _id: 1, item: { name: "ab", code: "123" }, qty: 15, tags: [ "A", "B", "C" ] } { _id: 2, item: { name: "cd", code: "123" }, qty: 20, tags: [ "B" ] } { _id: 3, item: { name: "ij", code: "456" }, qty: 25, tags: [ "A", "B" ] } { _id: 4, item: { name: "xy", code: "456" }, qty: 30, tags: [ "B", "A" ] } { _id: 5, item: { name: "mn", code: "000" }, qty: 20, tags: [ [ "A", "B" ], "C" ] }
Ví dụ sau truy vấn bộ sưu tập
db.inventory.find[ { $and: [ { $or: [ { qty: { $lt : 10 } }, { qty : { $gt: 50 } } ] }, { $or: [ { sale: true }, { price : { $lt : 5 } } ] } ] } ]
2 Để chọn tất cả các tài liệu trong đó giá trị của trường db.inventory.find[ { $and: [ { $or: [ { qty: { $lt : 10 } }, { qty : { $gt: 50 } } ] }, { $or: [ { sale: true }, { price : { $lt : 5 } } ] } ] } ]
8 bằng db.inventory.find[ { qty: { $eq: 20 } } ]
8:db.inventory.find[ { qty: { $eq: 20 } } ]
Truy vấn tương đương với:
db.inventory.find[ { qty: 20 } ]
Cả hai truy vấn đều khớp với các tài liệu sau:
{ _id: 2, item: { name: "cd", code: "123" }, qty: 20, tags: [ "B" ] } { _id: 5, item: { name: "mn", code: "000" }, qty: 20, tags: [ [ "A", "B" ], "C" ] }
Ví dụ sau truy vấn bộ sưu tập
db.inventory.find[ { $and: [ { $or: [ { qty: { $lt : 10 } }, { qty : { $gt: 50 } } ] }, { $or: [ { sale: true }, { price : { $lt : 5 } } ] } ] } ]
2 để chọn tất cả các tài liệu trong đó giá trị của trường db.inventory.find[ { qty: 20 } ]
0 trong tài liệu db.inventory.find[ { qty: 20 } ]
1 bằng db.inventory.find[ { qty: 20 } ]
2. Để chỉ định một điều kiện trên một trường trong tài liệu nhúng, hãy sử dụng ký hiệu dấu chấm.db.example.find[ { $and: [ { x: { $ne: 0 } }, { $expr: { $eq: [ { $divide: [ 1, "$x" ] }, 3 ] } } ] } ]
0Truy vấn tương đương với:
db.example.find[ { $and: [ { x: { $ne: 0 } }, { $expr: { $eq: [ { $divide: [ 1, "$x" ] }, 3 ] } } ] } ]
1Cả hai truy vấn đều khớp với tài liệu sau:
db.example.find[ { $and: [ { x: { $ne: 0 } }, { $expr: { $eq: [ { $divide: [ 1, "$x" ] }, 3 ] } } ] } ]
2Mẹo
Xem thêm:
Ví dụ sau truy vấn bộ sưu tập
db.inventory.find[ { $and: [ { $or: [ { qty: { $lt : 10 } }, { qty : { $gt: 50 } } ] }, { $or: [ { sale: true }, { price : { $lt : 5 } } ] } ] } ]
2 Để chọn tất cả các tài liệu trong đó mảng db.inventory.find[ { qty: 20 } ]
4 chứa một phần tử có giá trị db.inventory.find[ { qty: 20 } ]
5 [1]:db.example.find[ { $and: [ { x: { $ne: 0 } }, { $expr: { $eq: [ { $divide: [ 1, "$x" ] }, 3 ] } } ] } ]
3Truy vấn tương đương với:
db.example.find[ { $and: [ { x: { $ne: 0 } }, { $expr: { $eq: [ { $divide: [ 1, "$x" ] }, 3 ] } } ] } ]
4Cả hai truy vấn đều khớp với các tài liệu sau:
db.example.find[ { $and: [ { x: { $ne: 0 } }, { $expr: { $eq: [ { $divide: [ 1, "$x" ] }, 3 ] } } ] } ]
5Mẹo
Xem thêm:
db.example.find[ { $and: [ { x: { $ne: 0 } }, { $expr: { $eq: [ { $divide: [ 1, "$x" ] }, 3 ] } } ] } ]
6Truy vấn tương đương với:
db.example.find[ { $and: [ { x: { $ne: 0 } }, { $expr: { $eq: [ { $divide: [ 1, "$x" ] }, 3 ] } } ] } ]
7Cả hai truy vấn đều khớp với các tài liệu sau:
db.example.find[ { $and: [ { x: { $ne: 0 } }, { $expr: { $eq: [ { $divide: [ 1, "$x" ] }, 3 ] } } ] } ]
8Ví dụ sau truy vấn Bộ sưu tập
db.inventory.find[ { $and: [ { $or: [ { qty: { $lt : 10 } }, { qty : { $gt: 50 } } ] }, { $or: [ { sale: true }, { price : { $lt : 5 } } ] } ] } ]
2 Để chọn tất cả các tài liệu trong đó mảng db.inventory.find[ { qty: 20 } ]
4 bằng chính xác mảng được chỉ định hoặc mảng db.inventory.find[ { qty: 20 } ]
4 chứa một phần tử bằng với mảng db.inventory.find[ { qty: 20 } ]
9.db.example.find[ { $and: [ { x: { $ne: 0 } }, { $expr: { $eq: [ { $divide: [ 1, "$x" ] }, 3 ] } } ] } ]
9Các ví dụ sau đây chứng minh sự khác biệt về hành vi giữa việc kết hợp biểu thức ngầm và giải thích thông thường. Xem xét một bộ sưu tập với các tài liệu này:$ eq khớp trên một chuỗi
db.inventory.find[ { $and: [ { price: { $ne: 1.99 } }, { price: { $exists: true } } ] } ]
0Một chuỗi mở rộng để trả về các giá trị giống nhau cho dù phù hợp ngầm hay sử dụng rõ ràng
{ : { $eq: } }
7. Cả hai truy vấn này:db.inventory.find[ { $and: [ { price: { $ne: 1.99 } }, { price: { $exists: true } } ] } ]
1trả về kết quả sau:$ EQ phù hợp trên một biểu thức thông thường
db.inventory.find[ { $and: [ { price: { $ne: 1.99 } }, { price: { $exists: true } } ] } ]
2Một truy vấn rõ ràng sử dụng { : { $eq: } }
7 và biểu thức chính quy sẽ chỉ khớp với một đối tượng cũng là một biểu hiện thông thường. Truy vấn ví dụ sẽ không trả về bất cứ điều gì vì các giá trị trong trường { _id: 2, item: { name: "cd", code: "123" }, qty: 20, tags: [ "B" ] } { _id: 5, item: { name: "mn", code: "000" }, qty: 20, tags: [ [ "A", "B" ], "C" ] }
2 là chuỗi.Biểu thức thường xuyên phù hợp
db.inventory.find[ { $and: [ { price: { $ne: 1.99 } }, { price: { $exists: true } } ] } ]
3Một truy vấn có trận đấu ngầm với biểu thức chính quy tương đương với việc tạo một truy vấn với toán tử
db.inventory.find[ { qty: { $eq: 20 } } ]
2. Cả hai truy vấn này:db.inventory.find[ { $and: [ { price: { $ne: 1.99 } }, { price: { $exists: true } } ] } ]
4