@@ -28,72 +28,123 @@ The `$binarySize` operator is used to return the size of a binary data field. Th
2828| --- | --- |
2929| ** ` <field> ` ** | The field for which you want to get the binary size.|
3030
31- ## Example
31+ ## Examples
3232
33- Let's understand the usage with sample json from ` stores ` dataset .
33+ Consider this sample document from the stores collection .
3434
3535``` json
3636{
37- "_id" : " 7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5" ,
38- "name" : " Lakeshore Retail | DJ Equipment Stop - Port Cecile" ,
39- "location" : {
40- "lat" : 60.1441 ,
41- "lon" : -141.5012
42- },
43- "staff" : {
44- "totalStaff" : {
45- "fullTime" : 2 ,
46- "partTime" : 0
47- }
48- },
49- "sales" : {
50- "salesByCategory" : [
51- {
52- "categoryName" : " DJ Headphones" ,
53- "totalSales" : 35921
54- }
55- ],
56- "fullSales" : 3700
57- },
58- "promotionEvents" : [
59- {
60- "eventName" : " Bargain Blitz Days" ,
61- "promotionalDates" : {
62- "startDate" : {
63- "Year" : 2024 ,
64- "Month" : 3 ,
65- "Day" : 11
66- },
67- "endDate" : {
68- "Year" : 2024 ,
69- "Month" : 2 ,
70- "Day" : 18
37+ "_id" : " 0fcc0bf0-ed18-4ab8-b558-9848e18058f4" ,
38+ "name" : " First Up Consultants | Beverage Shop - Satterfieldmouth" ,
39+ "location" : {
40+ "lat" : -89.2384 ,
41+ "lon" : -46.4012
42+ },
43+ "staff" : {
44+ "totalStaff" : {
45+ "fullTime" : 8 ,
46+ "partTime" : 20
7147 }
72- },
73- "discounts" : [
48+ },
49+ "sales" : {
50+ "totalSales" : 75670 ,
51+ "salesByCategory" : [
52+ {
53+ "categoryName" : " Wine Accessories" ,
54+ "totalSales" : 34440
55+ },
56+ {
57+ "categoryName" : " Bitters" ,
58+ "totalSales" : 39496
59+ },
60+ {
61+ "categoryName" : " Rum" ,
62+ "totalSales" : 1734
63+ }
64+ ]
65+ },
66+ "promotionEvents" : [
7467 {
75- "categoryName" : " DJ Turntables" ,
76- "discountPercentage" : 18
68+ "eventName" : " Unbeatable Bargain Bash" ,
69+ "promotionalDates" : {
70+ "startDate" : {
71+ "Year" : 2024 ,
72+ "Month" : 6 ,
73+ "Day" : 23
74+ },
75+ "endDate" : {
76+ "Year" : 2024 ,
77+ "Month" : 7 ,
78+ "Day" : 2
79+ }
80+ },
81+ "discounts" : [
82+ {
83+ "categoryName" : " Whiskey" ,
84+ "discountPercentage" : 7
85+ },
86+ {
87+ "categoryName" : " Bitters" ,
88+ "discountPercentage" : 15
89+ },
90+ {
91+ "categoryName" : " Brandy" ,
92+ "discountPercentage" : 8
93+ },
94+ {
95+ "categoryName" : " Sports Drinks" ,
96+ "discountPercentage" : 22
97+ },
98+ {
99+ "categoryName" : " Vodka" ,
100+ "discountPercentage" : 19
101+ }
102+ ]
77103 },
78104 {
79- "categoryName" : " DJ Mixers" ,
80- "discountPercentage" : 15
105+ "eventName" : " Steal of a Deal Days" ,
106+ "promotionalDates" : {
107+ "startDate" : {
108+ "Year" : 2024 ,
109+ "Month" : 9 ,
110+ "Day" : 21
111+ },
112+ "endDate" : {
113+ "Year" : 2024 ,
114+ "Month" : 9 ,
115+ "Day" : 29
116+ }
117+ },
118+ "discounts" : [
119+ {
120+ "categoryName" : " Organic Wine" ,
121+ "discountPercentage" : 19
122+ },
123+ {
124+ "categoryName" : " White Wine" ,
125+ "discountPercentage" : 20
126+ },
127+ {
128+ "categoryName" : " Sparkling Wine" ,
129+ "discountPercentage" : 19
130+ },
131+ {
132+ "categoryName" : " Whiskey" ,
133+ "discountPercentage" : 17
134+ },
135+ {
136+ "categoryName" : " Vodka" ,
137+ "discountPercentage" : 23
138+ }
139+ ]
81140 }
82- ]
83- }
84- ],
85- "tag" : [
86- " #ShopLocal" ,
87- " #SeasonalSale" ,
88- " #FreeShipping" ,
89- " #MembershipDeals"
90- ]
141+ ]
91142}
92143```
93144
94145### Example 1: Calculate the size of a string or binary data in bytes using $binarySize
95146
96- The aggregation pipeline calculates the binary size of the name field for each document in the stores collection.
147+ This query calculates the binary size of the name field for each document in the stores collection.
97148
98149``` javascript
99150db .stores .aggregate ([
@@ -110,9 +161,10 @@ db.stores.aggregate([
110161])
111162```
112163
113- The query results help us with understanding storage impact or when optimizing field sizes.
164+ The first three results returned by this query are:
114165
115166``` json
167+ [
116168 {
117169 "_id" : " 7e53ca0f-6e24-4177-966c-fe62a11e9af5" ,
118170 "name" : " Contoso, Ltd. | Office Supply Deals - South Shana" ,
@@ -128,6 +180,7 @@ The query results help us with understanding storage impact or when optimizing f
128180 "name" : " Lakeshore Retail | Holiday Supply Hub - Marvinfort" ,
129181 "dataSize" : 50
130182 }
183+ ]
131184```
132185
133186## Related content
0 commit comments