This helper expose native entries from mongodb driver and add some elements to simplify some actions. A lot of old commands are purged from v1 helper.
npm i simple-mongodb-helper2
import { helper as db } from './dist/index.js'
const myMongoHelperClass = require('simple-mongodb-helper-2'); const db = myMongoHelperClass.helper;
param = {
"uri": "mongodb://ip:port/dbname", <-- MongoDb uri with options
"dbName": "dbname",
"options": { <-- MongoDB drivers options
"auth": {
"password": "xxxx",
"username": "yyyy",
"authSource": "dbauth"
}
},
keepConnected : true <-- if set to true, check connexion every 2s
};
db.connect(param).then(() => {
//...
}).catch((e: any) => {
//...
});
This method create db.client witch is active db connexion (the connected MongoClient) db.usedDB : dbname db.config : all configuration parameters db.connected : last connection check result
Default parameters if not overxwrited are :
defaultParameters = {
authSource: (helper.config.dbName !== undefined) ? helper.config.dbName : 'admin',
keepAlive: true,
tlsAllowInvalidCertificates: true,
tlsAllowInvalidHostnames: true,
tlsInsecure: true,
sslValidate: false
}
All parameters could be overwrited
example with native function call
db.client.db().collection('colname').find({}).toArray().then((list) => {
return Promise.map(list, (el) => {
console.log(el)
}, { concurrency: 4 });
})
db.find('test',{_id:'test'},{_id:0,order:1,info:1},{order:-1},10).then((result) => {
//...
}).catch((e: any) => {
//...
});
db.aggregate('test',[{...},{...}]).then((result) => {
//...
}).catch((e: any) => {
//...
});
db.insert('test',[{"a":"b"},{"c":"d"}]).then((result) => {
//...
}).catch((e: any) => {
//...
});
db.update('test',{_id:'test'},{order:2,info:"new test"},true).then((result) => {
//...
}).catch((e: any) => {
//...
});
// inputs is an object
let inputs = { database : "exemple", collection" : "demo" }
// rawscript must return a promise
let rawScript = "db.db(inputs.database).collection(inputs.collection).find({}).toArray()";
db.eval(rawScript,inputs).then((result) => {
//...
}).catch((e: any) => {
//...
});
db.replace('test',{_id:'test'},{ 'element':'test'}).then((result) => {
//...
}).catch((e: any) => {
//...
});
count documents associated with filter
purge elements where filterKey key is older than olderThanSecond parameters
delete elements corresponding to filter
Allow to run script on server side with inputs as parameters, db as database connection and rawscript the script to eval. waring about security issue on bad use.
the aggregate request
the find request. If filter is an array, run all filter and return an array of all results If not define, limit is 100
the insert command, single or array of documents
The replace command, as find filter is selector (single or array of selectors) and to the set of values
The update command, as find filter is selector (single or array of selectors)
get list of collections