Skip to content

Commit 1de7728

Browse files
authored
DOCSP 22495 simplifying setEquals example (#1416) (#1457)
* 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 c4280b3 commit 1de7728

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
@@ -58,42 +58,73 @@ Behavior
5858
Example
5959
-------
6060

61-
Consider an ``experiments`` collection with the following documents:
61+
Consider a ``bakeryOrders`` collection with the following documents:
6262

6363
.. code-block:: javascript
6464

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

7573
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:
7876

7977
.. code-block:: javascript
8078

81-
db.experiments.aggregate(
79+
db.bakeryOrders.aggregate(
8280
[
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``.
86101

87102
The operation returns the following results:
88103

89104
.. code-block:: javascript
90105

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

Comments
 (0)