/
setIsSubset.txt
110 lines (75 loc) · 3.01 KB
/
setIsSubset.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
==========================
$setIsSubset (aggregation)
==========================
.. default-domain:: mongodb
.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol
Definition
----------
.. expression:: $setIsSubset
.. versionadded:: 2.6
Takes two arrays and returns ``true`` when the first array is a
subset of the second, including when the first array equals the
second array, and ``false`` otherwise.
:expression:`$setIsSubset` has the following syntax:
.. code-block:: javascript
{ $setIsSubset: [ <expression1>, <expression2> ] }
The arguments can be any valid :ref:`expression
<aggregation-expressions>` as long as they each resolve to an array.
For more information on expressions, see
:ref:`aggregation-expressions`.
Behavior
--------
.. |set-operator-name| replace:: :expression:`$setIsSubset`
.. include:: /includes/important-set-operator-semantics.rst
:end-before: .. set-output
.. |exp| replace:: :expression:`$setIsSubset`
.. include:: /includes/fact-agg-top-level-expressions.rst
.. list-table::
:header-rows: 1
:widths: 70, 15, 15
* - Example
-
- Result
* - ``{ $setIsSubset: [ [ "a", "b", "a" ], [ "b", "a" ] ] }``
-
- ``true``
* - ``{ $setIsSubset: [ [ "a", "b" ], [ [ "a", "b" ] ] ] }``
-
- ``false``
Example
-------
Consider an ``experiments`` collection with the following documents:
.. code-block:: javascript
{ "_id" : 1, "A" : [ "red", "blue" ], "B" : [ "red", "blue" ] }
{ "_id" : 2, "A" : [ "red", "blue" ], "B" : [ "blue", "red", "blue" ] }
{ "_id" : 3, "A" : [ "red", "blue" ], "B" : [ "red", "blue", "green" ] }
{ "_id" : 4, "A" : [ "red", "blue" ], "B" : [ "green", "red" ] }
{ "_id" : 5, "A" : [ "red", "blue" ], "B" : [ ] }
{ "_id" : 6, "A" : [ "red", "blue" ], "B" : [ [ "red" ], [ "blue" ] ] }
{ "_id" : 7, "A" : [ "red", "blue" ], "B" : [ [ "red", "blue" ] ] }
{ "_id" : 8, "A" : [ ], "B" : [ ] }
{ "_id" : 9, "A" : [ ], "B" : [ "red" ] }
The following operation uses the :expression:`$setIsSubset` operator to
determine if the ``A`` array is a subset of the ``B``
array:
.. code-block:: javascript
db.experiments.aggregate(
[
{ $project: { A:1, B: 1, AisSubset: { $setIsSubset: [ "$A", "$B" ] }, _id:0 } }
]
)
The operation returns the following results:
.. code-block:: javascript
{ "A" : [ "red", "blue" ], "B" : [ "red", "blue" ], "AisSubset" : true }
{ "A" : [ "red", "blue" ], "B" : [ "blue", "red", "blue" ], "AisSubset" : true }
{ "A" : [ "red", "blue" ], "B" : [ "red", "blue", "green" ], "AisSubset" : true }
{ "A" : [ "red", "blue" ], "B" : [ "green", "red" ], "AisSubset" : false }
{ "A" : [ "red", "blue" ], "B" : [ ], "AisSubset" : false }
{ "A" : [ "red", "blue" ], "B" : [ [ "red" ], [ "blue" ] ], "AisSubset" : false }
{ "A" : [ "red", "blue" ], "B" : [ [ "red", "blue" ] ], "AisSubset" : false }
{ "A" : [ ], "B" : [ ], "AisSubset" : true }
{ "A" : [ ], "B" : [ "red" ], "AisSubset" : true }