validate objects & filter arrays with mongodb queries
For extended documentation, checkout http://docs.mongodb.org/manual/reference/operator/query/
Features:
- Supported operators: $in, $nin, $exists, $gte, $gt, $lte, $lt, $eq, $ne, $mod, $all, $and, $or, $nor, $not, $size, $type, $regex, $where, $elemMatch
- Regexp searches
- Function filtering
- sub object searching
- dot notation searching
- Supports node.js, and web
- Small (2 kb minified) library
- Custom Expressions
- filtering of immutable datastructures
Node.js Examples
; //intersecting arraysvar result = "hello" "sifted" "array!"; //['hello'] //regexp filtervar result = "craig" "john" "jake"; //['john','jake'] // function filtervar testFilter = ; var result = name: "craig" name: "john" name: "jake" ; // filtered: [{ name: 'craig' }] //you can test *single values* against your custom sifter; //true; //false\
Browser Examples
API
.sift(query: MongoQuery, options?: SiftOptions): Function
query
- the filter to use against the target arrayoptions
-select
- value selector -expressions
- custom expressions
With an array:
"craig" null; //['craig']
Without an array, a sifter is returned:
var existsFilter = ; ; //true; //false"craig" null; //['craig']
With a selector:
var omitNameFilter = ; name: "Craig" name: null ;
With your sifter, you can also test values:
; //false; //true
Supported Operators:
See MongoDB's advanced queries for more info.
$in
array value must be $in the given query:
Intersecting two arrays:
//filtered: ['Brazil']"Brazil" "Haiti" "Peru" "Chile";
Here's another example. This acts more like the $or operator:
name: "Craig" location: "Brazil" ;
$nin
Opposite of $in:
//filtered: ['Haiti','Peru','Chile']"Brazil" "Haiti" "Peru" "Chile";
$exists
Checks if whether a value exists:
//filtered: ['Craig','Tim'];
You can also filter out values that don't exist
//filtered: [{ name: 'Craig', city: 'Minneapolis' }] name: "Craig" city: "Minneapolis" name: "Tim" ;
$gte
Checks if a number is >= value:
//filtered: [2, 3]0 1 2 3;
$gt
Checks if a number is > value:
//filtered: [3]0 1 2 3;
$lte
Checks if a number is <= value.
//filtered: [0, 1, 2]0 1 2 3;
$lt
Checks if number is < value.
//filtered: [0, 1]0 1 2 3;
$eq
Checks if query === value
. Note that $eq can be omitted. For $eq, and $ne
//filtered: [{ state: 'MN' }] state: "MN" state: "CA" state: "WI" ;
Or:
//filtered: [{ state: 'MN' }] state: "MN" state: "CA" state: "WI" ;
$ne
Checks if query !== value
.
//filtered: [{ state: 'CA' }, { state: 'WI'}] state: "MN" state: "CA" state: "WI" ;
$mod
Modulus:
//filtered: [300, 600]100 200 300 400 500 600;
$all
values must match everything in array:
//filtered: [ { tags: ['books','programming','travel' ]} ] tags: "books" "programming" "travel" tags: "travel" "cooking" ;
$and
ability to use an array of expressions. All expressions must test true.
//filtered: [ { name: 'Craig', state: 'MN' }] name: "Craig" state: "MN" name: "Tim" state: "MN" name: "Joe" state: "CA" ;
$or
OR array of expressions.
//filtered: [ { name: 'Craig', state: 'MN' }, { name: 'Tim', state: 'MN' }] name: "Craig" state: "MN" name: "Tim" state: "MN" name: "Joe" state: "CA" ;
$nor
opposite of or:
//filtered: [ { name: 'Tim', state: 'MN' }, { name: 'Joe', state: 'CA' }] name: "Craig" state: "MN" name: "Tim" state: "MN" name: "Joe" state: "CA" ;
$size
Matches an array - must match given size:
//filtered: ['food','cooking'] tags: "food" "cooking" tags: "traveling" ;
$type
Matches a values based on the type
4342 "hello world"; //returns single date 4342 "hello world"; //returns ['hello world']
$regex
Matches values based on the given regular expression
"frank" "fred" "sam" "frost"; // ["fred", "frost"]"frank" "fred" "sam" "frost"; // ["fred", "frost"]
$where
Matches based on some javascript comparison
name: "frank" name: "joe" ; // ["frank"] name: "frank" name: "joe" ; // ["frank"]
$elemMatch
Matches elements of array
var bills = month: "july" casts: id: 1 value: 200 id: 2 value: 1000 month: "august" casts: id: 3 value: 1000 id: 4 value: 4000 ; var result = bills; // {month:'august', casts:[{id:3, value: 1000},{id: 4, value: 4000}]}
$not
Not expression:
"craig" "tim" "jake"; //['jake']"craig" "tim" "jake"; //['tim','jake']
Custom expressions
var filter = ; 1 2 3 4 5; // 1, 3, 5