Comparing version 0.10.1 to 0.10.2
@@ -644,2 +644,19 @@ /** | ||
/** | ||
* Use a function to match | ||
* @param {Model} obj | ||
* @param {Query} query | ||
*/ | ||
logicalOperators.$where = function (obj, fn) { | ||
var result; | ||
if (!_.isFunction(fn)) { throw "$where operator used without a function"; } | ||
result = fn.call(obj); | ||
if (!_.isBoolean(result)) { throw "$where function must return boolean"; } | ||
return result; | ||
}; | ||
/** | ||
* Tell if a given document matches a query | ||
@@ -646,0 +663,0 @@ * @param {Object} obj Document to check |
{ | ||
"name": "nedb", | ||
"version": "0.10.1", | ||
"version": "0.10.2", | ||
"author": { | ||
@@ -5,0 +5,0 @@ "name": "Louis Chatriot", |
@@ -34,3 +34,3 @@ # NeDB (Node embedded database) | ||
* <a href="#array-fields">Array fields</a> | ||
* <a href="#logical-operators-or-and-not">Logical operators $or, $and, $not</a> | ||
* <a href="#logical-operators-or-and-not-where">Logical operators $or, $and, $not, $where</a> | ||
* <a href="#sorting-and-paginating">Sorting and paginating</a> | ||
@@ -150,3 +150,3 @@ * <a href="#counting-documents">Counting documents</a> | ||
### Finding documents | ||
Use `find` to look for multiple documents matching you query, or `findOne` to look for one specific document. You can select documents based on field equality or use comparison operators (`$lt`, `$lte`, `$gt`, `$gte`, `$in`, `$nin`, `$ne`). You can also use logical operators `$or`, `$and` and `$not`. See below for the syntax. | ||
Use `find` to look for multiple documents matching you query, or `findOne` to look for one specific document. You can select documents based on field equality or use comparison operators (`$lt`, `$lte`, `$gt`, `$gte`, `$in`, `$nin`, `$ne`). You can also use logical operators `$or`, `$and`, `$not` and `$where`. See below for the syntax. | ||
@@ -289,3 +289,3 @@ You can use regular expressions in two ways: in basic querying in place of a string, or with the `$regex` operator. | ||
#### Logical operators $or, $and, $not | ||
#### Logical operators $or, $and, $not, $where | ||
You can combine queries using logical operators: | ||
@@ -295,2 +295,3 @@ | ||
* For `$not`, the syntax is `{ $not: query }` | ||
* For `$where`, the syntax is `{ $where: function () { /* object is "this", return a boolean */ } }` | ||
@@ -306,2 +307,6 @@ ```javascript | ||
db.find({ $where: function () { return Object.keys(this) > 6; } }, function (err, docs) { | ||
// docs with more than 6 properties | ||
}); | ||
// You can mix normal queries, comparison queries and logical operators | ||
@@ -308,0 +313,0 @@ db.find({ $or: [{ planet: 'Earth' }, { planet: 'Mars' }], inhabited: true }, function (err, docs) { |
@@ -1175,2 +1175,32 @@ var model = require('../lib/model') | ||
describe('Comparison operator $where', function () { | ||
it('Function should match and not match correctly', function () { | ||
model.match({ a: 4}, { $where: function () { return this.a === 4; } }).should.equal(true); | ||
model.match({ a: 4}, { $where: function () { return this.a === 5; } }).should.equal(false); | ||
}); | ||
it('Should throw an error if the $where function is not, in fact, a function', function () { | ||
(function () { model.match({ a: 4 }, { $where: 'not a function' }); }).should.throw(); | ||
}); | ||
it('Should throw an error if the $where function returns a non-boolean', function () { | ||
(function () { model.match({ a: 4 }, { $where: function () { return 'not a boolean'; } }); }).should.throw(); | ||
}); | ||
it('Should be able to do the complex matching it must be used for', function () { | ||
var checkEmail = function() { | ||
if (!this.firstName || !this.lastName) { return false; } | ||
return this.firstName.toLowerCase() + "." + this.lastName.toLowerCase() + "@gmail.com" === this.email; | ||
}; | ||
model.match({ firstName: "John", lastName: "Doe", email: "john.doe@gmail.com" }, { $where: checkEmail }).should.equal(true); | ||
model.match({ firstName: "john", lastName: "doe", email: "john.doe@gmail.com" }, { $where: checkEmail }).should.equal(true); | ||
model.match({ firstName: "Jane", lastName: "Doe", email: "john.doe@gmail.com" }, { $where: checkEmail }).should.equal(false); | ||
model.match({ firstName: "John", lastName: "Deere", email: "john.doe@gmail.com" }, { $where: checkEmail }).should.equal(false); | ||
model.match({ lastName: "Doe", email: "john.doe@gmail.com" }, { $where: checkEmail }).should.equal(false); | ||
}); | ||
}); | ||
describe('Array fields', function () { | ||
@@ -1177,0 +1207,0 @@ |
700197
16470
610