Skip to content

Commit 562b1cb

Browse files
committed
Document new comparison operators: $all, $not, $and
1 parent 1e78393 commit 562b1cb

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

doc/query-format/comparison-operators.md

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,104 @@
154154
```
155155
@codepen
156156
@highlight 6,only
157+
158+
@signature `{ $all: <value> }`
159+
160+
The `$all` operator behaves like the [$all MongoDB equivalent](https://docs.mongodb.com/manual/reference/operator/query/all/). It operates on Arrays, comparing a dataset against an array from which all matches must exist.
161+
162+
```js
163+
import {QueryLogic} from "can";
164+
165+
const queryLogic = new QueryLogic();
166+
167+
const data = [
168+
{
169+
"id": "Canada",
170+
"colors": [ "red", "white" ]
171+
},
172+
{
173+
"id": "Mexico",
174+
"colors": [ "red", "white", "green" ]
175+
},
176+
{
177+
"id": "USA",
178+
"colors": [ "red", "white", "blue" ]
179+
}
180+
];
181+
182+
const filter = queryLogic.filterMembers(
183+
{ filter: { colors: { $all: ["red", "white"] } } },
184+
data
185+
);
186+
187+
console.log( filter ); //-> matches all...
188+
```
189+
@codepen
190+
@highlight 5-18,21,only
191+
192+
@signature `{ $not: <value> }`
193+
194+
The `$not` operator behaves like the [$not MongoDB equivalent](https://docs.mongodb.com/manual/reference/operator/query/not/). It can be used to negate queries such as:
195+
196+
```js
197+
import {QueryLogic} from "can";
198+
199+
const queryLogic = new QueryLogic();
200+
201+
const data = [
202+
{
203+
"name": "Joe",
204+
"age": 45
205+
},
206+
{
207+
"name": "Zoey",
208+
"age": 22
209+
}
210+
];
211+
212+
const filter = queryLogic.filterMembers(
213+
{ filter: { age: { $not: { $lt: 40 } } },
214+
data
215+
);
216+
217+
console.log( filter ); //-> [{"name": "Joe", "age": 45}]
218+
```
219+
@codepen
220+
@highlight 5-14,17,only
221+
222+
@signature `{ $and: <value> }`
223+
224+
The `and` operator behaves like the [$and MongoDB equivalent](https://docs.mongodb.com/manual/reference/operator/query/and/index.html).
225+
226+
```js
227+
import {QueryLogic} from "can";
228+
229+
const queryLogic = new QueryLogic();
230+
231+
const data = [
232+
{
233+
"id": "Canada",
234+
"colors": [ "red", "white" ]
235+
},
236+
{
237+
"id": "Mexico",
238+
"colors": [ "red", "white", "green" ]
239+
},
240+
{
241+
"id": "USA",
242+
"colors": [ "red", "white", "blue" ]
243+
}
244+
];
245+
246+
const filter = queryLogic.filterMembers(
247+
{ $and: [
248+
{ colors: { $all: ["red", "white"] } },
249+
{ colors: { $not: { $all: ["blue"] } } }
250+
] },
251+
data
252+
);
253+
254+
console.log( filter ); //-> [{"id": "Mexico", "colors": [...]}]
255+
```
256+
@codepen
257+
@highlight 5-18,21-24,only

0 commit comments

Comments
 (0)