Comparison Operators
The comparison operators available to the default Query.
{ $eq: <value> }
The $eq
operator behaves like the $eq MongoDB equivalent
import {QueryLogic} from "can";
const queryLogic = new QueryLogic();
const filter = queryLogic.filterMembers(
{ filter: { age: {$eq: 3} } },
[{id: 1,age: 4},{id: 2,age: 3},{id: 3,age: 3},{id: 4,age: 2}]
);
console.log( filter ); //-> [{id: 2,age: 3},{id: 3,age: 3}]
{ $ne: <value> }
The $ne
operator behaves like the $ne MongoDB equivalent
import {QueryLogic} from "can";
const queryLogic = new QueryLogic();
const filter = queryLogic.filterMembers(
{ filter: { age: {$ne: 3} } },
[{id: 1,age: 4},{id: 2,age: 3},{id: 3,age: 3},{id: 4,age: 2}]
);
console.log( filter ); //-> [{id: 1,age: 4},{id: 4,age: 2}]
{ $in: [value,...] }
The $in
operator behaves like the $in MongoDB equivalent
import {QueryLogic} from "can";
const queryLogic = new QueryLogic();
const filter = queryLogic.filterMembers(
{ filter: { age: {$in: [4, 2]} } },
[{id: 1,age: 4},{id: 2,age: 3},{id: 3,age: 3},{id: 4,age: 2}]
);
console.log( filter ); //-> [{id: 1,age: 4},{id: 4,age: 2}]
{ $nin: [value,...] }
The $nin
operator behaves like the $nin MongoDB equivalent
import {QueryLogic} from "can";
const queryLogic = new QueryLogic();
const filter = queryLogic.filterMembers(
{ filter: { age: {$nin: [4, 2]} } },
[{id: 1,age: 4},{id: 2,age: 3},{id: 3,age: 3},{id: 4,age: 2}]
);
console.log( filter ); //-> [{id: 2,age: 3},{id: 3,age: 3}]
{ $gt: <value> }
The $gt
operator behaves like the $gt MongoDB equivalent
import {QueryLogic} from "can";
const queryLogic = new QueryLogic();
const filter = queryLogic.filterMembers(
{ filter: { age: {$gt: 3} } },
[{id: 1,age: 4},{id: 2,age: 3},{id: 3,age: 3},{id: 4,age:2}]
);
console.log( filter ); //-> [{id: 1,age: 4}]
{ $gte: <value> }
The $gte
operator behaves like the $gte MongoDB equivalent
import {QueryLogic} from "can";
const queryLogic = new QueryLogic();
const filter = queryLogic.filterMembers(
{ filter: { age: {$gte: 3} } },
[{id: 1,age: 4},{id: 2,age: 3},{id: 3,age: 3},{id: 4,age:2}]
);
console.log( filter ); //-> [{id: 1,age: 4},{id: 2,age: 3},{id: 3,age: 3}]
{ $lt: <value> }
The $lt
operator behaves like the $lt MongoDB equivalent
import {QueryLogic} from "can";
const queryLogic = new QueryLogic();
const filter = queryLogic.filterMembers(
{ filter: { age: {$lt: 3} } },
[{id: 1,age: 4},{id: 2,age: 3},{id: 3,age: 3},{id: 4,age:2}]
);
console.log( filter ); //-> [{id: 4,age:2}]
{ $lte: <value> }
The $lte
operator behaves like the $lte MongoDB equivalent
import {QueryLogic} from "can";
const queryLogic = new QueryLogic();
const filter = queryLogic.filterMembers(
{ filter: { age: {$lte: 3} } },
[{id: 1,age: 4},{id: 2,age: 3},{id: 3,age: 3},{id: 4,age:2}]
);
console.log( filter ); //-> [{id: 2,age: 3},{id: 3,age: 3},{id: 4,age:2}]
{ $all: <value> }
The $all
operator behaves like the $all MongoDB equivalent. It operates on Arrays, comparing a dataset against an array from which all matches must exist.
import {QueryLogic} from "can";
const queryLogic = new QueryLogic();
const data = [
{
"id": "Canada",
"colors": [ "red", "white" ]
},
{
"id": "Mexico",
"colors": [ "red", "white", "green" ]
},
{
"id": "USA",
"colors": [ "red", "white", "blue" ]
}
];
const filter = queryLogic.filterMembers(
{ filter: { colors: { $all: ["red", "white"] } } },
data
);
console.log( filter ); //-> matches all...
{ $not: <value> }
The $not
operator behaves like the $not MongoDB equivalent. It can be used to negate queries such as:
import {QueryLogic} from "can";
const queryLogic = new QueryLogic();
const data = [
{
"name": "Joe",
"age": 45
},
{
"name": "Zoey",
"age": 22
}
];
const filter = queryLogic.filterMembers(
{ filter: { age: { $not: { $lt: 40 } } },
data
);
console.log( filter ); //-> [{"name": "Joe", "age": 45}]
{ $and: <value> }
The and
operator behaves like the $and MongoDB equivalent.
import {QueryLogic} from "can";
const queryLogic = new QueryLogic();
const data = [
{
"id": "Canada",
"colors": [ "red", "white" ]
},
{
"id": "Mexico",
"colors": [ "red", "white", "green" ]
},
{
"id": "USA",
"colors": [ "red", "white", "blue" ]
}
];
const filter = queryLogic.filterMembers(
{ $and: [
{ colors: { $all: ["red", "white"] } },
{ colors: { $not: { $all: ["blue"] } } }
] },
data
);
console.log( filter ); //-> [{"id": "Mexico", "colors": [...]}]