@@ -64,42 +64,73 @@ Behavior
64
64
Example
65
65
-------
66
66
67
- Consider an ``experiments `` collection with the following documents:
67
+ Consider a ``bakeryOrders `` collection with the following documents:
68
68
69
69
.. code-block:: javascript
70
70
71
- { "_id" : 1, "A" : [ "red", "blue" ], "B" : [ "red", "blue" ] }
72
- { "_id" : 2, "A" : [ "red", "blue" ], "B" : [ "blue", "red", "blue" ] }
73
- { "_id" : 3, "A" : [ "red", "blue" ], "B" : [ "red", "blue", "green" ] }
74
- { "_id" : 4, "A" : [ "red", "blue" ], "B" : [ "green", "red" ] }
75
- { "_id" : 5, "A" : [ "red", "blue" ], "B" : [ ] }
76
- { "_id" : 6, "A" : [ "red", "blue" ], "B" : [ [ "red" ], [ "blue" ] ] }
77
- { "_id" : 7, "A" : [ "red", "blue" ], "B" : [ [ "red", "blue" ] ] }
78
- { "_id" : 8, "A" : [ ], "B" : [ ] }
79
- { "_id" : 9, "A" : [ ], "B" : [ "red" ] }
71
+ db.bakeryOrders.insertMany( [
72
+ { _id: 0, cakes: ["chocolate", "vanilla"], cupcakes: ["chocolate", "vanilla"] },
73
+ { _id: 1, cakes: ["chocolate", "vanilla"], cupcakes: ["vanilla", "chocolate"] },
74
+ { _id: 2, cakes: ["chocolate", "chocolate"], cupcakes: ["chocolate"] },
75
+ { _id: 3, cakes: ["vanilla"], cupcakes: ["chocolate"] },
76
+ { _id: 4, cakes: ["vanilla"], cupcakes: [] }
77
+ ] )
80
78
81
79
The following operation uses the :expression:`$setEquals` operator to
82
- determine if the ``A `` array and the ``B `` array
83
- contain the same elements :
80
+ determine if the ``cakes `` array and the ``cupcakes `` array in each order
81
+ contain the same flavors :
84
82
85
83
.. code-block:: javascript
86
84
87
- db.experiments .aggregate(
85
+ db.bakeryOrders .aggregate(
88
86
[
89
- { $project: { A: 1, B: 1, sameElements: { $setEquals: [ "$A", "$B" ] }, _id: 0 } }
90
- ]
91
- )
87
+ {
88
+ $project: {
89
+ _id: 0,
90
+ cakes: 1,
91
+ cupcakes: 1,
92
+ sameFlavors: { $setEquals: [ "$cakes", "$cupcakes" ] }
93
+ }
94
+ }
95
+ ] )
96
+
97
+ .. note:: $project
98
+
99
+ The :pipeline:`$project` stage specifies which fields are included
100
+ in the output documents. In this example, the :pipeline:`$project`
101
+ stage:
102
+
103
+ - Excludes the ``_id`` field from the output.
104
+ - Includes the ``cakes`` and ``cupcakes`` fields in the output.
105
+ - Outputs the result of the ``$setEquals`` operator in a new field
106
+ called ``sameFlavors``.
92
107
93
108
The operation returns the following results:
94
109
95
110
.. code-block:: javascript
96
111
97
- { "A" : [ "red", "blue" ], "B" : [ "red", "blue" ], "sameElements" : true }
98
- { "A" : [ "red", "blue" ], "B" : [ "blue", "red", "blue" ], "sameElements" : true }
99
- { "A" : [ "red", "blue" ], "B" : [ "red", "blue", "green" ], "sameElements" : false }
100
- { "A" : [ "red", "blue" ], "B" : [ "green", "red" ], "sameElements" : false }
101
- { "A" : [ "red", "blue" ], "B" : [ ], "sameElements" : false }
102
- { "A" : [ "red", "blue" ], "B" : [ [ "red" ], [ "blue" ] ], "sameElements" : false }
103
- { "A" : [ "red", "blue" ], "B" : [ [ "red", "blue" ] ], "sameElements" : false }
104
- { "A" : [ ], "B" : [ ], "sameElements" : true }
105
- { "A" : [ ], "B" : [ "red" ], "sameElements" : false }
112
+ {
113
+ cakes: [ "chocolate", "vanilla" ],
114
+ cupcakes: [ "chocolate", "vanilla" ],
115
+ sameFlavors: true
116
+ },
117
+ {
118
+ cakes: [ "chocolate", "vanilla" ],
119
+ cupcakes: [ "vanilla", "chocolate" ],
120
+ sameFlavors: true
121
+ },
122
+ {
123
+ cakes: [ "chocolate", "chocolate" ],
124
+ cupcakes: [ "chocolate" ],
125
+ sameFlavors: true
126
+ },
127
+ {
128
+ cakes: [ "vanilla" ],
129
+ cupcakes: [ "chocolate" ],
130
+ sameFlavors: false
131
+ },
132
+ {
133
+ cakes: [ "vanilla" ],
134
+ cupcakes: [],
135
+ sameFlavors: false
136
+ }
0 commit comments