File tree 2 files changed +95
-0
lines changed
docs/80-modifying-results
2 files changed +95
-0
lines changed Original file line number Diff line number Diff line change
1
+ ---
2
+ sidebar_position : 10
3
+ ---
4
+
1
5
import Tabs from ' @theme/Tabs' ;
2
6
import TabItem from ' @theme/TabItem' ;
3
7
Original file line number Diff line number Diff line change
1
+ ---
2
+ sidebar_position : 20
3
+ ---
4
+
5
+ import Tabs from ' @theme/Tabs' ;
6
+ import TabItem from ' @theme/TabItem' ;
7
+
8
+ # 👐 Unset Fields from Results
9
+
10
+ ## $unset
11
+
12
+ We can remove fields from the results, either using $project or more fine-grained, using $unset. If we don't want the attributes we can do:
13
+
14
+
15
+ <Tabs groupId = " aggregations" >
16
+ <TabItem value = " atlas" label = " Atlas UI" >
17
+
18
+ ``` js
19
+ [
20
+ {$unset: " attributes" }
21
+ ]
22
+ ```
23
+
24
+ </TabItem >
25
+
26
+ <TabItem value = " mongodb-shell" label = " MongoDB Shell" >
27
+
28
+ ``` js
29
+ db .books .aggregate ([
30
+ {$unset: " attributes" }
31
+ ])
32
+ ```
33
+
34
+ </TabItem >
35
+ </Tabs >
36
+
37
+
38
+ 👐 Remove from results the fields ` totalInventory ` and ` available ` .
39
+
40
+ <details >
41
+ <summary >Answer</summary >
42
+ <div >
43
+
44
+ <Tabs groupId = " aggregations" >
45
+ <TabItem value = " atlas" label = " Atlas UI" >
46
+
47
+ Several ways to do this, other than using ` $project ` :
48
+
49
+ ``` js
50
+ [
51
+ {$unset: " totalInventory" },
52
+ {$unset: " available" },
53
+ ]
54
+
55
+ // or
56
+
57
+ [
58
+ {$unset: [" totalInventory" , " available" ] },
59
+ ]
60
+ ```
61
+
62
+ </TabItem >
63
+
64
+ <TabItem value = " mongodb-shell" label = " MongoDB Shell" >
65
+
66
+ ``` js
67
+ db .books .aggregate ([
68
+ {$unset: " totalInventory" },
69
+ {$unset: " available" },
70
+ ])
71
+
72
+ // or
73
+
74
+ db .books .aggregate ([
75
+ {$unset: [" totalInventory" , " available" ] },
76
+ ])
77
+
78
+ ```
79
+
80
+ </TabItem >
81
+ </Tabs >
82
+
83
+
84
+
85
+ </div >
86
+
87
+ :::info
88
+ [ $set] ( https://www.mongodb.com/docs/manual/reference/operator/aggregation/set/#mongodb-pipeline-pipe.-set ) is an alias for $addFields that you'll find on many older posts and documentation.
89
+ :::
90
+
91
+ </details >
You can’t perform that action at this time.
0 commit comments