amk-mongo v0.0.0
AMK-MONGO
Setup
Set the following environment variables
- AMK_MONGO_USERNAME
- AMK_MONGO_PASSWORD
- AMK_MONGO_HOSTS
- AMK_MONGO_DEFAULT_DATABASE
you can achieve this by choosing one of the options below:
1. use dotenv to set the variables
2. issue the command export AMK_MONGO_USERNAME=username
3. put it on the .bashrc
or .bash_profile
file
4. for AMK_MONGO_HOSTS , it should be a string like '172.16.1.90:27017,172.16.1.91:27017,172.16.1.92:27017'. Each host is separated by the colon.
Usage
After setting up environment variables, inherit from this amk-mongo
. refer to code snipet below:
user.js
const Mongo = require('amk-mongo');
class User extends Mongo {
constructor() {
super('user', 'dbname') // dbname is optional if the AMK_MONGO_DEFAULT_DATABASE has been set.
}
}
module.exports = User
using user.js
const User = require('./user');
const user = new User();
user.find({}, {limit: 30}).then( results => {
//will return a list of user object
})
API
find(filter, options)
For filter and options usage refer to this
Returns
- (Array|Promise) - result set in an array or query object
findAll(filter, options)
Will return all the match results;
Returns
- (Array|Promise) - result set in an array or query object
findOne(query, options)
Will return the match document;
Returns
- (Object|Promise) - null or the match document;
insertOne(doc, options)
Inserts a single document into MongoDB. Doc and options refer to this
Returns
- (insertOneWriteOpResultObject|Promise) insertOneWriteOpResultObject
insertMany(docs, options)
Inserts an array of documents into MongoDB. Docs and options refer to this
Returns
- (insertWriteOpResultObject|Promise) insertWriteOpCallback
updateOne(filter, update, options)
Update a single document on MongoDB. Filter, update, options refer to this
Returns
- (updateWriteOpResultObject|Promise) updateWriteOpResultObject
updateMany(filter, update, options)
Update multiple documents on MongoDB.
Returns
- (updateWriteOpResultObject|Promise) updateWriteOpResultObject
deleteOne(filter, options)
Delete a document on MongoDB. refer to this
Returns
- (deleteWriteOpResultObject|Promise) deleteWriteOpResultObject
deleteMany(filter, options)
Delete multiple documents on MongoDB. refer to this
Returns
- (deleteWriteOpResultObject|Promise) deleteWriteOpResultObject
findOneAndUpdate(filter, update, options)
Find a document and update it in one atomic operation, requires a write lock for the duration of the operation. refer to this
Returns
- (findAndModifyWriteOpResultObject|Promise) findAndModifyWriteOpResultObject
sum(query, options)
Count number of matching documents in the db to a query. Alias for count in Mongo.
Returns
- (Number|Promise) The count of documents that matched the query.
aggregate(pipeline, options)
Execute an aggregation framework pipeline against the collection, needs MongoDB >= 2.2 Refer to this
Returns
- (Array|Promise) All the documents the satisfy the cursor. Refer to this
toObjectID(id)
Create a new ObjectID instance. Id parameter is optional.
Returns
- (ObjectID)
7 years ago