mongo-utils.js

0.0.1 • Public • Published

mongo-utils

Small and injectable mongodb utils!

Install

$ npm i --save mongo-utils

Usage

// 1. Reduce Array of Strings to String: prop = ["Sambhav", "Jain"] --> prop = "Sambhav, Jain"
const { reduceMongoArrayToString } = require("mongo-utils");
db.model.aggregate([
  {
    $project: {
      prop: reduceMongoArrayToString("prop"),
    },
  },
]);

// 2. Cast String to Number while comaparing in query:
// prop2 = "30", query = Find all records with prop2 >= 10
const { castStringToNumberInQuery } = require("mongo-utils");
db.model.find({ prop2: castStringToNumberInQuery("$gte", "Int", "prop2") });

// Caveat: Property under comparison must have numeric value stored as String for this to work

Released under MIT License

Dependencies (0)

    Dev Dependencies (0)

      Package Sidebar

      Install

      npm i mongo-utils.js

      Weekly Downloads

      1

      Version

      0.0.1

      License

      MIT

      Unpacked Size

      1.93 kB

      Total Files

      3

      Last publish

      Collaborators

      • sambhav.cbs