1.1.1 • Published 7 years ago
simple-mongodb v1.1.1
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 => {
// db is the instance of connected database.
})
.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
.connect(url)
This method must be called before any other methods.
.close(force = false)
Close the db and it's underlying connections.
The force
argument is default to false
.
.insertOne(name, args)
Inserts a single document into MongoDB.