Skip to content

Commit 1448dc5

Browse files
authored
DOCSP 22495 simplifying setEquals example (#1416) (#1460)
* DOCSP-22495 simplifying setEqual example * DOCSP-22495 simplifying setEqual example * DOCSP-22495 simplifying setEqual example * DOCSP-22495 simplifying setEqual example * DOCSP-22495 simplifying setEqual example * DOCSP-22495 simplifying setEqual example * DOCSP-22495 simplifying setEqual example * DOCSP-22495 simplifying setEqual example * DOCSP-22495 simplifying setEqual example * DOCSP-22495 simplifying setEqual example * DOCSP-22495 simplifying setEqual example * DOCSP-22495 adding note and fixing format * DOCSP-22495 updating format of example * DOCSP-22495 adding note and fixing format * DOCSP-22495 adding note and fixing format * DOCSP-22495 formatting updates * DOCSP-22495 formatting fixes * DOCSP-22495 tech review comments * DOCSP-22495 tech review comments
1 parent b841bc3 commit 1448dc5

File tree

1 file changed

+56
-25
lines changed

1 file changed

+56
-25
lines changed

source/reference/operator/aggregation/setEquals.txt

Lines changed: 56 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -64,42 +64,73 @@ Behavior
6464
Example
6565
-------
6666

67-
Consider an ``experiments`` collection with the following documents:
67+
Consider a ``bakeryOrders`` collection with the following documents:
6868

6969
.. code-block:: javascript
7070

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+
] )
8078

8179
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:
8482

8583
.. code-block:: javascript
8684

87-
db.experiments.aggregate(
85+
db.bakeryOrders.aggregate(
8886
[
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``.
92107

93108
The operation returns the following results:
94109

95110
.. code-block:: javascript
96111

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

Comments
 (0)