$bsonSize (aggregation)
定義
$bsonSize
Returns the size in bytes of a given document (i.e. bsontype
Object
) when encoded as BSON. You can use$bsonSize
as an alternative to thebsonSize()
method.$bsonSize
の構文は次のとおりです。{ $bsonSize: <object> } The argument can be any valid 式 as long as it resolves to either an object or
null
. For more information on expressions, see 式演算子.
動作
If the argument is an object, the expression returns the size of the object in bytes when the object is encoded as BSON.
If the argument is null
, the expression returns null
.
If the argument resolves to a data type other than an object or
null
, $bsonSize
errors.
例
Return Sizes of Documents
mongosh
では、次のドキュメントを含むemployees
という名前のサンプル コレクションが作成されます。
db.employees.insertMany([ { "_id": 1, "name": "Alice", "email": "alice@company.com", "position": "Software Developer", "current_task": { "project_id": 1, "project_name": "Aggregation Improvements", "project_duration": 5, "hours": 20 } }, { "_id": 2, "name": "Bob", "email": "bob@company.com", "position": "Sales", "current_task": { "project_id": 2, "project_name": "Write Blog Posts", "project_duration": 2, "hours": 10, "notes": "Progress is slow. Waiting for feedback." } }, { "_id": 3, "name": "Charlie", "email": "charlie@company.com", "position": "HR (On Leave)", "current_task": null }, { "_id": 4, "name": "Dianne", "email": "diane@company.com", "position": "Web Designer", "current_task": { "project_id": 3, "project_name": "Update Home Page", "notes": "Need to scope this project." } } ]);
The following aggregation projects
:
The
name
fieldThe
object_size
field, which uses$bsonSize
to return the size of the document in bytes. The$$ROOT
variable references the document currently being processed by the pipeline. To learn more about variables in the aggregation pipeline, see 集計式の変数.
db.employees.aggregate([ { "$project": { "name": 1, "object_size": { $bsonSize: "$$ROOT" } } } ])
この操作では、次の結果を返します。
{ "_id" : 1, "name" : "Alice", "object_size" : 222 } { "_id" : 2, "name" : "Bob", "object_size" : 248 } { "_id" : 3, "name" : "Charlie", "object_size" : 112 } { "_id" : 4, "name" : "Dianne", "object_size" : 207 }
Return Combined Size of All Documents in a Collection
The following pipeline returns the combined size of all of the
documents in the employees
collection:
db.employees.aggregate([ { "$group": { "_id": null, "combined_object_size": { $sum: { $bsonSize: "$$ROOT" } } } } ])
When you specify an $group
_id value of null
, or any
other constant value, the $group
stage calculates
accumulated values for all the input documents as a whole.
The operation uses the $sum
operator to calculate the combined
$bsonSize
of each document in the collection. The
$$ROOT
variable references the document currently
being processed by the pipeline. To learn more about variables in the
aggregation pipeline, see 集計式の変数.
この操作では、次の結果を返します。
{ "_id" : null, "combined_object_size" : 789 }
以下も参照してください。
Return Document with Largest Specified Field
The following pipeline returns the document with the largest
current_task
field in bytes:
db.employees.aggregate([ // First Stage { $project: { name: "$name", task_object_size: { $bsonSize: "$current_task" } } }, // Second Stage { $sort: { "task_object_size" : -1 } }, // Third Stage { $limit: 1 } ])
- 第 1 ステージ
The first stage of the pipeline
projects
:The
name
fieldThe
task_object_size
field, which uses$bsonSize
to return the size of the document'scurrent_task
field in bytes.
This stage outputs the following documents to the next stage:
{ "_id" : 1, "name" : "Alice", "task_object_size" : 109 } { "_id" : 2, "name" : "Bob", "task_object_size" : 152 } { "_id" : 3, "name" : "Charlie", "task_object_size" : null } { "_id" : 4, "name" : "Dianne", "task_object_size" : 99 } - 第 2 ステージ
The second stage
sorts
the documents bytask_object_size
in descending order.This stage outputs the following documents to the next stage:
{ "_id" : 2, "name" : "Bob", "task_object_size" : 152 } { "_id" : 1, "name" : "Alice", "task_object_size" : 109 } { "_id" : 4, "name" : "Dianne", "task_object_size" : 99 } { "_id" : 3, "name" : "Charlie", "task_object_size" : null } - 第 3 ステージ
The third stage
limits
the output documents to only return the document appearing first in the sort order:{ "_id" : 2, "name" : "Bob", "task_object_size" : 152 }
以下も参照してください。