Docs Home → MongoDB Manual → MongoDB Manual
$size
Counts and returns the total number of items in an array.Counts and returns the total number of items in an array.
$size
has the following syntax: has the following syntax:
The argument for $size
can be any expression as long as it resolves to an array. For more information on expressions, see Expressions.$size
can be any
expression as long as it resolves to an array. For more information on expressions, see Expressions.
The argument for $size
must resolve to an array. If the argument for $size
is missing or does not resolve to an array, $size
errors.$size
must resolve to an array. If the argument for $size
is missing or does not resolve to an array,
$size
errors.
Consider an inventory
collection with the following documents:
{ "_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" }
The following aggregation pipeline operation uses the $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"} } } } ] ]
1 array:$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"} } } } ] ]
1 array:db.inventory.aggregate[[ { $project: { item: 1, numberOfColors: { $cond: { if: { $isArray: "$colors" }, then: { $size: "$colors" }, else: "NA"} } } } ] ]
The operation returns the following:
{ "_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" }
Đọc file json trong php
Trong bài này, chúng ta sẽ tìm hiểu về dữ liệu json trong PHP. Để học tốt bài này, các bạn cần đọc lại bài Cài đặt môi trường lập trình Web PHP với ...
Hướng dẫn tìm kiếm trong mongodb
Như trong bài viết trước mình đã giới thiệu basic nhất về mongodb với các bạn. Tiếp tục công cuộc tìm hiểu về Mongo. Chúng ta sẽ cùng nhau tìm hiểu về các ...
Hướng dẫn python split array into smaller arrays - python chia mảng thành các mảng nhỏ hơn
Tôi đang tìm cách để dễ dàng chia danh sách Python làm đôi.Vì vậy, nếu tôi có một mảng:A = [0,1,2,3,4,5] Tôi sẽ có thể nhận được:B = [0,1,2] C = [3,4,5] Hỏi ...
How do you initialize a 2D array?
Get this book -> Problems on Array: For Interviews and Competitive Programming Arrays are the derived type in C++ that can hold values of same data type in contiguous memory allocation. In an ...
Find string in array php
[PHP 4, PHP 5, PHP 7, PHP 8]in_array — Checks if a value exists in an arrayDescriptionin_array[mixed $needle, array $haystack, bool $strict = false]: boolParameters needle The searched value. Note: ...
Convert mongodb to replica set
Replication > Replica Set Deployment Tutorials > Convert a Standalone to a Replica SetThis tutorial describes the process for converting a standalone mongod instance into a replica set. Use ...
How do you check rows in python?
How do I get the row count of a Pandas DataFrame?This table summarises the different situations in which youd want to count something in a DataFrame [or Series, for completeness], along with the ...