Skip to content

Commit

Permalink
Add support $all in find function
Browse files Browse the repository at this point in the history
  • Loading branch information
wh1100717 committed Sep 6, 2014
1 parent 754e8fe commit c5d08ed
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ collection.remove({

#####Array

* [ ] [$all](http://docs.mongodb.org/manual/reference/operator/query/all/#op._S_all)<br>
* [X] [$all](http://docs.mongodb.org/manual/reference/operator/query/all/#op._S_all)<br>
Matches arrays that contain all elements specified in the query.
* [ ] [$elemMatch](http://docs.mongodb.org/manual/reference/operator/query/elemMatch/#op._S_elemMatch)<br>
Selects documents if element in the array field matches all the specified **$elemMatch** condition.
Expand Down
39 changes: 37 additions & 2 deletions dist/localdb/0.0.1/src/localdb-debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ define("localdb/0.0.1/src/lib/collection-debug", [], function(require, exports,
define("localdb/0.0.1/src/lib/criteria-debug", [], function(require, exports, module) {
// Generated by CoffeeScript 1.7.1
'use strict';
var Criteria, Utils, cmpCheck, logicCheck, numberCheck, regexCheck, stringCheck,
var Criteria, Utils, arrayCheck, cmpCheck, logicCheck, numberCheck, regexCheck, stringCheck,
__indexOf = [].indexOf || function(item) {
for (var i = 0, l = this.length; i < l; i++) {
if (i in this && this[i] === item) return i;
Expand Down Expand Up @@ -286,6 +286,33 @@ define("localdb/0.0.1/src/lib/criteria-debug", [], function(require, exports, mo
}
return true;
};
arrayCheck = function(obj, arrayKey, arrayCondition) {
var c, _i, _len;
switch (arrayKey) {
case "$all":
if (!Utils.isArray(obj)) {
return false;
}
for (_i = 0, _len = arrayCondition.length; _i < _len; _i++) {
c = arrayCondition[_i];
if (!(function() {
var d, _j, _len1;
for (_j = 0, _len1 = obj.length; _j < _len1; _j++) {
d = obj[_j];
if (Utils.isArray(c) ? arrayCheck(d, arrayKey, c) : d === c) {
return true;
}
}
})()) {
return false;
}
}
break;
default:
return void 0;
}
return true;
};
cmpCheck = function(obj, key, cmpCondition) {
var c_key, c_value, _ref, _ref1;
for (c_key in cmpCondition) {
Expand Down Expand Up @@ -356,7 +383,7 @@ define("localdb/0.0.1/src/lib/criteria-debug", [], function(require, exports, mo
};
Criteria = {};
Criteria.check = function(obj, criteria) {
var condition, key, logicCheckResult;
var arrayCheckResult, condition, key, logicCheckResult;
for (key in criteria) {
condition = criteria[key];
if (Utils.isNumber(condition)) {
Expand Down Expand Up @@ -388,6 +415,14 @@ define("localdb/0.0.1/src/lib/criteria-debug", [], function(require, exports, mo
return false;
}
}
arrayCheckResult = arrayCheck(obj, key, condition);
if (arrayCheckResult != null) {
if (arrayCheckResult) {
continue;
} else {
return false;
}
}
if (cmpCheck(obj, key, condition)) {
continue;
} else {
Expand Down
2 changes: 1 addition & 1 deletion dist/localdb/0.0.1/src/localdb.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions src/lib/criteria.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ logicCheck = (obj, logicKey, logicCondition) ->
else return undefined
return true

arrayCheck = (obj, arrayKey, arrayCondition) ->
switch arrayKey
when "$all"
return false if not Utils.isArray(obj)
return false for c in arrayCondition when not (-> return true for d in obj when if Utils.isArray(c) then arrayCheck(d, arrayKey, c) else d is c)()
else return undefined
return true


cmpCheck = (obj, key, cmpCondition) ->
for c_key, c_value of cmpCondition
switch c_key
Expand Down Expand Up @@ -56,6 +65,9 @@ Criteria.check = (obj, criteria) ->
# Logic Check
logicCheckResult = logicCheck(obj, key, condition)
(if logicCheckResult then continue else return false) if logicCheckResult?
# Array Check
arrayCheckResult = arrayCheck(obj, key, condition)
(if arrayCheckResult then continue else return false) if arrayCheckResult?
# Other Check (Comparison | Element | Evaluation)
if cmpCheck(obj, key, condition) then continue else return false
return true
Expand Down
40 changes: 38 additions & 2 deletions src/lib/criteria.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 16 additions & 2 deletions tests/localdb-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,22 @@ describe 'LocalDB', ->
}
console.log data
expect(/ello/.test(d.g)).to.be.ok() for d in data


it '$all', ->
collection.insert({a:1,b:2,c:3,h:[1,2,3,4],i:[[1,2,3],[1,2,4]]})
data = collection.find {
criteria: {
h: {$all: [1,2]}
}
}
expect(1 in d.h).to.be.ok() for d in data
expect(2 in d.h).to.be.ok() for d in data
data = collection.find {
criteria: {
i: {$all: [[1,2]]}
}
}
expect(1 in d.i[0]).to.be.ok() for d in data
expect(2 in d.i[0]).to.be.ok() for d in data



Expand Down
47 changes: 45 additions & 2 deletions tests/localdb-spec.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c5d08ed

Please sign in to comment.