$bsonSize (aggregation)

On this page本页内容

Definition定义

$bsonSize

New in version 4.4.在版本4.4中新增

Returns the size in bytes of a given document (i.e. bsontype Object) when encoded as BSON. 当编码为BSON时,返回给定文档(即bsontypeObject)的字节大小。You can use $bsonSize as an alternative to the Object.bsonSize() method.您可以使用$bsonSize作为ObjectbsonSize()方法的替代方法。

$bsonSize has the following syntax:语法如下:

{ $bsonSize: <object> }

The argument can be any valid expression as long as it resolves to either an object or null. 参数可以是任何有效表达式,只要它解析为对象或空。For more information on expressions, see Expressions.有关表达式的详细信息,请参阅表达式

Behavior行为

If the argument is an object, the expression returns the size of the object in bytes when the object is encoded as BSON.如果参数是对象,则表达式将返回对象编码为BSON时对象的大小(以字节为单位)。

If the argument is null, the expression returns null.如果参数为null,则表达式返回null

If the argument resolves to a data type other than an object or null, $bsonSize errors.如果参数解析为对象或null以外的数据类型,$bsonSize错误。

Examples示例

Return Sizes of Documents文件的返回大小

In mongosh, create a sample collection named employees with the following 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:以下聚合projects

  • The name fieldname字段
  • The object_size field, which uses $bsonSize to return the size of the document in bytes. object_size字段,它使用$bsonSize返回以字节为单位的文档大小。The $$ROOT variable references the document currently being processed by the pipeline. $$ROOT变量引用管道当前正在处理的文档。To learn more about variables in the aggregation pipeline, see Variables in Aggregation Expressions.要了解有关聚合管道中变量的更多信息,请参阅聚合表达式中的变量
db.employees.aggregate([
  {
    "$project": {
      "name": 1,
      "object_size": { $bsonSize: "$$ROOT" }
    }
  }
])

The operation returns the following result:该操作返回以下结果:

{ "_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:以下管道返回employees集合中所有文档的合并大小:

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.当您将$group_id值指定为null或任何其他常量值时,$group阶段将计算所有输入文档的累积值。

The operation uses the $sum operator to calculate the combined $bsonSize of each document in the collection. 该操作使用$sum运算符计算集合中每个文档的合并$bsonSizeThe $$ROOT variable references the document currently being processed by the pipeline. $$ROOT变量引用管道当前正在处理的文档。To learn more about variables in the aggregation pipeline, see Variables in Aggregation Expressions.要了解有关聚合管道中变量的更多信息,请参阅聚合表达式中的变量

The operation returns the following result:该操作返回以下结果:

{ "_id" : null, "combined_object_size" : 789 }
Tip提示
See also: 参阅:

Return Document with Largest Specified Field返回指定字段最大的单据

The following pipeline returns the document with the largest current_task field in bytes:以下管道返回具有最大current_task字段(字节)的文档:

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 }
])
First Stage第一阶段

The first stage of the pipeline projects:管道projects的第一阶段:

  • The name fieldname字段
  • The task_object_size field, which uses $bsonSize to return the size of the document's current_task field in bytes.task_object_size字段,它使用$bsonSize返回文档current_task字段的大小(以字节为单位)。

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 }
Second Stage第二阶段

The second stage sorts the documents by task_object_size in descending order.第二阶段按task_object_size降序对文档进行排序

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 }
Third Stage第三阶段

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 }
Tip提示
←  $bottomN (aggregation accumulator)$ceil (aggregation) →