@cdxoo/mongodb-utils
v1.0.1
Published
simple mongodb utils
Downloads
51
Readme
@cdxoo/mongodb-utils
Tiny package containing 3 simple mongodb query utilities.
Installation
npm install --save @cdxoo/dbscan
Usage
const {
aggregateOne,
aggregateToArray,
aggregateToCursor
} = require('@cdxoo/mongodb-utils');
const { MongoClient } = require('mongodb');
const db = (await new MongoClient(/* ... */).connect()).db();
let doc = await aggregateOne({
db, // mongodb database handle
myCollection: [ // aggregate stages or object (see below)
{ $match: {
_id: 1234
}},
/* ... */
],
mongoSettings: { /* ... */ } // optional aggregate options like: session,
// readConcern, allowDiskUseetc
})
// if you dont really need any stage apart from $match there is
// this shorthand in all the utilites
let doc_alt = await aggregateOne({ db, myCollection: {
_id: 1234
}})
let manydocs = await aggregateToArray({ db, myCollection: [
{ $match: {
x: { $gt: 9000 }
}}
]})
let cursorL10 = aggregateToCursor({ db, myCollection: [
{ $match: {
x: { $gt: 9000 }
}}
]}).limit(10);
for await (let doc of cursorL10) {
/**/
}