Hướng dẫn size array mongodb - mảng kích thước mongodb

Tài liệu về nhà → Hướng dẫn sử dụng MongoDBMongoDB Manual

$sizeCounts và trả về tổng số mục trong một mảng.
Hướng dẫn size array mongodb - mảng kích thước mongodb

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 một bộ sưu tập

db.inventory.aggregate([
{
$project: {
item: 1,
numberOfColors: { $cond: { if: { $isArray: "$colors" }, then: { $size: "$colors" }, else: "NA"} }
}
}
] )
1 với các tài liệu sau:

{ "_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

db.inventory.aggregate([
{
$project: {
item: 1,
numberOfColors: { $cond: { if: { $isArray: "$colors" }, then: { $size: "$colors" }, else: "NA"} }
}
}
] )
3:$size operator to return the number of elements in the
db.inventory.aggregate([
{
$project: {
item: 1,
numberOfColors: { $cond: { if: { $isArray: "$colors" }, then: { $size: "$colors" }, else: "NA"} }
}
}
] )
3 array:

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" }

Tài liệu về nhà → Hướng dẫn sử dụng MongoDBMongoDB Manual

$size Trình vận hành $size khớp với bất kỳ mảng nào với số lượng các phần tử được chỉ định bởi đối số. Ví dụ:

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

db.inventory.aggregate([
{
$project: {
item: 1,
numberOfColors: { $cond: { if: { $isArray: "$colors" }, then: { $size: "$colors" }, else: "NA"} }
}
}
] )
6 trong đó
db.inventory.aggregate([
{
$project: {
item: 1,
numberOfColors: { $cond: { if: { $isArray: "$colors" }, then: { $size: "$colors" }, else: "NA"} }
}
}
] )
7 là một mảng có 2 phần tử. Chẳng hạn, biểu thức trên sẽ trả về
db.inventory.aggregate([
{
$project: {
item: 1,
numberOfColors: { $cond: { if: { $isArray: "$colors" }, then: { $size: "$colors" }, else: "NA"} }
}
}
] )
8 và
db.inventory.aggregate([
{
$project: {
item: 1,
numberOfColors: { $cond: { if: { $isArray: "$colors" }, then: { $size: "$colors" }, else: "NA"} }
}
}
] )
9 nhưng không phải
{ "_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" }
0 hoặc
{ "_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" }
1. Để 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: