README
mongo-dot-notation
Fast, lightweight library to transform objects to mongo update instructions using operators.
const $ = require('mongo-dot-notation')
const instructions = $.flatten({
firstName: 'John',
contact: { phone: '874-478-1254' },
lastUpdate: $.$currentDate()
})
/*
const instructions = {
$currentDate: {
lastUpdate: { $type: 'date' }
},
$set: {
'firstName': 'John',
'contact.phone': '874-478-1254'
}
}
*/
Features
- Transform objects to
mongo
update instructions- supports embedded documents
- supports mongo types (
ObjectID
,Int32
etc.)
- Full support of
mongo
update operatorsField
update operatorsArray
update operatorsBitwise
update operators
- Supports Node.js v8 and above
- No
npm
dependencies onmongo
Usage
Using $.flatten
and operators to transform to mongo update instructions.
const $ = require('mongo-dot-notation')
const MongoClient = require('mongodb').MongoClient
const url = 'mongodb://localhost:27017/db'
const users = (await MongoClient.connect(url)).db.collection('users')
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:
// ...
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)
Tests
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
:
$ npm install
$ npm run test
To calculate code coverage run npm run coverage
.
API
.flatten()
Use .flatten()
to transform objects:
const $ = require('mongo-dot-notation')
const instructions = $.flatten({
account: {
name: 'hero'
}
})
// { '$set': { 'account.name': 'hero' } }
.isOperator()
Checks if a given value is a mongo-dot-notation
operator:
const $ = require('mongo-dot-notation')
$.isOperator(1) // false
$.isOperator({}) // false
$.isOperator($.$set(10)) // true
See below the list of all supported mongo update opertors.
Mongo update operators
Field update operators
.$inc
See mongo $inc.
The $inc
operator increments a field by a specified value. If no value is provided, defaults to 1.
const $ = require('mongo-dot-notation')
$.flatten({
visits: $.$inc(5) // increment current visits value by 5
})
// { '$inc': { 'visits': 5 } }
.$mul
See mongo $mul.
Multiplies the value of a field by a number. (Supported in mongo >= 2.6)
const $ = require('mongo-dot-notation')
$.flatten({
price: $.$mul(0.75) // multiply current price value by 0.75
})
// { '$mul': { 'price': 0.75 } }
.$rename
See mongo $rename.
The $rename
operator updates the name of a field.
const $ = require('mongo-dot-notation')
$.flatten({
nmae: $.$rename('name') // rename nmae field to name
})
// { '$rename': { 'nmae': 'name' } }
.$setOnInsert
See mongo $setOnInsert.
Assigns value to field only when the document is inserted (when an update operation is with upsert:true
). (Supported in mongo >= 2.4)
const $ = require('mongo-dot-notation')
db.collection('users').update(
{ _id: 1 },
$.flatten({
createdOn: $.$setOnInsert(new Date()) // sets createdOn field only when document is inserted
}),
{ upsert: true })
// { '$setOnInsert': { 'createdOn': new Date() } }
.$set
See mongo $set.
The $set
operator replaces the value of a field with the specified value.
const $ = require('mongo-dot-notation')
$.flatten({
name: $.$set('Mike')
})
// { '$set': { 'name': 'Mike' } }
The $set
is an implicit operator, meaning if an object is passed to $.flatten
, it will navigate through own and embedded document fields and apply $set.
const $ = require('mongo-dot-notation')
$.flatten({
name: 'Mike',
contactDetails: {
email: 'mike@test.com'
}
})
// {
// '$set': {
// 'name': 'Mike',
// 'contactDetails.email': 'mike@test.com'
// }
// }
The $set
operator could also be used to update an embedded document entirely:
const $ = require('mongo-dot-notation')
$.flatten({
name: 'Mike',
// contactDetails is updated to a new document
contactDetails: $.$set({
email: 'mike@test.com'
})
})
// {
// '$set': {
// 'name': 'Mike',
// 'contactDetails': { email: 'mike@test.com' }
// }
// }
.$unset
See mongo $unset.
The $unset
operator deletes a particular field.
const $ = require('mongo-dot-notation')
$.flatten({
comments: $.$unset(), // remove field from document
history: $.$unset()
})
// { '$unset': { 'comments': '', 'history': '' } }
.$min
See mongo $min.
The $min
updates the value of the field to a specified value if the specified value is less than the current value of the field.
const $ = require('mongo-dot-notation')
$.flatten({
low: $.$min(200) // update low to 200 if current low value is greater than 200
})
// { '$min': { 'low': 200 } }
.$max
See mongo $max.
The $max
operator updates the value of the field to a specified value if the specified value is greater than the current value of the field.
const $ = require('mongo-dot-notation')
$.flatten({
high: $.$max(450) // update high to 450 if current high value is less than 450
})
// { '$max': { 'high': 450 } }
.$currentDate
See mongo $currentDate.
The $currentDate
operator sets the value of a field to the current date, either as a Date or a Timestamp.
If type is not specified, uses Date by default.
const $ = require('mongo-dot-notation')
$.flatten({
lastUpdate: $.$currentDate()
})
// { '$currentDate': { 'lastUpdated': { '$type': 'date' } } }
To set as a timestamp, use:
const $ = require('mongo-dot-notation')
$.flatten({
lastUpdate: $.$currentDate('timestamp')
})
// { '$currentDate': { 'lastUpdated': { '$type': 'timestamp' } } }
Also, for timestamp an alias operator is defined in mongo-dot-notation
:
const $ = require('mongo-dot-notation')
$.flatten({
lastUpdate: $.$timestamp()
})
// { '$currentDate': { 'lastUpdated': { '$type': 'timestamp' } } }
Array update operators
.$ (update)
See mongo $ (update).
The positional $
operator identifies an element in an array to update without explicitly specifying the position of the element in the array.
const $ = require('mongo-dot-notation')
db.students.update(
{ _id: 1, grades: 80 }, // match all elements from grades array where value equals to 80
$.flatten({
grades: $.$().$set(82) // for matched elements, update value to 82
})
)
// { $set: { "grades.