simple-mongodb
A simple lib to simplify your work of using mongoldb native driver in node.js.
Installation
npm install --save simple-mongodb
Usage
First, you should connect the mongodb when app starts:
import DB from 'simple-mongodb';
DB.connect(url);
The connect()
method is asynchronous. It will return a Promise, so you can use it like:
DB.connect(url)
.then(db => {
})
.catch(err => console.log(err));
or:
async function() {
try {
const db = await DB.connect(url);
} catch (err) {
console.log(err);
}
}
Once the mongodb is connected, you could use it anywhere in your code like this:
DB.find(collectionName, args);
The args
is an object whose propertoes arr correspond to the MongoDB Driver's arguments.
Methods
This method must be called before any other methods.
Close the db and it's underlying connections.
The force
argument is default to false
.
Inserts a single document into MongoDB.