- Tham khảo> >
- Toán tử> >
- Truy vấn và toán tử trình chiếu> >
- Các toán tử truy vấn so sánh> >
- $ne
________ 6¶
Cú pháp: {field: {$ne: value} }
$ne chọn các tài liệu trong đó giá trị của field không bằng với ____10 được chỉ định. Điều này bao gồm các tài liệu không chứa field.
Để 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.specified BSON comparison order.
Xem xét ví dụ sau:
db.inventory.find( { qty: { $ne: 20 } } )
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.update( { "carrier.state": { $ne: "NY" } }, { $set: { qty: 20 } } ) 2 trong đó giá trị trường db.inventory.update( { "carrier.state": { $ne: "NY" } }, { $set: { qty: 20 } } ) 3 không bằng db.inventory.update( { "carrier.state": { $ne: "NY" } }, { $set: { qty: 20 } } ) 4, bao gồm các tài liệu không chứa trường db.inventory.update( { "carrier.state": { $ne: "NY" } }, { $set: { qty: 20 } } ) 3.
Xem xét ví dụ sau sử dụng toán tử $ne với một trường trong tài liệu nhúng:
db.inventory.update( { "carrier.state": { $ne: "NY" } }, { $set: { qty: 20 } } )
Hoạt động db.inventory.update( { "carrier.state": { $ne: "NY" } }, { $set: { qty: 20 } } ) 7 này sẽ đặt giá trị trường db.inventory.update( { "carrier.state": { $ne: "NY" } }, { $set: { qty: 20 } } ) 3 trong các tài liệu có chứa tài liệu nhúng db.inventory.update( { "carrier.state": { $ne: "NY" } }, { $set: { qty: 20 } } ) 9 có giá trị trường
{ $ne: [ <expression1>, <expression2> ] }
0 không bằng với NY NY, hoặc trường{ $ne: [ <expression1>, <expression2> ] }
0 hoặc tài liệu nhúng db.inventory.update( { "carrier.state": { $ne: "NY" } }, { $set: { qty: 20 } } ) 9 không tồn tại.Toán tử bất bình đẳng $ne 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 $ne có chỉ mục có thể không thực hiện tốt hơn truy vấn $ne 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.Query Selectivity.
Tài liệu về nhà → Hướng dẫn sử dụng MongoDB → MongoDB Manual
Compares two values and returns:
{ $ne: [ <expression1>, <expression2> ] }
7 Khi các giá trị không tương đương.{ $ne: [ <expression1>, <expression2> ] }
8 Khi các giá trị tương đương.
$ne so sánh cả giá trị và loại, sử dụng thứ tự so sánh BSON được chỉ định cho các giá trị của các loại khác nhau.$ne compares both value and type, using the specified BSON comparison order for values of different types.
$ne có cú pháp sau: has the following syntax:
{ $ne: [ <expression1>, <expression2> ] }
Để biết thêm thông tin về biểu thức, xem biểu thức.
Xem xét bộ sưu tập
{ "_id" : 1, "item" : "abc1", description: "product 1", qty: 300 } |
{ "_id" : 2, "item" : "abc2", description: "product 2", qty: 200 } |
{ "_id" : 3, "item" : "xyz1", description: "product 3", qty: 250 } |
{ "_id" : 4, "item" : "VWZ1", description: "product 4", qty: 300 } |
{ "_id" : 5, "item" : "VWZ2", description: "product 5", qty: 180 } |
{ "_id" : 1, "item" : "abc1", description: "product 1", qty: 300 } |
{ "_id" : 2, "item" : "abc2", description: "product 2", qty: 200 } |
{ "_id" : 3, "item" : "xyz1", description: "product 3", qty: 250 } |
{ "_id" : 4, "item" : "VWZ1", description: "product 4", qty: 300 } |
{ "_id" : 5, "item" : "VWZ2", description: "product 5", qty: 180 } |
Hoạt động sau sử dụng toán tử $ne để xác định xem
{ "_id" : 1, "item" : "abc1", description: "product 1", qty: 300 } |
{ "_id" : 2, "item" : "abc2", description: "product 2", qty: 200 } |
{ "_id" : 3, "item" : "xyz1", description: "product 3", qty: 250 } |
{ "_id" : 4, "item" : "VWZ1", description: "product 4", qty: 300 } |
{ "_id" : 5, "item" : "VWZ2", description: "product 5", qty: 180 } |
{ "_id" : 1, "item" : "abc1", description: "product 1", qty: 300 } |
{ "_id" : 2, "item" : "abc2", description: "product 2", qty: 200 } |
{ "_id" : 3, "item" : "xyz1", description: "product 3", qty: 250 } |
{ "_id" : 4, "item" : "VWZ1", description: "product 4", qty: 300 } |
{ "_id" : 5, "item" : "VWZ2", description: "product 5", qty: 180 } |
{ "_id" : 1, "item" : "abc1", description: "product 1", qty: 300 } |
{ "_id" : 2, "item" : "abc2", description: "product 2", qty: 200 } |
{ "_id" : 3, "item" : "xyz1", description: "product 3", qty: 250 } |
{ "_id" : 4, "item" : "VWZ1", description: "product 4", qty: 300 } |
{ "_id" : 5, "item" : "VWZ2", description: "product 5", qty: 180 } |
{ "_id" : 1, "item" : "abc1", description: "product 1", qty: 300 } |
{ "_id" : 2, "item" : "abc2", description: "product 2", qty: 200 } |
{ "_id" : 3, "item" : "xyz1", description: "product 3", qty: 250 } |
{ "_id" : 4, "item" : "VWZ1", description: "product 4", qty: 300 } |
{ "_id" : 5, "item" : "VWZ2", description: "product 5", qty: 180 } |
db.inventory.aggregate( |
[ |
{ |
$project: |
{ |
item: 1, |
qty: 1, |
qtyNe250: { $ne: [ "$qty", 250 ] }, |
_id: 0 |
} |
} |
] |
) |
Hoạt động trả về các kết quả sau:
{ "item" : "abc1", "qty" : 300, "qtyNe250" : true } |
{ "item" : "abc2", "qty" : 200, "qtyNe250" : true } |
{ "item" : "xyz1", "qty" : 250, "qtyNe250" : false } |
{ "item" : "VWZ1", "qty" : 300, "qtyNe250" : true } |
{ "item" : "VWZ2", "qty" : 180, "qtyNe250" : true } |