정의
행동
인수가 객체이면 이 표현식은 객체가 BSON으로 인코딩된 경우 객체의 크기를 바이트 단위로 반환합니다.
인수가 null 이면 표현식은 null 을 반환합니다.
If the argument resolves to a data type other than an object or null, $bsonSize errors.
예시
문서 반환 크기
In mongosh, create a sample collection named employees with the following documents:
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필드The
object_sizefield, which uses$bsonSizeto return the size of the document in bytes. The$$ROOTvariable references the document currently being processed by the pipeline. To learn more about variables in the aggregation pipeline, see Variables in Aggregation Expressions.
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 }
collection에 있는 모든 문서의 합산 크기 반환
다음 파이프라인은 employees 컬렉션 에 있는 모든 문서의 결합된 크기를 반환합니다.
db.employees.aggregate([ { "$group": { "_id": null, "combined_object_size": { $sum: { $bsonSize: "$$ROOT" } } } } ])
null의 $group _id 값 또는 기타 상수 값을 지정하면 $group 단계는 모든 입력 문서에 대한 누적 값을 전체적으로 계산합니다.
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 Variables in Aggregation Expressions.
이 연산은 다음과 같은 결과를 반환합니다.
{ "_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필드The
task_object_sizefield, which uses$bsonSizeto return the size of the document'scurrent_taskfield in bytes.
이 단계에서는 다음 문서를 다음 단계로 출력합니다.
{ "_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 } - 두 번째 단계
두 번째 단계에서는 문서를
task_object_size씩 내림차순으로sorts처리합니다.이 단계에서는 다음 문서를 다음 단계로 출력합니다.
{ "_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 }