0.0.1 • Published 3 years ago
mongo-utils.js v0.0.1
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
0.0.1
3 years ago