Skip to content

Commit 6deebc9

Browse files
committed
Add reference files
1 parent 3a2123c commit 6deebc9

File tree

2 files changed

+359
-0
lines changed

2 files changed

+359
-0
lines changed
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
---
2+
title: $binarySize
3+
description: The $binarySize operator is used to return the size of a binary data field.
4+
type: operators
5+
category: data-size
6+
---
7+
8+
# $binarySize
9+
10+
The `$binarySize` operator is used to return the size of a binary data field. This can be useful when dealing with binary data stored, such as images, files, or any other binary content. The argument for `$binarySize` should be a string, or a binary value.
11+
12+
## Syntax
13+
14+
```javascript
15+
{
16+
$binarySize: "<field>"
17+
}
18+
```
19+
20+
### Parameters
21+
22+
| Parameter | Description |
23+
| --- | --- |
24+
| **`<field>`**| The field for which you want to get the binary size.|
25+
26+
## Examples
27+
28+
Consider this sample document from the stores collection.
29+
30+
```json
31+
{
32+
"_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4",
33+
"name": "First Up Consultants | Beverage Shop - Satterfieldmouth",
34+
"location": {
35+
"lat": -89.2384,
36+
"lon": -46.4012
37+
},
38+
"staff": {
39+
"totalStaff": {
40+
"fullTime": 8,
41+
"partTime": 20
42+
}
43+
},
44+
"sales": {
45+
"totalSales": 75670,
46+
"salesByCategory": [
47+
{
48+
"categoryName": "Wine Accessories",
49+
"totalSales": 34440
50+
},
51+
{
52+
"categoryName": "Bitters",
53+
"totalSales": 39496
54+
},
55+
{
56+
"categoryName": "Rum",
57+
"totalSales": 1734
58+
}
59+
]
60+
},
61+
"promotionEvents": [
62+
{
63+
"eventName": "Unbeatable Bargain Bash",
64+
"promotionalDates": {
65+
"startDate": {
66+
"Year": 2024,
67+
"Month": 6,
68+
"Day": 23
69+
},
70+
"endDate": {
71+
"Year": 2024,
72+
"Month": 7,
73+
"Day": 2
74+
}
75+
},
76+
"discounts": [
77+
{
78+
"categoryName": "Whiskey",
79+
"discountPercentage": 7
80+
},
81+
{
82+
"categoryName": "Bitters",
83+
"discountPercentage": 15
84+
},
85+
{
86+
"categoryName": "Brandy",
87+
"discountPercentage": 8
88+
},
89+
{
90+
"categoryName": "Sports Drinks",
91+
"discountPercentage": 22
92+
},
93+
{
94+
"categoryName": "Vodka",
95+
"discountPercentage": 19
96+
}
97+
]
98+
},
99+
{
100+
"eventName": "Steal of a Deal Days",
101+
"promotionalDates": {
102+
"startDate": {
103+
"Year": 2024,
104+
"Month": 9,
105+
"Day": 21
106+
},
107+
"endDate": {
108+
"Year": 2024,
109+
"Month": 9,
110+
"Day": 29
111+
}
112+
},
113+
"discounts": [
114+
{
115+
"categoryName": "Organic Wine",
116+
"discountPercentage": 19
117+
},
118+
{
119+
"categoryName": "White Wine",
120+
"discountPercentage": 20
121+
},
122+
{
123+
"categoryName": "Sparkling Wine",
124+
"discountPercentage": 19
125+
},
126+
{
127+
"categoryName": "Whiskey",
128+
"discountPercentage": 17
129+
},
130+
{
131+
"categoryName": "Vodka",
132+
"discountPercentage": 23
133+
}
134+
]
135+
}
136+
]
137+
}
138+
```
139+
140+
### Example 1: Calculate the size of a string or binary data in bytes using $binarySize
141+
142+
This query calculates the binary size of the name field for each document in the stores collection.
143+
144+
```javascript
145+
db.stores.aggregate([
146+
{
147+
$project: {
148+
name: 1,
149+
dataSize: {
150+
$binarySize: "$name" // Calculate the binary size of the string data
151+
}
152+
}
153+
},
154+
// Limit the result to the first 3 documents
155+
{ $limit: 3 }
156+
])
157+
```
158+
159+
The first three results returned by this query are:
160+
161+
```json
162+
[
163+
{
164+
"_id": "7e53ca0f-6e24-4177-966c-fe62a11e9af5",
165+
"name": "Contoso, Ltd. | Office Supply Deals - South Shana",
166+
"dataSize": 49
167+
},
168+
{
169+
"_id": "923d2228-6a28-4856-ac9d-77c39eaf1800",
170+
"name": "Lakeshore Retail | Home Decor Hub - Franciscoton",
171+
"dataSize": 48
172+
},
173+
{
174+
"_id": "a715ab0f-4c6e-4e9d-a812-f2fab11ce0b6",
175+
"name": "Lakeshore Retail | Holiday Supply Hub - Marvinfort",
176+
"dataSize": 50
177+
}
178+
]
179+
```
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
---
2+
title: $bsonSize
3+
description: The $bsonSize operator returns the size of a document in bytes when encoded as BSON.
4+
type: operators
5+
category: data-size
6+
---
7+
8+
# $bsonSize
9+
10+
The `$bsonSize` operator is used to return the size of a document in bytes when encoded as BSON. It's useful for understanding the storage requirements of documents within your collections. The operator can be used within aggregation pipelines to calculate the size of documents and is helpful for optimizing storage and understanding performance implications.
11+
12+
## Syntax
13+
14+
```javascript
15+
{
16+
$bsonSize: <expression>
17+
}
18+
```
19+
20+
### Parameters
21+
22+
| Parameter | Description |
23+
| --- | --- |
24+
| **`<expression>`**| Any valid expression that resolves to a document whose BSON size you want to calculate.|
25+
26+
## Examples
27+
28+
Consider this sample document from the stores collection.
29+
30+
```json
31+
{
32+
"_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4",
33+
"name": "First Up Consultants | Beverage Shop - Satterfieldmouth",
34+
"location": {
35+
"lat": -89.2384,
36+
"lon": -46.4012
37+
},
38+
"staff": {
39+
"totalStaff": {
40+
"fullTime": 8,
41+
"partTime": 20
42+
}
43+
},
44+
"sales": {
45+
"totalSales": 75670,
46+
"salesByCategory": [
47+
{
48+
"categoryName": "Wine Accessories",
49+
"totalSales": 34440
50+
},
51+
{
52+
"categoryName": "Bitters",
53+
"totalSales": 39496
54+
},
55+
{
56+
"categoryName": "Rum",
57+
"totalSales": 1734
58+
}
59+
]
60+
},
61+
"promotionEvents": [
62+
{
63+
"eventName": "Unbeatable Bargain Bash",
64+
"promotionalDates": {
65+
"startDate": {
66+
"Year": 2024,
67+
"Month": 6,
68+
"Day": 23
69+
},
70+
"endDate": {
71+
"Year": 2024,
72+
"Month": 7,
73+
"Day": 2
74+
}
75+
},
76+
"discounts": [
77+
{
78+
"categoryName": "Whiskey",
79+
"discountPercentage": 7
80+
},
81+
{
82+
"categoryName": "Bitters",
83+
"discountPercentage": 15
84+
},
85+
{
86+
"categoryName": "Brandy",
87+
"discountPercentage": 8
88+
},
89+
{
90+
"categoryName": "Sports Drinks",
91+
"discountPercentage": 22
92+
},
93+
{
94+
"categoryName": "Vodka",
95+
"discountPercentage": 19
96+
}
97+
]
98+
},
99+
{
100+
"eventName": "Steal of a Deal Days",
101+
"promotionalDates": {
102+
"startDate": {
103+
"Year": 2024,
104+
"Month": 9,
105+
"Day": 21
106+
},
107+
"endDate": {
108+
"Year": 2024,
109+
"Month": 9,
110+
"Day": 29
111+
}
112+
},
113+
"discounts": [
114+
{
115+
"categoryName": "Organic Wine",
116+
"discountPercentage": 19
117+
},
118+
{
119+
"categoryName": "White Wine",
120+
"discountPercentage": 20
121+
},
122+
{
123+
"categoryName": "Sparkling Wine",
124+
"discountPercentage": 19
125+
},
126+
{
127+
"categoryName": "Whiskey",
128+
"discountPercentage": 17
129+
},
130+
{
131+
"categoryName": "Vodka",
132+
"discountPercentage": 23
133+
}
134+
]
135+
}
136+
]
137+
}
138+
```
139+
140+
### Example 1: Calculate the total BSON-encoded size of a document in bytes using $bsonSize
141+
142+
This query calculates the BSON size of the document.
143+
144+
```javascript
145+
db.stores.aggregate([
146+
{
147+
$project: {
148+
_id: 1,
149+
name: 1,
150+
documentSize: {
151+
$bsonSize: "$$ROOT" //pass the whole document
152+
}
153+
}
154+
},
155+
// Limit the result to the first 3 documents
156+
{ $limit: 3 }
157+
])
158+
```
159+
160+
The first three results returned by this query are:
161+
162+
```json
163+
[
164+
{
165+
"_id": "a715ab0f-4c6e-4e9d-a812-f2fab11ce0b6",
166+
"name": "Lakeshore Retail | Holiday Supply Hub - Marvinfort",
167+
"documentSize": 2226
168+
},
169+
{
170+
"_id": "923d2228-6a28-4856-ac9d-77c39eaf1800",
171+
"name": "Lakeshore Retail | Home Decor Hub - Franciscoton",
172+
"documentSize": 1365
173+
},
174+
{
175+
"_id": "7e53ca0f-6e24-4177-966c-fe62a11e9af5",
176+
"name": "Contoso, Ltd. | Office Supply Deals - South Shana",
177+
"documentSize": 1882
178+
}
179+
]
180+
```

0 commit comments

Comments
 (0)