Tài liệu về nhà → Hướng dẫn sử dụng MongoDB → MongoDB Manual
The $size operator matches any array with the number of elements specified by the argument. For example:
db.collection.find( { field: { $size: 2 } } );
Trả về tất cả các tài liệu trong collection trong đó field là một mảng có 2 yếu tố. Chẳng hạn, biểu thức trên sẽ trả về { field: [ red, green ] } và
db.collection.find( { field: { $size: 1 } } );
0 nhưng không phảidb.collection.find( { field: { $size: 1 } } );
1 hoặcdb.collection.find( { field: { $size: 1 } } );
2. Để khớp các trường chỉ có một phần tử trong một mảng sử dụng $size với giá trị 1, như sau:$size with a value of 1, as follows:db.collection.find( { field: { $size: 1 } } );
$size không chấp nhận phạm vi của các giá trị. Để chọn các tài liệu dựa trên các trường có số lượng thành phần khác nhau, hãy tạo một trường truy cập mà bạn tăng khi bạn thêm các phần tử vào một trường. does not accept ranges of values. To select documents based on fields with different numbers of elements, create a counter field that you increment when you add elements to a field.
Truy vấn không thể sử dụng các chỉ mục cho phần $size của truy vấn, mặc dù các phần khác của truy vấn có thể sử dụng các chỉ mục nếu có.$size portion of a query, although the other portions of a query can use indexes if applicable.
Để biết các ví dụ bổ sung trong các mảng truy vấn, xem:
Truy vấn một mảng
Truy vấn một loạt các tài liệu nhúng
Để biết các ví dụ bổ sung trong truy vấn, xem:
Tài liệu truy vấn
Mẹo
Xem thêm:
Tài liệu về nhà → Hướng dẫn sử dụng MongoDB → MongoDB Manual
Counts and returns the total number of items in an array.
$size có cú pháp sau: has the following syntax:
Đối số cho $size có thể là bất kỳ biểu thức nào miễn là nó giải quyết thành một mảng. Để biết thêm thông tin về biểu thức, xem biểu thức.$size can be any expression as long as it resolves to an array. For more information on expressions, see Expressions.
Đối số cho $size phải giải quyết cho một mảng. Nếu đối số cho $size bị thiếu hoặc không giải quyết được một mảng, $size lỗi.$size must resolve to an array. If the argument for $size is missing or does not resolve to an array, $size errors.
Xem xét bộ sưu tập
{ "_id" : 1, "item" : "ABC1", "description" : "product 1", colors: [ "blue", "black", "red" ] } |
{ "_id" : 2, "item" : "ABC2", "description" : "product 2", colors: [ "purple" ] } |
{ "_id" : 3, "item" : "XYZ1", "description" : "product 3", colors: [ ] } |
{ "_id" : 4, "item" : "ZZZ1", "description" : "product 4 - missing colors" } |
{ "_id" : 5, "item" : "ZZZ2", "description" : "product 5 - colors is string", colors: "blue,red" } |
{ "_id" : 1, "item" : "ABC1", "description" : "product 1", colors: [ "blue", "black", "red" ] } |
{ "_id" : 2, "item" : "ABC2", "description" : "product 2", colors: [ "purple" ] } |
{ "_id" : 3, "item" : "XYZ1", "description" : "product 3", colors: [ ] } |
{ "_id" : 4, "item" : "ZZZ1", "description" : "product 4 - missing colors" } |
{ "_id" : 5, "item" : "ZZZ2", "description" : "product 5 - colors is string", colors: "blue,red" } |
Hoạt động đường ống tổng hợp sau đây sử dụng toán tử $size để trả về số lượng phần tử trong mảng
{ "_id" : 1, "item" : "ABC1", "description" : "product 1", colors: [ "blue", "black", "red" ] } |
{ "_id" : 2, "item" : "ABC2", "description" : "product 2", colors: [ "purple" ] } |
{ "_id" : 3, "item" : "XYZ1", "description" : "product 3", colors: [ ] } |
{ "_id" : 4, "item" : "ZZZ1", "description" : "product 4 - missing colors" } |
{ "_id" : 5, "item" : "ZZZ2", "description" : "product 5 - colors is string", colors: "blue,red" } |
{ "_id" : 1, "item" : "ABC1", "description" : "product 1", colors: [ "blue", "black", "red" ] } |
{ "_id" : 2, "item" : "ABC2", "description" : "product 2", colors: [ "purple" ] } |
{ "_id" : 3, "item" : "XYZ1", "description" : "product 3", colors: [ ] } |
{ "_id" : 4, "item" : "ZZZ1", "description" : "product 4 - missing colors" } |
{ "_id" : 5, "item" : "ZZZ2", "description" : "product 5 - colors is string", colors: "blue,red" } |
db.inventory.aggregate([ |
{ |
$project: { |
item: 1, |
numberOfColors: { $cond: { if: { $isArray: "$colors" }, then: { $size: "$colors" }, else: "NA"} } |
} |
} |
] ) |
Hoạt động trả về như sau:
{ "_id" : 1, "item" : "ABC1", "numberOfColors" : 3 } |
{ "_id" : 2, "item" : "ABC2", "numberOfColors" : 1 } |
{ "_id" : 3, "item" : "XYZ1", "numberOfColors" : 0 } |
{ "_id" : 4, "item" : "ZZZ1", "numberOfColors" : "NA" } |
{ "_id" : 5, "item" : "ZZZ2", "numberOfColors" : "NA" } |