Tài liệu về nhà → Hướng dẫn sử dụng MongoDB → MongoDB Manual
Trên trang này
- Khớp với một mảng
- Truy vấn một mảng cho một phần tử
- Chỉ định nhiều điều kiện cho các phần tử mảng
- Hướng dẫn truy vấn bổ sung
Sử dụng menu chọn ngôn ngữ của bạn ở phía trên bên phải để đặt ngôn ngữ của các ví dụ sau.Select your language drop-down menu in the upper-right to set the language of the following examples.
Các truy vấn ví dụ sau đây cho tất cả các tài liệu trong đó giá trị trường tags
là một mảng có chính xác hai phần tử, "red"
và "blank"
, theo thứ tự được chỉ định:
Thay vào đó, nếu bạn muốn tìm một mảng chứa cả hai phần tử "red"
và "blank"
, mà không liên quan đến thứ tự hoặc các phần tử khác trong mảng, hãy sử dụng toán tử $all
:
Các truy vấn ví dụ sau đây cho tất cả các tài liệu trong đó tags
là một mảng chứa chuỗi "red"
là một trong các yếu tố của nó:
Ví dụ: các truy vấn hoạt động sau đây cho tất cả các tài liệu trong đó mảng
db.warehouses.insertMany[ [ { "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] }, { "_id" : 2, instock: [ "apples", "pudding", "pie" ] }, { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] }, { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] } ] ]
2 chứa ít nhất một phần tử có giá trị lớn hơn db.warehouses.insertMany[ [ { "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] }, { "_id" : 2, instock: [ "apples", "pudding", "pie" ] }, { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] }, { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] } ] ]
3.Khi chỉ định các điều kiện hợp chất trên các phần tử mảng, bạn có thể chỉ định truy vấn sao cho một phần tử mảng duy nhất đáp ứng các điều kiện này hoặc bất kỳ sự kết hợp nào của các phần tử mảng đáp ứng các điều kiện.
Các truy vấn ví dụ sau đây cho các tài liệu trong đó mảng
db.warehouses.insertMany[ [ { "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] }, { "_id" : 2, instock: [ "apples", "pudding", "pie" ] }, { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] }, { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] } ] ]
2 chứa các yếu tố trong một số kết hợp thỏa mãn các điều kiện truy vấn; ví dụ: một yếu tố có thể thỏa mãn điều kiện lớn hơn db.warehouses.insertMany[ [ { "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] }, { "_id" : 2, instock: [ "apples", "pudding", "pie" ] }, { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] }, { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] } ] ]
5 và một phần tử khác có thể thỏa mãn điều kiện nhỏ hơn db.warehouses.insertMany[ [ { "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] }, { "_id" : 2, instock: [ "apples", "pudding", "pie" ] }, { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] }, { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] } ] ]
6 hoặc một phần tử duy nhất có thể thỏa mãn cả hai:Sử dụng toán tử
db.warehouses.insertMany[ [ { "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] }, { "_id" : 2, instock: [ "apples", "pudding", "pie" ] }, { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] }, { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] } ] ]
7 để chỉ định nhiều tiêu chí về các phần tử của một mảng sao cho ít nhất một phần tử mảng đáp ứng tất cả các tiêu chí được chỉ định.Các truy vấn ví dụ sau đây cho các tài liệu trong đó mảng
db.warehouses.insertMany[ [ { "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] }, { "_id" : 2, instock: [ "apples", "pudding", "pie" ] }, { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] }, { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] } ] ]
2 chứa ít nhất một phần tử vừa hơn [db.warehouses.insertMany[ [ { "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] }, { "_id" : 2, instock: [ "apples", "pudding", "pie" ] }, { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] }, { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] } ] ]
9] db.warehouses.aggregate[ [ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ] ]
0 và nhỏ hơn [db.warehouses.aggregate[ [ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ] ]
1] db.warehouses.aggregate[ [ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ] ]
2:Sử dụng ký hiệu DOT, bạn có thể chỉ định các điều kiện truy vấn cho một phần tử tại một chỉ mục hoặc vị trí cụ thể của mảng. Mảng sử dụng lập chỉ mục dựa trên không.
Ghi chú
Khi truy vấn sử dụng ký hiệu DOT, trường và trường lồng nhau phải ở bên trong dấu ngoặc kép.
Các truy vấn ví dụ sau đây cho tất cả các tài liệu trong đó phần tử thứ hai trong mảng
db.warehouses.insertMany[ [ { "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] }, { "_id" : 2, instock: [ "apples", "pudding", "pie" ] }, { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] }, { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] } ] ]
2 lớn hơn db.warehouses.insertMany[ [ { "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] }, { "_id" : 2, instock: [ "apples", "pudding", "pie" ] }, { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] }, { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] } ] ]
3:Sử dụng toán tử
db.warehouses.aggregate[ [ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ] ]
5 để truy vấn cho các mảng theo số phần tử. Ví dụ: các tài liệu chọn sau đây trong đó mảng tags
có 3 yếu tố.Để biết các ví dụ truy vấn bổ sung, xem:
Tài liệu truy vấn
Truy vấn trên các tài liệu nhúng/lồng nhau
Truy vấn một loạt các tài liệu nhúng
Tài liệu về nhà → Hướng dẫn sử dụng MongoDB → MongoDB Manual
db.warehouses.aggregate[ [ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ] ]
7
Xác định nếu toán hạng là một mảng. Trả lại một boolean.
7 có cú pháp sau: has the following syntax:db.warehouses.aggregate[ [ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ] ]
{ $isArray: [ ] }
db.warehouses.aggregate[ [ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ] ]
9 có thể là bất kỳ biểu thức hợp lệ. Để biết thêm thông tin về biểu thức, xem biểu thức.
|
|
| ||||||||||||
|
|
| ||||||||||||
|
|
|
Ghi chú
Biểu thức tổng hợp chấp nhận một số lượng khác nhau của các đối số. Những đối số này thường được thông qua như một mảng. Tuy nhiên, khi đối số là một giá trị duy nhất, bạn có thể đơn giản hóa mã của mình bằng cách truyền trực tiếp đối số mà không cần quấn nó trong một mảng.
Tạo bộ sưu tập
{ "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }
9:db.warehouses.insertMany[ [ { "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] }, { "_id" : 2, instock: [ "apples", "pudding", "pie" ] }, { "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] }, { "_id" : 4, instock: [ "ice cream" ], ordered: [ ] } ] ]
Kiểm tra xem các trường tags
0 và tags
1 có phải là mảng không. Nếu cả hai trường là mảng, hãy nối chúng:
db.warehouses.aggregate[ [ { $project: { items: { $cond: { if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, then: { $concatArrays: [ "$instock", "$ordered" ] }, else: "One or more fields is not an array." } } } } ] ]
{ "_id" : 1, "items" : [ "chocolate", "butter", "apples" ] } { "_id" : 2, "items" : "One or more fields is not an array." } { "_id" : 3, "items" : [ "pears", "pecans", "cherries" ] } { "_id" : 4, "items" : [ "ice cream" ] }