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