Update README.md

pull/2/head
Louis Chatriot 12 years ago
parent 811098f90a
commit c796a511e0
  1. 34
      README.md

@ -118,18 +118,46 @@ The syntax is `{ field: { $op: value } }` where `$op` is any comparison operator
* `$ne`, `$nin`: not equal, not a member of * `$ne`, `$nin`: not equal, not a member of
```javascript ```javascript
// They work on numbers and strings (lexicographical order in that case) // $lt, $lte, $gt and $gte work on numbers and strings
db.find({ "humans.genders": { $gt: 5 } }, function (err, docs) { db.find({ "humans.genders": { $gt: 5 } }, function (err, docs) {
// docs contains Omicron Persei 8, whose humans have more than 5 genders (7). // 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" } }
}); });
// 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 // If a document's field is an array, matching it means matching any element of the array
db.find({ satellites: 'Phobos' }, function (err, docs) { db.find({ satellites: 'Phobos' }, function (err, docs) {
// docs contains Mars. Result would have been the same if query had been { satellites: 'Deimos' } // docs contains Mars. Result would have been the same if query had been { satellites: 'Deimos' }
}); });
// 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
});
// 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
```javascript
// You can use logical operator $or and $and ($and is the same // You can use logical operator $or and $and ($and is the same
// as just using a normal query object) // as just using a normal query object)
// Syntax is { $logicalOperator: [query1, query2, ...] } // Syntax is { $logicalOperator: [query1, query2, ...] }

Loading…
Cancel
Save