{ item: 'Erasers', quantity: 15, tags: [ 'school', 'home' ] }, |
{ item: 'Books', quantity: 5, tags: [ 'school', 'storage', 'home' ] } |
0 Trình điều khiển { item: 'Erasers', quantity: 15, tags: [ 'school', 'home' ] }, |
{ item: 'Books', quantity: 5, tags: [ 'school', 'storage', 'home' ] } |
0 chọn các tài liệu trong đó giá trị của một trường bằng bất kỳ giá trị nào trong mảng được chỉ định. Để chỉ định biểu thức { item: 'Erasers', quantity: 15, tags: [ 'school', 'home' ] }, |
{ item: 'Books', quantity: 5, tags: [ 'school', 'storage', 'home' ] } |
0, hãy sử dụng nguyên mẫu sau:The
{ item: 'Erasers', quantity: 15, tags: [ 'school', 'home' ] }, |
{ item: 'Books', quantity: 5, tags: [ 'school', 'storage', 'home' ] } |
0 operator selects the documents where the value of a field equals any value in the specified array. To specify an { item: 'Erasers', quantity: 15, tags: [ 'school', 'home' ] }, |
{ item: 'Books', quantity: 5, tags: [ 'school', 'storage', 'home' ] } |
0 expression, use the following prototype:
{ field: { $in: [, , ... ] } }
Để 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.
Nếu
{ item: 'Erasers', quantity: 15, tags: [ 'school', 'home' ] }, |
{ item: 'Books', quantity: 5, tags: [ 'school', 'storage', 'home' ] } |
3 giữ một mảng, thì toán tử { item: 'Erasers', quantity: 15, tags: [ 'school', 'home' ] }, |
{ item: 'Books', quantity: 5, tags: [ 'school', 'storage', 'home' ] } |
0 chọn các tài liệu có { item: 'Erasers', quantity: 15, tags: [ 'school', 'home' ] }, |
{ item: 'Books', quantity: 5, tags: [ 'school', 'storage', 'home' ] } |
3 giữ một mảng chứa ít nhất một phần tử phù hợp với giá trị trong mảng được chỉ định [ví dụ: { item: 'Erasers', quantity: 15, tags: [ 'school', 'home' ] }, |
{ item: 'Books', quantity: 5, tags: [ 'school', 'storage', 'home' ] } |
6, { item: 'Erasers', quantity: 15, tags: [ 'school', 'home' ] }, |
{ item: 'Books', quantity: 5, tags: [ 'school', 'storage', 'home' ] } |
7, v.v.].{ item: 'Erasers', quantity: 15, tags: [ 'school', 'home' ] }, |
{ item: 'Books', quantity: 5, tags: [ 'school', 'storage', 'home' ] } |
0 operator selects the documents whose { item: 'Erasers', quantity: 15, tags: [ 'school', 'home' ] }, |
{ item: 'Books', quantity: 5, tags: [ 'school', 'storage', 'home' ] } |
3 holds an array that contains at least one element that matches a value in the
specified array [for example, { item: 'Erasers', quantity: 15, tags: [ 'school', 'home' ] }, |
{ item: 'Books', quantity: 5, tags: [ 'school', 'storage', 'home' ] } |
6, { item: 'Erasers', quantity: 15, tags: [ 'school', 'home' ] }, |
{ item: 'Books', quantity: 5, tags: [ 'school', 'storage', 'home' ] } |
7, and so on].Toán tử
{ item: 'Erasers', quantity: 15, tags: [ 'school', 'home' ] }, |
{ item: 'Books', quantity: 5, tags: [ 'school', 'storage', 'home' ] } |
0 so sánh từng tham số với từng tài liệu trong bộ sưu tập, có thể dẫn đến các vấn đề về hiệu suất. Để cải thiện hiệu suất:{ item: 'Erasers', quantity: 15, tags: [ 'school', 'home' ] }, |
{ item: 'Books', quantity: 5, tags: [ 'school', 'storage', 'home' ] } |
0 operator compares each parameter to each document in the collection, which can lead to performance issues. To improve performance:Bạn nên giới hạn số lượng tham số được chuyển cho toán tử
{ item: 'Erasers', quantity: 15, tags: [ 'school', 'home' ] }, |
{ item: 'Books', quantity: 5, tags: [ 'school', 'storage', 'home' ] } |
0 đến hàng chục giá trị. Sử dụng hàng trăm tham số trở lên có thể tác động tiêu cực đến hiệu suất truy vấn.{ item: 'Erasers', quantity: 15, tags: [ 'school', 'home' ] }, |
{ item: 'Books', quantity: 5, tags: [ 'school', 'storage', 'home' ] } |
0 operator to tens of values. Using hundreds of parameters or more can negatively impact query performance.Tạo một chỉ mục trên
{ item: 'Erasers', quantity: 15, tags: [ 'school', 'home' ] }, |
{ item: 'Books', quantity: 5, tags: [ 'school', 'storage', 'home' ] } |
3 bạn muốn truy vấn.
Ghi chú
Tạo bộ sưu tập
db.inventory.updateMany[ |
{ tags: { $in: [ "home", "school" ] } }, |
{ $set: { exclude: false } } |
] |
1:db.inventory.insertMany[ [ |
{ "item": "Pens", "quantity": 350, "tags": [ "school", "office" ] }, |
{ "item": "Erasers", "quantity": 15, "tags": [ "school", "home" ] }, |
{ "item": "Maps", "tags": [ "office", "storage" ] }, |
{ "item": "Books", "quantity": 5, "tags": [ "school", "storage", "home" ] } |
] ] |
Xem xét ví dụ sau:
db.inventory.find[ { quantity: { $in: [ 5, 15 ] } }, { _id: 0 } ]
Truy vấn này chọn tất cả các tài liệu trong bộ sưu tập
db.inventory.updateMany[ |
{ tags: { $in: [ "home", "school" ] } }, |
{ $set: { exclude: false } } |
] |
1 trong đó giá trị của trường db.inventory.updateMany[ |
{ tags: { $in: [ "home", "school" ] } }, |
{ $set: { exclude: false } } |
] |
3 là 5 hoặc 15.{ item: 'Erasers', quantity: 15, tags: [ 'school', 'home' ] }, |
{ item: 'Books', quantity: 5, tags: [ 'school', 'storage', 'home' ] } |
Mặc dù bạn có thể viết truy vấn này bằng toán tử
db.inventory.updateMany[ |
{ tags: { $in: [ "home", "school" ] } }, |
{ $set: { exclude: false } } |
] |
4, hãy sử dụng toán tử { item: 'Erasers', quantity: 15, tags: [ 'school', 'home' ] }, |
{ item: 'Books', quantity: 5, tags: [ 'school', 'storage', 'home' ] } |
0 thay vì toán tử db.inventory.updateMany[ |
{ tags: { $in: [ "home", "school" ] } }, |
{ $set: { exclude: false } } |
] |
4 khi thực hiện kiểm tra bình đẳng trên cùng một trường.{ item: 'Erasers', quantity: 15, tags: [ 'school', 'home' ] }, |
{ item: 'Books', quantity: 5, tags: [ 'school', 'storage', 'home' ] } |
0 operator rather
than the db.inventory.updateMany[ |
{ tags: { $in: [ "home", "school" ] } }, |
{ $set: { exclude: false } } |
] |
4 operator when performing equality checks on the same field.
Hoạt động
db.inventory.updateMany[ |
{ tags: { $in: [ "home", "school" ] } }, |
{ $set: { exclude: false } } |
] |
7 sau đây đặt trường db.inventory.updateMany[ |
{ tags: { $in: [ "home", "school" ] } }, |
{ $set: { exclude: false } } |
] |
8 thành db.inventory.updateMany[ |
{ tags: { $in: [ "home", "school" ] } }, |
{ $set: { exclude: false } } |
] |
9 khi mảng tags
có ít nhất một phần tử khớp với { |
item: 'Pens', |
quantity: 350, |
tags: [ 'school', 'office' ], |
exclude: false |
}, |
{ |
item: 'Erasers', |
quantity: 15, |
tags: [ 'school', 'home' ], |
exclude: false |
}, |
{ |
item: 'Maps', |
tags: [ 'office', 'storage' ] |
}, |
{ |
item: 'Books', |
quantity: 5, |
tags: [ 'school', 'storage', 'home' ], |
exclude: false |
} |
1 hoặc { |
item: 'Pens', |
quantity: 350, |
tags: [ 'school', 'office' ], |
exclude: false |
}, |
{ |
item: 'Erasers', |
quantity: 15, |
tags: [ 'school', 'home' ], |
exclude: false |
}, |
{ |
item: 'Maps', |
tags: [ 'office', 'storage' ] |
}, |
{ |
item: 'Books', |
quantity: 5, |
tags: [ 'school', 'storage', 'home' ], |
exclude: false |
} |
2.db.inventory.updateMany[ |
{ tags: { $in: [ "home", "school" ] } }, |
{ $set: { exclude: false } } |
] |
Ví dụ đầu ra:
{ |
item: 'Pens', |
quantity: 350, |
tags: [ 'school', 'office' ], |
exclude: false |
}, |
{ |
item: 'Erasers', |
quantity: 15, |
tags: [ 'school', 'home' ], |
exclude: false |
}, |
{ |
item: 'Maps', |
tags: [ 'office', 'storage' ] |
}, |
{ |
item: 'Books', |
quantity: 5, |
tags: [ 'school', 'storage', 'home' ], |
exclude: false |
} |
Để biết các ví dụ bổ sung trong các mảng truy vấn, xem:
Để biết các ví dụ bổ sung trong truy vấn, xem:
Toán tử
{ item: 'Erasers', quantity: 15, tags: [ 'school', 'home' ] }, |
{ item: 'Books', quantity: 5, tags: [ 'school', 'storage', 'home' ] } |
0 có thể chỉ định các giá trị phù hợp bằng cách sử dụng các biểu thức thông thường của mẫu { |
item: 'Pens', |
quantity: 350, |
tags: [ 'school', 'office' ], |
exclude: false |
}, |
{ |
item: 'Erasers', |
quantity: 15, |
tags: [ 'school', 'home' ], |
exclude: false |
}, |
{ |
item: 'Maps', |
tags: [ 'office', 'storage' ] |
}, |
{ |
item: 'Books', |
quantity: 5, |
tags: [ 'school', 'storage', 'home' ], |
exclude: false |
} |
4. Bạn không thể sử dụng các biểu thức toán tử { |
item: 'Pens', |
quantity: 350, |
tags: [ 'school', 'office' ], |
exclude: false |
}, |
{ |
item: 'Erasers', |
quantity: 15, |
tags: [ 'school', 'home' ], |
exclude: false |
}, |
{ |
item: 'Maps', |
tags: [ 'office', 'storage' ] |
}, |
{ |
item: 'Books', |
quantity: 5, |
tags: [ 'school', 'storage', 'home' ], |
exclude: false |
} |
5 bên trong { item: 'Erasers', quantity: 15, tags: [ 'school', 'home' ] }, |
{ item: 'Books', quantity: 5, tags: [ 'school', 'storage', 'home' ] } |
0{ item: 'Erasers', quantity: 15, tags: [ 'school', 'home' ] }, |
{ item: 'Books', quantity: 5, tags: [ 'school', 'storage', 'home' ] } |
0 operator can specify matching values using regular expressions of the form { |
item: 'Pens', |
quantity: 350, |
tags: [ 'school', 'office' ], |
exclude: false |
}, |
{ |
item: 'Erasers', |
quantity: 15, |
tags: [ 'school', 'home' ], |
exclude: false |
}, |
{ |
item: 'Maps', |
tags: [ 'office', 'storage' ] |
}, |
{ |
item: 'Books', |
quantity: 5, |
tags: [ 'school', 'storage', 'home' ], |
exclude: false |
} |
4. You cannot use
{ |
item: 'Pens', |
quantity: 350, |
tags: [ 'school', 'office' ], |
exclude: false |
}, |
{ |
item: 'Erasers', |
quantity: 15, |
tags: [ 'school', 'home' ], |
exclude: false |
}, |
{ |
item: 'Maps', |
tags: [ 'office', 'storage' ] |
}, |
{ |
item: 'Books', |
quantity: 5, |
tags: [ 'school', 'storage', 'home' ], |
exclude: false |
} |
5 operator expressions inside an { item: 'Erasers', quantity: 15, tags: [ 'school', 'home' ] }, |
{ item: 'Books', quantity: 5, tags: [ 'school', 'storage', 'home' ] } |
0Xem xét ví dụ sau:
db.inventory.find[ { tags: { $in: [ /^be/, /^st/ ] } } ]
db.inventory.find[ { quantity: { $in: [ 5, 15 ] } }, { _id: 0 } ]
Truy vấn này chọn tất cả các tài liệu trong bộ sưu tập db.inventory.updateMany[ |
{ tags: { $in: [ "home", "school" ] } }, |
{ $set: { exclude: false } } |
] |
1 trong đó giá trị của trường db.inventory.updateMany[ |
{ tags: { $in: [ "home", "school" ] } }, |
{ $set: { exclude: false } } |
] |
3 là 5 hoặc 15.