New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

mongo-round

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mongo-round

Mongo Aggregation Framework helper function to perform rounding of numbers

latest
Source
npmnpm
Version
1.0.0
Version published
Weekly downloads
5.5K
45.68%
Maintainers
1
Weekly downloads
 
Created
Source

mongo-round Build Status

Helper function to perform rounding of numbers when working with mongoDB Aggregation Framework.

npm install mongo-round

Usage

var round = require('mongo-round');

To zero decimal places:

db.myCollection.aggregate([
	{ $project: {
		roundValue: round('$value')
	} }
]);

To two decimal places:

db.myCollection.aggregate([
	{ $project: {
		roundAmount: round('$amount', 2)
	} }
]);

To cents and from cents - saves from rounding errors for armithmetic operations on financial data:

db.myCollection.aggregate([
	{ $project: {
		amountCents: round({$multiply:['$amount', 100]})
	} },
	{ $group: {
		_id: null, amountCents: {$sum: '$amountCents'}
	} }
	{ $project: {
    	amount: round({$divide:['$amountCents', 100]}, 2)
    } }
]);

Note that for negative numbers rounding is done using absolute values, which is fitting for financial data:

round(-0.5) will give -1

License

MIT

Keywords

mongo

FAQs

Package last updated on 22 Apr 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts