연 $bsonSize 산자는 BSON으로 인코딩할 때 문서 크기를 바이트 단위로 반환하는 데 사용됩니다. 컬렉션 내에서 문서의 스토리지 요구 사항을 이해하는 데 유용합니다. 연산자는 집계 파이프라인 내에서 문서 크기를 계산하는 데 사용할 수 있으며 스토리지를 최적화하고 성능에 미치는 영향을 이해하는 데 유용합니다.
문법
{
$bsonSize: <expression>
}
매개 변수
| 매개 변수 | Description |
|---|---|
<expression> |
계산할 BSON 크기의 문서로 확인되는 유효한 식입니다. |
예시
스토어 컬렉션에서 이 샘플 문서를 고려합니다.
{
"_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4",
"name": "First Up Consultants | Beverage Shop - Satterfieldmouth",
"location": {
"lat": -89.2384,
"lon": -46.4012
},
"staff": {
"totalStaff": {
"fullTime": 8,
"partTime": 20
}
},
"sales": {
"totalSales": 75670,
"salesByCategory": [
{
"categoryName": "Wine Accessories",
"totalSales": 34440
},
{
"categoryName": "Bitters",
"totalSales": 39496
},
{
"categoryName": "Rum",
"totalSales": 1734
}
]
},
"promotionEvents": [
{
"eventName": "Unbeatable Bargain Bash",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 6,
"Day": 23
},
"endDate": {
"Year": 2024,
"Month": 7,
"Day": 2
}
},
"discounts": [
{
"categoryName": "Whiskey",
"discountPercentage": 7
},
{
"categoryName": "Bitters",
"discountPercentage": 15
},
{
"categoryName": "Brandy",
"discountPercentage": 8
},
{
"categoryName": "Sports Drinks",
"discountPercentage": 22
},
{
"categoryName": "Vodka",
"discountPercentage": 19
}
]
},
{
"eventName": "Steal of a Deal Days",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 9,
"Day": 21
},
"endDate": {
"Year": 2024,
"Month": 9,
"Day": 29
}
},
"discounts": [
{
"categoryName": "Organic Wine",
"discountPercentage": 19
},
{
"categoryName": "White Wine",
"discountPercentage": 20
},
{
"categoryName": "Sparkling Wine",
"discountPercentage": 19
},
{
"categoryName": "Whiskey",
"discountPercentage": 17
},
{
"categoryName": "Vodka",
"discountPercentage": 23
}
]
}
]
}
예제 1: $bsonSize 사용하여 문서의 총 BSON 인코딩 크기를 바이트 단위로 계산합니다.
이 쿼리는 문서의 BSON 크기를 계산합니다.
db.stores.aggregate([
{
$project: {
_id: 1,
name: 1,
documentSize: {
$bsonSize: "$$ROOT" //pass the whole document
}
}
},
// Limit the result to the first 3 documents
{ $limit: 3 }
])
이 쿼리에서 반환된 처음 세 가지 결과는 다음과 같습니다.
[
{
"_id": "a715ab0f-4c6e-4e9d-a812-f2fab11ce0b6",
"name": "Lakeshore Retail | Holiday Supply Hub - Marvinfort",
"documentSize": 2226
},
{
"_id": "923d2228-6a28-4856-ac9d-77c39eaf1800",
"name": "Lakeshore Retail | Home Decor Hub - Franciscoton",
"documentSize": 1365
},
{
"_id": "7e53ca0f-6e24-4177-966c-fe62a11e9af5",
"name": "Contoso, Ltd. | Office Supply Deals - South Shana",
"documentSize": 1882
}
]