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:
- use dotenv to set the variables
- issue the command
export AMK_MONGO_USERNAME=username
- put it on the
.bashrc
or.bash_profile
file - 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.
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
})
For filter and options usage refer to this
- (Array|Promise) - result set in an array or query object
Will return all the match results;
- (Array|Promise) - result set in an array or query object
Will return the match document;
- (Object|Promise) - null or the match document;
Inserts a single document into MongoDB. Doc and options refer to this
- (insertOneWriteOpResultObject|Promise) insertOneWriteOpResultObject
Inserts an array of documents into MongoDB. Docs and options refer to this
- (insertWriteOpResultObject|Promise) insertWriteOpCallback
Update a single document on MongoDB. Filter, update, options refer to this
- (updateWriteOpResultObject|Promise) updateWriteOpResultObject
Update multiple documents on MongoDB.
- (updateWriteOpResultObject|Promise) updateWriteOpResultObject
Delete a document on MongoDB. refer to this
- (deleteWriteOpResultObject|Promise) deleteWriteOpResultObject
Delete multiple documents on MongoDB. refer to this
- (deleteWriteOpResultObject|Promise) deleteWriteOpResultObject
Find a document and update it in one atomic operation, requires a write lock for the duration of the operation. refer to this
- (findAndModifyWriteOpResultObject|Promise) findAndModifyWriteOpResultObject
Count number of matching documents in the db to a query. Alias for count in Mongo.
- (Number|Promise) The count of documents that matched the query.
Execute an aggregation framework pipeline against the collection, needs MongoDB >= 2.2 Refer to this
- (Array|Promise) All the documents the satisfy the cursor. Refer to this
Create a new ObjectID instance. Id parameter is optional.
- (ObjectID)