mongo-dot-notation
Advanced tools
Comparing version 1.2.0 to 2.0.0
263
lib/array.js
'use strict'; | ||
var util = require('util'); | ||
const field = require('./field'); | ||
const Operator = require('./operator'); | ||
var fields = require('./field'); | ||
var Operator = require('./operator'); | ||
// MongoDB Update field operators | ||
// https://docs.mongodb.com/manual/reference/operator/update-array/#array-update-operators | ||
module.exports.$ = function(x){ return new PositionalOperator(x); }; | ||
module.exports.$addToSet = function(x){ return AddToSetOperator.create(x); }; | ||
module.exports.$pop = function(x){ return new PopOperator().value(x || 1); }; | ||
module.exports.$pullAll = function(x){ return Operator.create('$pullAll', util.isArray(x) ? x : [x]); }; | ||
module.exports.$pull = function(x){ return Operator.create('$pull', util.isArray(x) ? { '$in': x } : x); }; | ||
module.exports.$pushAll = function(x){ return Operator.create('$pushAll', util.isArray(x) ? x : [x]); }; | ||
module.exports.$push = function(x){ return PushOperator.create(x); }; | ||
module.exports.$slice = function(x){ return PushOperator.create([]).$each().$slice(x); }; | ||
module.exports.$sort = function(x){ return PushOperator.create([]).$each().$sort(x); }; | ||
function AddToSetOperator(){ | ||
Operator.call(this, '$addToSet'); | ||
this._each = false; | ||
} | ||
util.inherits(AddToSetOperator, Operator); | ||
module.exports.$ = x => new PositionalOperator(x); | ||
module.exports.$addToSet = x => buildOperator(new AddToSetOperator(), x); | ||
module.exports.$pop = x => buildOperator(new PopOperator(), x || 1); | ||
module.exports.$pullAll = x => Operator.create('$pullAll', Array.isArray(x) ? x : [x]); | ||
module.exports.$pull = x => Operator.create('$pull', Array.isArray(x) ? { '$in': x } : x); | ||
module.exports.$push = x => buildOperator(new PushOperator(), x); | ||
module.exports.$slice = x => buildOperator(new PushOperator(), []).$each().$slice(x); | ||
module.exports.$sort = x => buildOperator(new PushOperator(), []).$each().$sort(x); | ||
AddToSetOperator.create = function(value){ | ||
var operator = new AddToSetOperator(); | ||
operator.value(value); | ||
function buildOperator(operator, val) { | ||
operator.value(val); | ||
return operator; | ||
}; | ||
} | ||
AddToSetOperator.prototype.$each = function(){ | ||
this._each = true; | ||
return this; | ||
}; | ||
class AddToSetOperator extends Operator { | ||
constructor() { | ||
super('$addToSet'); | ||
this._each = false; | ||
} | ||
AddToSetOperator.prototype.value = function(val){ | ||
if (typeof(val) === 'undefined' && this._each){ | ||
var result = Operator.prototype.value.call(this); | ||
return { '$each': util.isArray(result) ? result : [result] }; | ||
$each() { | ||
this._each = true; | ||
return this; | ||
} | ||
return Operator.prototype.value.call(this, val); | ||
}; | ||
function PopOperator(){ | ||
Operator.call(this, '$pop'); | ||
this._value = 1; | ||
value(val) { | ||
if (typeof(val) === 'undefined' && this._each){ | ||
const result = super.value(); | ||
return { '$each': Array.isArray(result) ? result : [result] }; | ||
} | ||
return super.value(val); | ||
} | ||
} | ||
util.inherits(PopOperator, Operator); | ||
PopOperator.prototype.first = function(){ | ||
return this.value(-1); | ||
}; | ||
PopOperator.prototype.last = function(){ | ||
return this.value(1); | ||
}; | ||
class PopOperator extends Operator { | ||
constructor() { | ||
super('$pop'); | ||
this._value = 1; | ||
} | ||
function PushOperator(){ | ||
Operator.call(this, '$push'); | ||
first() { | ||
return this.value(-1); | ||
} | ||
this._each = false; | ||
this._slice = null; | ||
this._sort = null; | ||
this._position = null; | ||
last() { | ||
return this.value(1); | ||
} | ||
} | ||
util.inherits(PushOperator, Operator); | ||
PushOperator.create = function(value){ | ||
var operator = new PushOperator(); | ||
operator.value(value); | ||
return operator; | ||
}; | ||
PushOperator.prototype.value = function(val){ | ||
if (typeof (val) !== 'undefined' || !this._each){ | ||
return Operator.prototype.value.call(this, val); | ||
class PushOperator extends Operator { | ||
constructor() { | ||
super('$push'); | ||
this._each = false; | ||
this._slice = null; | ||
this._sort = null; | ||
this._position = null; | ||
} | ||
var data = { | ||
'$each': typeof(this._value) === 'undefined' ? [] : | ||
(util.isArray(this._value) ? this._value : [this._value]) | ||
}; | ||
value(val){ | ||
if (typeof (val) !== 'undefined' || !this._each){ | ||
return super.value(val); | ||
} | ||
if (this._sort !== null) | ||
data['$sort'] = this._sort; | ||
if (this._slice !== null) | ||
data['$slice'] = this._slice; | ||
if (this._position !== null) | ||
data['$position'] = this._position; | ||
return data; | ||
}; | ||
const data = { | ||
'$each': typeof (this._value) === 'undefined' ? [] : | ||
(Array.isArray(this._value) ? this._value : [this._value]) | ||
}; | ||
PushOperator.prototype.$each = function(){ | ||
this._each = true; | ||
return this; | ||
}; | ||
if (this._sort !== null) { | ||
data['$sort'] = this._sort; | ||
} | ||
PushOperator.prototype.$slice = function(count){ | ||
if (!this._each) | ||
throw new Error('$slice operator is available only when using $each.'); | ||
if (typeof(count) !== 'undefined') | ||
this._slice = count; | ||
return this; | ||
}; | ||
if (this._slice !== null) { | ||
data['$slice'] = this._slice; | ||
} | ||
PushOperator.prototype.$sort = function(specification){ | ||
if (!this._each) | ||
throw new Error('$sort operator is available only when using $each.'); | ||
this._sort = typeof(specification) === 'undefined' ? 1 : specification; | ||
return this; | ||
}; | ||
if (this._position !== null) { | ||
data['$position'] = this._position; | ||
} | ||
PushOperator.prototype.$position = function(index){ | ||
if (!this._each) | ||
throw new Error('$position operator is available only when using $each.'); | ||
return data; | ||
} | ||
$each(){ | ||
this._each = true; | ||
return this; | ||
} | ||
$slice(count){ | ||
if (!this._each) { | ||
throw new Error('$slice operator is available only when using $each.'); | ||
} | ||
if (typeof (count) !== 'undefined') { | ||
this._slice = count; | ||
} | ||
return this; | ||
} | ||
if (typeof(index) !== 'undefined') | ||
this._position = index; | ||
return this; | ||
}; | ||
$sort(specification){ | ||
if (!this._each) { | ||
throw new Error('$sort operator is available only when using $each.'); | ||
} | ||
function PositionalOperator(field){ | ||
if (typeof(field) === 'number' || field && /\d+/.test(field)) | ||
Operator.call(this, field + ''); | ||
else | ||
Operator.call(this, '$' + (field ? '.' + field : '')); | ||
this._sort = typeof(specification) === 'undefined' ? 1 : specification; | ||
return this; | ||
} | ||
$position(index){ | ||
if (!this._each) { | ||
throw new Error('$position operator is available only when using $each.'); | ||
} | ||
if (typeof (index) !== 'undefined') { | ||
this._position = index; | ||
} | ||
return this; | ||
} | ||
} | ||
util.inherits(PositionalOperator, Operator); | ||
class PositionalOperator extends Operator { | ||
constructor(field) { | ||
if (typeof (field) === 'number' || field && /\d+/.test(field)) { | ||
super(field + ''); | ||
} else { | ||
super('$' + (field ? '.' + field : '')); | ||
} | ||
} | ||
value(val){ | ||
if (typeof(val) === 'undefined'){ | ||
if (!this._operator) { | ||
throw Error('Value is mandatory for positional operator.'); | ||
} | ||
return this._operator; | ||
} | ||
if (Operator.isOperator(val)){ | ||
if (!isValidPositionalOperator(val)) | ||
throw new Error(val.name + ' operator is not supported with $ positional operator.'); | ||
this._operator = val; | ||
} | ||
else{ | ||
this._operator = field.$set(val); | ||
} | ||
return this; | ||
} | ||
} | ||
PositionalOperator._SupportedOperators = | ||
['$inc', '$mul', '$set', '$unset', '$min', '$max', '$currentDate', '$timestamp']; | ||
PositionalOperator._SupportedOperators.forEach(function(name){ | ||
PositionalOperator._SupportedOperators.forEach((name) => { | ||
PositionalOperator.prototype[name] = function(){ | ||
return this.value(fields[name].apply(null, arguments)); | ||
return this.value(field[name].apply(null, arguments)); | ||
}; | ||
}); | ||
PositionalOperator.prototype.value = function(val){ | ||
if (typeof(val) === 'undefined'){ | ||
if (!this._operator) | ||
throw Error('Value is mandatory for positional operator.'); | ||
return this._operator; | ||
} | ||
if (Operator.isOperator(val)){ | ||
if (!isValidPositionalOperator(val)) | ||
throw new Error(val.name + ' operator is not supported with $ positional operator.'); | ||
this._operator = val; | ||
} | ||
else{ | ||
this._operator = fields.$set(val); | ||
} | ||
return this; | ||
}; | ||
function isValidPositionalOperator(operator){ | ||
return PositionalOperator._SupportedOperators.indexOf(operator.name) > -1; | ||
} |
'use strict'; | ||
var util = require('util'); | ||
var Operator = require('./operator'); | ||
const Operator = require('./operator'); | ||
// MongoDB Update field operators | ||
// https://docs.mongodb.com/manual/reference/operator/update/#bitwise | ||
module.exports.$bit = function(){ return new BitOperator(); }; | ||
@@ -13,37 +13,39 @@ module.exports.$and = function(x){ return new BitOperator().$and(x); }; | ||
function BitwiseOperator(operation){ | ||
Operator.call(this, '$bit'); | ||
this._operation = operation; | ||
} | ||
util.inherits(BitwiseOperator, Operator); | ||
class BitwiseOperator extends Operator { | ||
constructor(operation) { | ||
super('$bit'); | ||
this._operation = operation; | ||
} | ||
BitwiseOperator.prototype.value = function(val){ | ||
if (typeof (val) !== 'undefined'){ | ||
return Operator.prototype.value.call(this, val); | ||
value(val){ | ||
if (typeof (val) !== 'undefined'){ | ||
return super.value(val); | ||
} | ||
const result = {}; | ||
result[this._operation] = super.value(); | ||
return result; | ||
} | ||
var result = {}; | ||
result[this._operation] = Operator.prototype.value.call(this); | ||
return result; | ||
}; | ||
function BitOperator(){ | ||
Operator.call(this, '$bit'); | ||
} | ||
util.inherits(BitOperator, Operator); | ||
BitOperator.prototype.value = function(){ | ||
throw new Error('Value not supported when bitwise operation not set. Use $and, $or or $xor.'); | ||
}; | ||
class BitOperator extends Operator { | ||
constructor() { | ||
super('$bit'); | ||
} | ||
BitOperator.prototype.$and = function(value){ | ||
return new BitwiseOperator('and').value(value); | ||
}; | ||
value() { | ||
throw new Error('Value not supported when bitwise operation not set. Use $and, $or or $xor.'); | ||
} | ||
BitOperator.prototype.$or = function(value){ | ||
return new BitwiseOperator('or').value(value); | ||
}; | ||
$and(value){ | ||
return new BitwiseOperator('and').value(value); | ||
} | ||
BitOperator.prototype.$xor = function(value){ | ||
return new BitwiseOperator('xor').value(value); | ||
}; | ||
$or(value){ | ||
return new BitwiseOperator('or').value(value); | ||
} | ||
$xor(value){ | ||
return new BitwiseOperator('xor').value(value); | ||
} | ||
} |
'use strict'; | ||
var Operator = require('./operator'); | ||
const Operator = require('./operator'); | ||
// MongoDB Update field operators | ||
// https://docs.mongodb.com/manual/reference/operator/update/#fields | ||
module.exports.$set = function(value){ return Operator.create('$set', value); }; | ||
module.exports.$inc = function(value){ return Operator.create('$inc', value, 1); }; | ||
module.exports.$max = function(value){ return Operator.create('$max', value); }; | ||
module.exports.$min = function(value){ return Operator.create('$min', value); }; | ||
module.exports.$mul = function(value){ return Operator.create('$mul', value, 1); }; | ||
module.exports.$rename = function(field){ return Operator.create('$rename', field); }; | ||
module.exports.$setOnInsert = function(value){ return Operator.create('$setOnInsert', value); }; | ||
module.exports.$unset = function(){ return Operator.create('$unset', ''); }; | ||
module.exports.$currentDate = function(type) { return Operator.create('$currentDate', {$type: type || 'date'}); }; | ||
module.exports.$timestamp = function(){ return Operator.create('$currentDate', {$type: 'timestamp'}); }; | ||
module.exports.$set = value => Operator.create('$set', value); | ||
module.exports.$inc = value => Operator.create('$inc', value, 1); | ||
module.exports.$max = value => Operator.create('$max', value); | ||
module.exports.$min = value => Operator.create('$min', value); | ||
module.exports.$mul = value => Operator.create('$mul', value, 1); | ||
module.exports.$rename = field => Operator.create('$rename', field); | ||
module.exports.$setOnInsert = value => Operator.create('$setOnInsert', value); | ||
module.exports.$unset = () => Operator.create('$unset', ''); | ||
module.exports.$currentDate = type => Operator.create('$currentDate', {$type: type || 'date'}); | ||
module.exports.$timestamp = () => Operator.create('$currentDate', { $type: 'timestamp' }); |
'use strict'; | ||
var util = require('util'); | ||
var Operator = require('./operator'); | ||
const Operator = require('./operator'); | ||
var PrimitiveTypes = [ | ||
const PrimitiveTypes = [ | ||
'number', | ||
@@ -13,3 +12,3 @@ 'string', | ||
var BsonTypes = [ | ||
const BsonTypes = [ | ||
'Binary', | ||
@@ -24,2 +23,3 @@ 'Code', | ||
'MinKey', | ||
'ObjectId', | ||
'ObjectID', | ||
@@ -42,7 +42,7 @@ 'BSONRegExp', | ||
if (Operator.isOperator(propertyValue)) { | ||
return build(updateData, propertyValue.name, | ||
propertyName, propertyValue.value()); | ||
return build(updateData, propertyValue.name, | ||
propertyName, propertyValue.value()); | ||
} | ||
var keys = Object.keys(propertyValue); | ||
const keys = Object.keys(propertyValue); | ||
if (!keys.length) { | ||
@@ -53,5 +53,5 @@ return propertyName ? | ||
for(var i = 0; i < keys.length; i++){ | ||
var key = keys[i]; | ||
var newPrefix = !propertyName ? key : (propertyName + '.' + key); | ||
for(let i = 0, n = keys.length; i < n; i++){ | ||
const key = keys[i]; | ||
const newPrefix = !propertyName ? key : (propertyName + '.' + key); | ||
flatten(updateData, newPrefix, propertyValue[key]); | ||
@@ -82,4 +82,4 @@ } | ||
return PrimitiveTypes.indexOf(typeof (value)) > -1 || | ||
util.isArray(value) || | ||
util.isDate(value); | ||
Array.isArray(value) || | ||
value instanceof Date; | ||
} | ||
@@ -86,0 +86,0 @@ |
'use strict'; | ||
module.exports = Operator; | ||
class Operator{ | ||
constructor(name) { | ||
if (!name) | ||
throw new Error('Null argument error: name'); | ||
this.name = name; | ||
} | ||
function Operator(name){ | ||
if (!name) | ||
throw new Error('Null argument error: name'); | ||
this.name = name; | ||
value(val){ | ||
if (typeof(val) === 'undefined') | ||
return this._value; | ||
this._value = val; | ||
return this; | ||
} | ||
} | ||
Operator.prototype.value = function(val){ | ||
if (typeof(val) === 'undefined') | ||
return this._value; | ||
this._value = val; | ||
return this; | ||
Operator.isOperator = obj => obj instanceof Operator; | ||
Operator.create = (name, value, defaultValue) => { | ||
const operator = new Operator(name); | ||
operator.value(typeof(value) === 'undefined' ? defaultValue : value); | ||
return operator; | ||
}; | ||
Operator.isOperator = function(obj){ return obj && obj instanceof Operator; }; | ||
Operator.create = function(name, value, defaultValue){ | ||
var operator = new Operator(name); | ||
operator.value(typeof(value) === 'undefined' ? defaultValue : value); | ||
return operator; | ||
}; | ||
module.exports = Operator; |
{ | ||
"name": "mongo-dot-notation", | ||
"version": "1.2.0", | ||
"version": "2.0.0", | ||
"description": "Transform objects to mongo update instructions", | ||
@@ -34,15 +34,14 @@ "author": { | ||
"test": "mocha", | ||
"test-ci": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly", | ||
"ci": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly", | ||
"coverage": "istanbul cover node_modules/mocha/bin/_mocha --report html", | ||
"lint": "eslint lib", | ||
"perf": "node ./test/performance.js" | ||
"lint": "eslint lib" | ||
}, | ||
"devDependencies": { | ||
"chai": "^4.0.1", | ||
"eslint": "^3.19.0", | ||
"chai": "^4.2.0", | ||
"eslint": "^6.8.0", | ||
"istanbul": "^0.4.5", | ||
"mocha": "^3.4.2", | ||
"mocha": "^6.2.2", | ||
"mocha-lcov-reporter": "^1.3.0", | ||
"mongodb": "^2.2.28" | ||
"mongodb": "^3.4.1" | ||
} | ||
} |
242
README.md
@@ -10,5 +10,5 @@ mongo-dot-notation | ||
```javascript | ||
var $ = require('mongo-dot-notation') | ||
const $ = require('mongo-dot-notation') | ||
var instructions = $.flatten({ | ||
const instructions = $.flatten({ | ||
firstName: 'John', | ||
@@ -20,3 +20,3 @@ contact: { phone: '874-478-1254' }, | ||
/* | ||
var instructions = { | ||
const instructions = { | ||
$currentDate: { | ||
@@ -33,80 +33,77 @@ lastUpdate: { $type: 'date' } | ||
## Installation | ||
## Features | ||
```bash | ||
$ npm install mongo-dot-notation --save | ||
``` | ||
- Transform objects to `mongo` update instructions | ||
- supports embedded documents | ||
- supports mongo types (`ObjectID`, `Int32` etc.) | ||
- Full support of `mongo` update operators | ||
- `Field` update operators | ||
- `Array` update operators | ||
- `Bitwise` update operators | ||
- Supports Node.js v8 and above | ||
- No `npm` dependencies on `mongo` | ||
## Features | ||
* Transform objects to mongo update instructions | ||
* supports embedded documents | ||
* understands mongo types (ObjectID, Int32 etc.) | ||
* Full support of mongo update operators | ||
* Field update operators | ||
* Array update operators | ||
* Bitwise update operators | ||
* Compatible with node >= 0.12 | ||
* No *npm* dependencies on mongo | ||
## Usage | ||
## Usage | ||
Using `$.flatten` and operators to transform to mongo update instructions. | ||
```javascript | ||
var $ = require('mongo-dot-notation') | ||
const $ = require('mongo-dot-notation') | ||
var MongoClient = require('mongodb').MongoClient | ||
var url = 'mongodb://localhost:27017/mydatabase' | ||
const MongoClient = require('mongodb').MongoClient | ||
const url = 'mongodb://localhost:27017/db' | ||
const users = (await MongoClient.connect(url)).db.collection('users') | ||
MongoClient.connect(url).then(function(db) { | ||
return db.collection('users').update( | ||
{ _id: 1 }, | ||
$.flatten({ | ||
comments: $.$push('Logged in.').$each().$slice(-100), | ||
env: 'demo', | ||
login: { | ||
date: $.$currentDate() | ||
}, | ||
analytics: { | ||
visits: $.$inc() | ||
}, | ||
account: { | ||
createdOn: $.$setOnInsert(new Date()), | ||
blocked: $.$unset(), | ||
attempts: 0, | ||
logins: $.$inc() | ||
} | ||
}) | ||
}) | ||
const updateData = { | ||
comments: $.$push('Logged in.').$each().$slice(-100), | ||
env: 'demo', | ||
login: { | ||
date: $.$currentDate() | ||
}, | ||
analytics: { | ||
visits: $.$inc() | ||
}, | ||
account: { | ||
createdOn: $.$setOnInsert(new Date()), | ||
blocked: $.$unset(), | ||
attempts: 0, | ||
logins: $.$inc() | ||
} | ||
} | ||
await users.update({ _id: 1 }, $.flatten(updateData)) | ||
``` | ||
Without `mongo-dot-notation` update instructions should look like: | ||
``` javascript | ||
... | ||
return collection.update( | ||
{ _id: 1 }, | ||
{ | ||
$set: { | ||
'env': 'demo', | ||
'account.attempts': 0 | ||
}, | ||
$push: { | ||
'comments': { | ||
'$each': ['Logged in.'], | ||
'$slice': -100 | ||
} | ||
} | ||
$currentDate: { | ||
'login.date': 'date' | ||
}, | ||
$inc: { | ||
'analytics.visits': 1, | ||
'account.logins': 1, | ||
}, | ||
$unset: { | ||
'account.blocked': '' | ||
}, | ||
$setOnInsert: { | ||
'account.createdOn': new Date() | ||
} | ||
}) | ||
}) | ||
```javascript | ||
// ... | ||
const updateData = { | ||
$set: { | ||
'env': 'demo', | ||
'account.attempts': 0 | ||
}, | ||
$push: { | ||
'comments': { | ||
'$each': ['Logged in.'], | ||
'$slice': -100 | ||
} | ||
} | ||
$currentDate: { | ||
'login.date': 'date' | ||
}, | ||
$inc: { | ||
'analytics.visits': 1, | ||
'account.logins': 1, | ||
}, | ||
$unset: { | ||
'account.blocked': '' | ||
}, | ||
$setOnInsert: { | ||
'account.createdOn': new Date() | ||
} | ||
} | ||
await users.update({ _id: 1 }, updateData) | ||
``` | ||
@@ -116,12 +113,12 @@ | ||
To run the test suite make sure you have mongo 2.6+ installed locally on the default port (*27017*). | ||
Mongo is used to run integration tests. | ||
Once mongo is available, install the dependencies, then run `npm test`: | ||
To run the tests make sure you have mongo 3.0+ installed locally on the default port (*27017*). | ||
`mongo` is used to run integration tests. | ||
Once `mongo` is available, install the dependencies, then run `npm run test`: | ||
```bash | ||
$ npm install | ||
$ npm test | ||
$ npm run test | ||
``` | ||
To calculate code coverage run `npm run-script test-ci`. | ||
To calculate code coverage run `npm run coverage`. | ||
@@ -133,4 +130,4 @@ ## API | ||
```javascript | ||
var $ = require('mongo-dot-notation') | ||
var instructions = $.flatten({ | ||
const $ = require('mongo-dot-notation') | ||
const instructions = $.flatten({ | ||
account: { | ||
@@ -146,3 +143,3 @@ name: 'hero' | ||
```javascript | ||
var $ = require('mongo-dot-notation') | ||
const $ = require('mongo-dot-notation') | ||
@@ -165,3 +162,3 @@ $.isOperator(1) // false | ||
```javascript | ||
var $ = require('mongo-dot-notation') | ||
const $ = require('mongo-dot-notation') | ||
$.flatten({ | ||
@@ -178,3 +175,3 @@ visits: $.$inc(5) // increment current visits value by 5 | ||
```javascript | ||
var $ = require('mongo-dot-notation') | ||
const $ = require('mongo-dot-notation') | ||
$.flatten({ | ||
@@ -191,3 +188,3 @@ price: $.$mul(0.75) // multiply current price value by 0.75 | ||
```javascript | ||
var $ = require('mongo-dot-notation') | ||
const $ = require('mongo-dot-notation') | ||
$.flatten({ | ||
@@ -204,3 +201,3 @@ nmae: $.$rename('name') // rename nmae field to name | ||
```javascript | ||
var $ = require('mongo-dot-notation') | ||
const $ = require('mongo-dot-notation') | ||
@@ -222,3 +219,3 @@ db.collection('users').update( | ||
```javascript | ||
var $ = require('mongo-dot-notation') | ||
const $ = require('mongo-dot-notation') | ||
$.flatten({ | ||
@@ -232,3 +229,3 @@ name: $.$set('Mike') | ||
```javascript | ||
var $ = require('mongo-dot-notation') | ||
const $ = require('mongo-dot-notation') | ||
$.flatten({ | ||
@@ -251,3 +248,3 @@ name: 'Mike', | ||
```javascript | ||
var $ = require('mongo-dot-notation') | ||
const $ = require('mongo-dot-notation') | ||
$.flatten({ | ||
@@ -274,3 +271,3 @@ name: 'Mike', | ||
```javascript | ||
var $ = require('mongo-dot-notation') | ||
const $ = require('mongo-dot-notation') | ||
$.flatten({ | ||
@@ -288,3 +285,3 @@ comments: $.$unset(), // remove field from document | ||
```javascript | ||
var $ = require('mongo-dot-notation') | ||
const $ = require('mongo-dot-notation') | ||
$.flatten({ | ||
@@ -301,3 +298,3 @@ low: $.$min(200) // update low to 200 if current low value is greater than 200 | ||
```javascript | ||
var $ = require('mongo-dot-notation') | ||
const $ = require('mongo-dot-notation') | ||
$.flatten({ | ||
@@ -315,3 +312,3 @@ high: $.$max(450) // update high to 450 if current high value is less than 450 | ||
```javascript | ||
var $ = require('mongo-dot-notation') | ||
const $ = require('mongo-dot-notation') | ||
$.flatten({ | ||
@@ -325,3 +322,3 @@ lastUpdate: $.$currentDate() | ||
```javascript | ||
var $ = require('mongo-dot-notation') | ||
const $ = require('mongo-dot-notation') | ||
$.flatten({ | ||
@@ -335,3 +332,3 @@ lastUpdate: $.$currentDate('timestamp') | ||
```javascript | ||
var $ = require('mongo-dot-notation') | ||
const $ = require('mongo-dot-notation') | ||
$.flatten({ | ||
@@ -349,3 +346,3 @@ lastUpdate: $.$timestamp() | ||
```javascript | ||
var $ = require('mongo-dot-notation') | ||
const $ = require('mongo-dot-notation') | ||
db.students.update( | ||
@@ -363,3 +360,3 @@ { _id: 1, grades: 80 }, // match all elements from grades array where value equals to 80 | ||
```javascript | ||
var $ = require('mongo-dot-notation') | ||
const $ = require('mongo-dot-notation') | ||
db.students.update( | ||
@@ -377,7 +374,7 @@ { _id: 1, 'grades.grade': 80 }, | ||
```javascript | ||
var $ = require('mongo-dot-notation') | ||
const $ = require('mongo-dot-notation') | ||
db.students.update( | ||
{ _id: 1, grades: 80 }, | ||
$.flatten({ | ||
grades: $.$().$mul(0.1) //multiplies matched array element by 0.1 | ||
grades: $.$().$mul(0.1) // multiplies matched array element by 0.1 | ||
}) | ||
@@ -391,3 +388,3 @@ | ||
```javascript | ||
var $ = require('mongo-dot-notation') | ||
const $ = require('mongo-dot-notation') | ||
$.flatten({ | ||
@@ -400,3 +397,3 @@ grades: $.$(0).$set(100) | ||
```javascript | ||
var $ = require('mongo-dot-notation') | ||
const $ = require('mongo-dot-notation') | ||
$.flatten({ | ||
@@ -413,3 +410,3 @@ months: $.$('5.avgTemp').$set(25.7) | ||
```javascript | ||
var $ = require('mongo-dot-notation') | ||
const $ = require('mongo-dot-notation') | ||
$.flatten({ | ||
@@ -423,3 +420,3 @@ values: $.$addToSet(5) | ||
```javascript | ||
var $ = require('mongo-dot-notation') | ||
const $ = require('mongo-dot-notation') | ||
$.flatten({ | ||
@@ -436,3 +433,3 @@ values: $.$addToSet([7, 1, 4]).$each() | ||
```javascript | ||
var $ = require('mongo-dot-notation') | ||
const $ = require('mongo-dot-notation') | ||
$.flatten({ | ||
@@ -446,3 +443,3 @@ values: $.$pop() // removes by default last element | ||
```javascript | ||
var $ = require('mongo-dot-notation') | ||
const $ = require('mongo-dot-notation') | ||
$.flatten({ | ||
@@ -456,3 +453,3 @@ values: $.$pop(-1) | ||
```javascript | ||
var $ = require('mongo-dot-notation') | ||
const $ = require('mongo-dot-notation') | ||
$.flatten({ | ||
@@ -470,3 +467,3 @@ indexes: $.$pop().first(), | ||
```javascript | ||
var $ = require('mongo-dot-notation') | ||
const $ = require('mongo-dot-notation') | ||
$.flatten({ | ||
@@ -483,3 +480,3 @@ values: $.$pullAll([0, 1]) | ||
```javascript | ||
var $ = require('mongo-dot-notation') | ||
const $ = require('mongo-dot-notation') | ||
$.flatten({ | ||
@@ -493,3 +490,3 @@ values: $.$pull(7) | ||
```javascript | ||
var $ = require('mongo-dot-notation') | ||
const $ = require('mongo-dot-notation') | ||
$.flatten({ | ||
@@ -503,13 +500,2 @@ values: $.$pull([0, 1]) | ||
#### `.$pushAll` | ||
See mongo [**$pushAll**](https://docs.mongodb.com/manual/reference/operator/update/pushAll/). | ||
The `$pushAll` operator appends the specified values to an array. (*Note that this operator is deprecated since mongo 2.4.*) | ||
```javascript | ||
var $ = require('mongo-dot-notation') | ||
$.flatten({ | ||
values: $.$pushAll([1, 2, 3]) | ||
}) | ||
// { '$pushAll': { 'values': [1, 2, 3] } } | ||
``` | ||
#### `.$push` | ||
@@ -520,3 +506,3 @@ See mongo [**$push**](https://docs.mongodb.com/manual/reference/operator/update/push/). | ||
```javascript | ||
var $ = require('mongo-dot-notation') | ||
const $ = require('mongo-dot-notation') | ||
$.flatten({ | ||
@@ -530,3 +516,3 @@ grades: $.$push({ grade: 'A' }) | ||
```javascript | ||
var $ = require('mongo-dot-notation') | ||
const $ = require('mongo-dot-notation') | ||
$.flatten({ | ||
@@ -547,3 +533,3 @@ grades: $.$push([{ grade: 'A' }, { grade: 'B' }]).$each() | ||
```javascript | ||
var $ = require('mongo-dot-notation') | ||
const $ = require('mongo-dot-notation') | ||
@@ -567,3 +553,3 @@ // insert as a first element in the array | ||
```javascript | ||
var $ = require('mongo-dot-notation') | ||
const $ = require('mongo-dot-notation') | ||
@@ -587,3 +573,3 @@ // insert the element and limit to last 10 values | ||
```javascript | ||
var $ = require('mongo-dot-notation') | ||
const $ = require('mongo-dot-notation') | ||
@@ -607,3 +593,3 @@ // insert the element and sorts descending by grade | ||
```javascript | ||
var $ = require('mongo-dot-notation') | ||
const $ = require('mongo-dot-notation') | ||
@@ -629,3 +615,3 @@ // insert the element, sorts descending by grade | ||
```javascript | ||
var $ = require('mongo-dot-notation') | ||
const $ = require('mongo-dot-notation') | ||
@@ -647,3 +633,3 @@ $.flatten({ | ||
```javascript | ||
var $ = require('mongo-dot-notation') | ||
const $ = require('mongo-dot-notation') | ||
$.flatten({ | ||
@@ -666,3 +652,3 @@ owner: $.$bit().$and(7) // performs a bitwise AND | ||
```javascript | ||
var $ = require('mongo-dot-notation') | ||
const $ = require('mongo-dot-notation') | ||
$.flatten({ | ||
@@ -676,4 +662,2 @@ owner: $.$and(7), // same as $.$bit().$and(7) | ||
## License | ||
[MIT](LICENSE) | ||
Copyright © 2015-2017 Dumitru Deveatii | ||
[MIT](LICENSE) |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
28691
10
295
620