“文档” 菜单
文档首页
/
MongoDB Manual
/ / /

$bsonSize(聚合)

在此页面上

  • 定义
  • 行为
  • 举例
$bsonSize

当编码为 BSON时,返回给定文档(即 bsontype Object )的大小(以字节为单位)。您可以使用 $bsonSize作为bsonSize()方法的替代方案。

$bsonSize 通过以下语法实现:

{ $bsonSize: <object> }

参数可以是任何有效的表达式,只要它解析为对象或 null 即可。有关表达式的更多信息,请参阅表达式

如果参数是一个对象,表达式将返回该对象以BSON 编码时的大小(以字节为单位)。

如果参数为null ,则表达式返回null

如果参数解析为对象或 null 以外的数据类型,则会出现 $bsonSize 错误。

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."
}
}
]);

以下聚合projects

  • name 字段

  • object_size 字段,它使用 $bsonSize 以字节为单位返回文档的大小。$$ROOT 变量引用了管道当前正在处理的文档。要了解有关聚合管道变量的更多信息,请参阅聚合表达式中的变量

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 }

以下管道会返回 employees 集合中所有文档的总大小:

db.employees.aggregate([
{
"$group": {
"_id": null,
"combined_object_size": { $sum: { $bsonSize: "$$ROOT" } }
}
}
])

当您指定 null$group_id 值或任何其他常量值时,$group 阶段会计算所有输入文档的累积值。

该操作使用 $sum 操作符来计算集合中每个文档总计的 $bsonSize$$ROOT 变量引用了管道当前正在处理的文档。要了解有关聚合管道变量的更多信息,请参阅聚合表达式中的变量

操作返回以下结果:

{ "_id" : null, "combined_object_size" : 789 }

提示

另请参阅:

以下管道返回具有最大 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 }
])
第一个阶段:

管道projects的第一阶段:

  • name 字段

  • task_object_size 字段,它使用 $bsonSize 返回以字节为单位的文档 current_task 字段的大小。

此阶段将以下文档输出到下一阶段:

{ "_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 }
第二阶段

第二阶段按sorts task_object_size文档。

此阶段将以下文档输出到下一阶段:

{ "_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 }
第三个阶段

第三阶段limits输出文档,仅返回按排序顺序排在第一位的文档:

{ "_id" : 2, "name" : "Bob", "task_object_size" : 152 }
← $bottomN(聚合累加器)

在此页面上