Socket
Socket
Sign inDemoInstall

nedb

Package Overview
Dependencies
Maintainers
1
Versions
95
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nedb - npm Package Compare versions

Comparing version 0.4.0 to 0.4.1

41

lib/model.js

@@ -306,3 +306,5 @@ /**

/**
* Arithmetic comparators
* Arithmetic and comparison operators
* @param {Native value} a Value in the object
* @param {Native value} b Value in the query
*/

@@ -325,5 +327,30 @@ comparisonFunctions.$lt = function (a, b) {

comparisonFunctions.$ne = function (a, b) {
if (!a) { return true; }
return !areThingsEqual(a, b);
};
comparisonFunctions.$in = function (a, b) {
var i;
if (!util.isArray(b)) { throw "$in operator called with a non-array"; }
for (i = 0; i < b.length; i += 1) {
if (areThingsEqual(a, b[i])) { return true; }
}
return false;
};
comparisonFunctions.$nin = function (a, b) {
if (!util.isArray(b)) { throw "$nin operator called with a non-array"; }
return !comparisonFunctions.$in(a, b);
};
/**
* Match any of the subqueries
* @param {Model} obj
* @param {Array of Queries} query
*/

@@ -345,2 +372,4 @@ logicalOperators.$or = function (obj, query) {

* Match all of the subqueries
* @param {Model} obj
* @param {Array of Queries} query
*/

@@ -361,2 +390,12 @@ logicalOperators.$and = function (obj, query) {

/**
* Inverted match of the query
* @param {Model} obj
* @param {Query} query
*/
logicalOperators.$not = function (obj, query) {
return !match(obj, query);
};
/**
* Tell if a given document matches a query

@@ -363,0 +402,0 @@ * @param {Object} obj Document to check

2

package.json
{
"name": "nedb",
"version": "0.4.0",
"version": "0.4.1",
"author": {

@@ -5,0 +5,0 @@ "name": "tldr.io",

@@ -69,4 +69,6 @@ # NeDB (Node embedded database)

### Finding documents
You can 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`). You can also use logical operators `$or` and `$and`. 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` and `$not`. See below for the syntax.
#### Basic querying
```javascript

@@ -100,2 +102,42 @@ // Let's say our datastore contains the following collection

// Find all documents in the collection
db.find({}, function (err, docs) {
});
// The same rules apply when you want to only find one document
db.findOne({ _id: 'id1' }, function (err, doc) {
// doc is the document Mars
// If no document is found, doc is null
});
```
#### Comparison operators ($lt, $lte, $gt, $gte, $in, $nin, $ne)
The syntax is `{ field: { $op: value } }` where `$op` is any comparison operator:
* `$lt`, `$lte`: less than, less than or equal
* `$gt`, `$gte`: greater than, greater than or equal
* `$in`: member of. `value` must be an array of values
* `$ne`, `$nin`: not equal, not a member of
```javascript
// $lt, $lte, $gt and $gte work on numbers and strings
db.find({ "humans.genders": { $gt: 5 } }, function (err, docs) {
// docs contains Omicron Persei 8, whose humans have more than 5 genders (7).
});
// When used with strings, lexicographical order is used
db.find({ planet: { $gt: 'Mercury' }}, function (err, docs) {
// docs contains Omicron Persei 8
})
// Using $in. $nin is used in the same way
db.find({ planet: { $in: ['Earth', 'Jupiter'] }}, function (err, docs) {
// docs contains Earth and Jupiter
});
```
#### Array fields
When a field in a document is an array, NeDB tries the query on every element and there is a match if at least one element matches.
```javascript
// If a document's field is an array, matching it means matching any element of the array

@@ -106,14 +148,20 @@ db.find({ satellites: 'Phobos' }, function (err, docs) {

// You can use comparison operators $lt (less than), $lte (less than or equal),
// $gt (greater than) and $gte (greater than or equal)
// They work on numbers and strings (lexicographical order in that case)
db.find({ "humans.genders": { $gt: 5 } }, function (err, docs) {
// docs contains Omicron Persei 8, whose humans have more than 5 genders (7).
// You can use comparison operators on array fields
// For example: { "human.genders": { $lte: "Deimot" } }
// This also works for queries that use comparison operators
db.find({ satellites: { $lt: 'Amos' } }, function (err, docs) {
// docs is empty since Phobos and Deimos are after Amos in lexicographical order
});
// You can use logical operator $or and $and ($and is the same
// as just using a normal query object)
// Syntax is { $logicalOperator: [query1, query2, ...] }
// This also works with the $in and $nin operator
db.find({ satellites: { $in: ['Moon', 'Deimos'] } }, function (err, docs) {
// docs contains Mars (the Earth document is not complete!)
});
```
#### Logical operators $or, $and, $not
You can combine queries using logical operators:
* For `$or` and `$and`, the syntax is `{ $op: [query1, query2, ...] }`.
* For `$not`, the syntax is `{ $not: query }`
```javascript
db.find({ $or: [{ planet: 'Earth' }, { planet: 'Mars' }] }, function (err, docs) {

@@ -123,2 +171,6 @@ // docs contains Earth and Mars

db.find({ $not: { planet: 'Earth' } }, function (err, docs) {
// docs contains Mars, Jupiter, Omicron Persei 8
});
// You can mix normal queries, comparison queries and logical operators

@@ -129,11 +181,2 @@ db.find({ $or: [{ planet: 'Earth' }, { planet: 'Mars' }], inhabited: true }, function (err, docs) {

// Find all documents in the collection
db.find({}, function (err, docs) {
});
// The same rules apply when you want to only find one document
db.findOne({ _id: 'id1' }, function (err, doc) {
// doc is the document Mars
// If no document is found, doc is null
});
```

@@ -140,0 +183,0 @@

@@ -462,3 +462,3 @@ var model = require('../lib/model')

// General behaviour is tested in the block about $lt. Here we just test operators work
describe('Other comparison operators: $lte, $gt, $gte', function () {
describe('Other comparison operators: $lte, $gt, $gte, $ne, $in', function () {

@@ -483,6 +483,35 @@ it('$lte', function () {

it('$ne', function () {
model.match({ a: 5 }, { a: { $ne: 4 } }).should.equal(true);
model.match({ a: 5 }, { a: { $ne: 5 } }).should.equal(false);
model.match({ a: 5 }, { b: { $ne: 5 } }).should.equal(true);
});
it('$in', function () {
model.match({ a: 5 }, { a: { $in: [6, 8, 9] } }).should.equal(false);
model.match({ a: 6 }, { a: { $in: [6, 8, 9] } }).should.equal(true);
model.match({ a: 7 }, { a: { $in: [6, 8, 9] } }).should.equal(false);
model.match({ a: 8 }, { a: { $in: [6, 8, 9] } }).should.equal(true);
model.match({ a: 9 }, { a: { $in: [6, 8, 9] } }).should.equal(true);
(function () { model.match({ a: 5 }, { a: { $in: 5 } }); }).should.throw();
});
it('$nin', function () {
model.match({ a: 5 }, { a: { $nin: [6, 8, 9] } }).should.equal(true);
model.match({ a: 6 }, { a: { $nin: [6, 8, 9] } }).should.equal(false);
model.match({ a: 7 }, { a: { $nin: [6, 8, 9] } }).should.equal(true);
model.match({ a: 8 }, { a: { $nin: [6, 8, 9] } }).should.equal(false);
model.match({ a: 9 }, { a: { $nin: [6, 8, 9] } }).should.equal(false);
// Matches if field doesn't exist
model.match({ a: 9 }, { b: { $nin: [6, 8, 9] } }).should.equal(true);
(function () { model.match({ a: 5 }, { a: { $in: 5 } }); }).should.throw();
});
});
describe('Logical operators $or, $and', function () {
describe('Logical operators $or, $and, $not', function () {

@@ -504,2 +533,7 @@ it('Any of the subqueries should match for an $or to match', function () {

it('Subquery should not match for a $not to match', function () {
model.match({ a: 5, b: 10 }, { a: 5 }).should.equal(true);
model.match({ a: 5, b: 10 }, { $not: { a: 5 } }).should.equal(false);
});
it('Logical operators are all top-level, only other logical operators can be above', function () {

@@ -506,0 +540,0 @@ (function () { model.match({ a: { b: 7 } }, { a: { $or: [ { b: 5 }, { b: 7 } ] } })}).should.throw();

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc