mongo-round

1.0.0 • Public • Published

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

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 1.0.0
    1,400
    • latest

Version History

  • Version
    Downloads (Last 7 Days)
    • Published
  • 1.0.0
    1,400

Package Sidebar

Install

npm i mongo-round

Weekly Downloads

1,400

Version

1.0.0

License

MIT

Last publish

Collaborators

  • ebdrup
  • bubenshchykov
  • kapetan
  • wtfil
  • eagleeye
  • jonatanpedersen
  • bifrost
  • mpushkin