11---
22 title : $getField
3- titleSuffix : Overview of the $getField operation in Azure Cosmos DB for MongoDB (vCore)
3+ titleSuffix : Overview of the $getField operator in Azure Cosmos DB for MongoDB (vCore)
44 description : The $getField operator allows retrieving the value of a specified field from a document.
55 author : suvishodcitus
66 ms.author : suvishod
77 ms.service : azure-cosmos-db
88 ms.subservice : mongodb-vcore
99 ms.topic : reference
10- ms.date : 02/12 /2025
10+ ms.date : 09/04 /2025
1111---
1212
1313# $getField
@@ -16,8 +16,6 @@ The `$getField` operator is used to retrieve the value of a specified field from
1616
1717## Syntax
1818
19- The syntax for the ` $getField ` operator is as follows:
20-
2119``` javascript
2220{
2321 $getField: {
@@ -48,50 +46,111 @@ Consider this sample document from the stores collection.
4846
4947``` json
5048{
51- "_id" : " 2cf3f885-9962-4b67-a172-aa9039e9ae2f" ,
52- "name" : " First Up Consultants | Bed and Bath Center - South Amir" ,
53- "location" : {
54- "lat" : 60.7954 ,
55- "lon" : -142.0012
56- },
57- "staff" : {
58- "totalStaff" : {
59- "fullTime" : 18 ,
60- "partTime" : 17
61- }
62- },
63- "sales" : {
64- "totalSales" : 37701 ,
65- "salesByCategory" : [
66- {
67- "categoryName" : " Mattress Toppers" ,
68- "totalSales" : 37701
69- }
70- ]
71- },
72- "promotionEvents" : [
73- {
74- "eventName" : " Price Drop Palooza" ,
75- "promotionalDates" : {
76- "startDate" : {
77- "Year" : 2024 ,
78- "Month" : 9 ,
79- "Day" : 21
80- },
81- "endDate" : {
82- "Year" : 2024 ,
83- "Month" : 9 ,
84- "Day" : 30
49+ "_id" : " 0fcc0bf0-ed18-4ab8-b558-9848e18058f4" ,
50+ "name" : " First Up Consultants | Beverage Shop - Satterfieldmouth" ,
51+ "location" : {
52+ "lat" : -89.2384 ,
53+ "lon" : -46.4012
54+ },
55+ "staff" : {
56+ "totalStaff" : {
57+ "fullTime" : 8 ,
58+ "partTime" : 20
8559 }
86- },
87- "discounts" : [
60+ },
61+ "sales" : {
62+ "totalSales" : 75670 ,
63+ "salesByCategory" : [
64+ {
65+ "categoryName" : " Wine Accessories" ,
66+ "totalSales" : 34440
67+ },
68+ {
69+ "categoryName" : " Bitters" ,
70+ "totalSales" : 39496
71+ },
72+ {
73+ "categoryName" : " Rum" ,
74+ "totalSales" : 1734
75+ }
76+ ]
77+ },
78+ "promotionEvents" : [
8879 {
89- "categoryName" : " Bath Accessories" ,
90- "discountPercentage" : 18
80+ "eventName" : " Unbeatable Bargain Bash" ,
81+ "promotionalDates" : {
82+ "startDate" : {
83+ "Year" : 2024 ,
84+ "Month" : 6 ,
85+ "Day" : 23
86+ },
87+ "endDate" : {
88+ "Year" : 2024 ,
89+ "Month" : 7 ,
90+ "Day" : 2
91+ }
92+ },
93+ "discounts" : [
94+ {
95+ "categoryName" : " Whiskey" ,
96+ "discountPercentage" : 7
97+ },
98+ {
99+ "categoryName" : " Bitters" ,
100+ "discountPercentage" : 15
101+ },
102+ {
103+ "categoryName" : " Brandy" ,
104+ "discountPercentage" : 8
105+ },
106+ {
107+ "categoryName" : " Sports Drinks" ,
108+ "discountPercentage" : 22
109+ },
110+ {
111+ "categoryName" : " Vodka" ,
112+ "discountPercentage" : 19
113+ }
114+ ]
115+ },
116+ {
117+ "eventName" : " Steal of a Deal Days" ,
118+ "promotionalDates" : {
119+ "startDate" : {
120+ "Year" : 2024 ,
121+ "Month" : 9 ,
122+ "Day" : 21
123+ },
124+ "endDate" : {
125+ "Year" : 2024 ,
126+ "Month" : 9 ,
127+ "Day" : 29
128+ }
129+ },
130+ "discounts" : [
131+ {
132+ "categoryName" : " Organic Wine" ,
133+ "discountPercentage" : 19
134+ },
135+ {
136+ "categoryName" : " White Wine" ,
137+ "discountPercentage" : 20
138+ },
139+ {
140+ "categoryName" : " Sparkling Wine" ,
141+ "discountPercentage" : 19
142+ },
143+ {
144+ "categoryName" : " Whiskey" ,
145+ "discountPercentage" : 17
146+ },
147+ {
148+ "categoryName" : " Vodka" ,
149+ "discountPercentage" : 23
150+ }
151+ ]
91152 }
92- ]
93- }
94- ]
153+ ]
95154}
96155```
97156
@@ -116,14 +175,16 @@ db.stores.aggregate([
116175])
117176```
118177
119- This command produces the following output:
178+ This query returns the following result.
120179
121180``` json
122- {
123- "_id" : " 2cf3f885-9962-4b67-a172-aa9039e9ae2f" ,
124- "name" : " First Up Consultants | Bed and Bath Center - South Amir" ,
125- "totalSalesValue" : 37701
126- }
181+ [
182+ {
183+ "_id" : " 2cf3f885-9962-4b67-a172-aa9039e9ae2f" ,
184+ "name" : " First Up Consultants | Bed and Bath Center - South Amir" ,
185+ "totalSalesValue" : 37701
186+ }
187+ ]
127188```
128189
129190### Example 2: Shorthand syntax
@@ -142,17 +203,19 @@ db.stores.aggregate([
142203])
143204```
144205
145- This command produces the following output:
206+ This query returns the following result.
146207
147208``` json
148- {
149- "_id" : " 2cf3f885-9962-4b67-a172-aa9039e9ae2f" ,
150- "storeName" : " First Up Consultants | Bed and Bath Center - South Amir" ,
151- "storeLocation" : {
152- "lat" : 60.7954 ,
153- "lon" : -142.0012
209+ [
210+ {
211+ "_id" : " 2cf3f885-9962-4b67-a172-aa9039e9ae2f" ,
212+ "storeName" : " First Up Consultants | Bed and Bath Center - South Amir" ,
213+ "storeLocation" : {
214+ "lat" : 60.7954 ,
215+ "lon" : -142.0012
216+ }
154217 }
155- }
218+ ]
156219```
157220
158221## Related content
0 commit comments