Comparing version 1.7.2 to 1.7.3
@@ -625,3 +625,16 @@ /** | ||
}; | ||
comparisonFunctions.$elemMatch = function (obj, value) { | ||
if (!util.isArray(obj)) { return false; } | ||
var i = obj.length; | ||
var result = false; // Initialize result | ||
while (i--) { | ||
if (match(obj[i], value)) { // If match for array element, return true | ||
result = true; | ||
break; | ||
} | ||
} | ||
return result; | ||
}; | ||
arrayComparisonFunctions.$size = true; | ||
arrayComparisonFunctions.$elemMatch = true; | ||
@@ -628,0 +641,0 @@ |
{ | ||
"name": "nedb", | ||
"version": "1.7.2", | ||
"version": "1.7.3", | ||
"author": { | ||
@@ -5,0 +5,0 @@ "name": "Louis Chatriot", |
@@ -274,4 +274,7 @@ <img src="http://i.imgur.com/9O1xHFb.png" style="width: 25%; height: 25%; float: left;"> | ||
#### Array fields | ||
When a field in a document is an array, NeDB first tries to see if the query value is an array to perform an exact match, then whether there is an array-specific comparison function (for now there is only `$size`) being used. If not, the query is treated as a query on every element and there is a match if at least one element matches. | ||
When a field in a document is an array, NeDB first tries to see if the query value is an array to perform an exact match, then whether there is an array-specific comparison function (for now there is only `$size` and `$elemMatch`) being used. If not, the query is treated as a query on every element and there is a match if at least one element matches. | ||
* `$size`: match on the size of the array | ||
* `$elemMatch`: matches if at least one array element matches the query entirely | ||
```javascript | ||
@@ -287,2 +290,16 @@ // Exact match | ||
// Using an array-specific comparison function | ||
// $elemMatch operator will provide match for a document, if an element from the array field satisfies all the conditions specified with the `$elemMatch` operator | ||
db.find({ completeData: { planets: { $elemMatch: { name: 'Earth', number: 3 } } } }, function (err, docs) { | ||
// docs contains documents with id 5 (completeData) | ||
}); | ||
db.find({ completeData: { planets: { $elemMatch: { name: 'Earth', number: 5 } } } }, function (err, docs) { | ||
// docs is empty | ||
}); | ||
// You can use inside #elemMatch query any known document query operator | ||
db.find({ completeData: { planets: { $elemMatch: { name: 'Earth', number: { $gt: 2 } } } } }, function (err, docs) { | ||
// docs contains documents with id 5 (completeData) | ||
}); | ||
// Note: you can't use nested comparison functions, e.g. { $size: { $lt: 5 } } will throw an error | ||
@@ -669,2 +686,4 @@ db.find({ satellites: { $size: 2 } }, function (err, docs) { | ||
* Do not build the browser version in your branch, I'll take care of it once the code is merged. | ||
* Update the readme accordingly. | ||
* Last but not least: keep in mind what NeDB's mindset is! The goal is not to be a replacement for MongoDB, but to have a pure JS database, easy to use, cross platform, fast and expressive enough for the target projects (small and self contained apps on server/desktop/browser/mobile). Sometimes it's better to shoot for simplicity than for API completeness with regards to MongoDB. | ||
@@ -671,0 +690,0 @@ ## Bug reporting guidelines |
@@ -1219,2 +1219,26 @@ var model = require('../lib/model') | ||
it('Can query array documents with multiple simultaneous conditions', function () { | ||
// Non nested documents | ||
model.match({ childrens: [ { name: "Huey", age: 3 }, { name: "Dewey", age: 7 }, { name: "Louie", age: 12 } ] }, { "childrens": { $elemMatch: { name: "Dewey", age: 7 } } }).should.equal(true); | ||
model.match({ childrens: [ { name: "Huey", age: 3 }, { name: "Dewey", age: 7 }, { name: "Louie", age: 12 } ] }, { "childrens": { $elemMatch: { name: "Dewey", age: 12 } } }).should.equal(false); | ||
model.match({ childrens: [ { name: "Huey", age: 3 }, { name: "Dewey", age: 7 }, { name: "Louie", age: 12 } ] }, { "childrens": { $elemMatch: { name: "Louie", age: 3 } } }).should.equal(false); | ||
// Nested documents | ||
model.match({ outer: { childrens: [ { name: "Huey", age: 3 }, { name: "Dewey", age: 7 }, { name: "Louie", age: 12 } ] } }, { "outer.childrens": { $elemMatch: { name: "Dewey", age: 7 } } }).should.equal(true); | ||
model.match({ outer: { childrens: [ { name: "Huey", age: 3 }, { name: "Dewey", age: 7 }, { name: "Louie", age: 12 } ] } }, { "outer.childrens": { $elemMatch: { name: "Dewey", age: 12 } } }).should.equal(false); | ||
model.match({ outer: { childrens: [ { name: "Huey", age: 3 }, { name: "Dewey", age: 7 }, { name: "Louie", age: 12 } ] } }, { "outer.childrens": { $elemMatch: { name: "Louie", age: 3 } } }).should.equal(false); | ||
}); | ||
it('$elemMatch operator works with empty arrays', function () { | ||
model.match({ childrens: [] }, { "childrens": { $elemMatch: { name: "Mitsos" } } }).should.equal(false); | ||
model.match({ childrens: [] }, { "childrens": { $elemMatch: {} } }).should.equal(false); | ||
}); | ||
it('Can use more complex comparisons inside nested query documents', function () { | ||
model.match({ childrens: [ { name: "Huey", age: 3 }, { name: "Dewey", age: 7 }, { name: "Louie", age: 12 } ] }, { "childrens": { $elemMatch: { name: "Dewey", age: { $gt: 6, $lt: 8 } } } }).should.equal(true); | ||
model.match({ childrens: [ { name: "Huey", age: 3 }, { name: "Dewey", age: 7 }, { name: "Louie", age: 12 } ] }, { "childrens": { $elemMatch: { name: "Dewey", age: { $in: [ 6, 7, 8 ] } } } } ).should.equal(true); | ||
model.match({ childrens: [ { name: "Huey", age: 3 }, { name: "Dewey", age: 7 }, { name: "Louie", age: 12 } ] }, { "childrens": { $elemMatch: { name: "Dewey", age: { $gt: 6, $lt: 7 } } } }).should.equal(false); | ||
model.match({ childrens: [ { name: "Huey", age: 3 }, { name: "Dewey", age: 7 }, { name: "Louie", age: 12 } ] }, { "childrens": { $elemMatch: { name: "Louie", age: { $gt: 6, $lte: 7 } } } }).should.equal(false); | ||
}); | ||
}); | ||
@@ -1221,0 +1245,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1285203
28802
701