@cdxoo/mongodb-utils

1.0.1 • Public • Published

@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) {
    /**/
}

Readme

Keywords

none

Package Sidebar

Install

npm i @cdxoo/mongodb-utils

Weekly Downloads

6

Version

1.0.1

License

MIT

Unpacked Size

9.69 kB

Total Files

12

Last publish

Collaborators

  • cdxoo