/** * It will execute the aggregation query on the given connection, database and collection * @note It will NOT disconnect * @stable * @async * @param connection * @param dbConfig * @param pipeline */ export function mongoAggregate(connection: any, dbConfig: any, pipeline: any, options?: any) { const db = connection.db(dbConfig.database); const collection = db.collection(dbConfig.collection); return collection.aggregate(pipeline, options); } /** * This will run aggregation queries on the same connection, database and collection in parallel and return array of promises * @stable * @async * @param connection * @param dbConfig * @param pipelines */ export const mongoAggregateParallelRunner = (connection: any, dbConfig: any, pipelines: Array, options?: Array) => { let promiseArray: Array> = []; for (let i = 0; i < pipelines.length; i++) { if(options) promiseArray.push(mongoAggregate(connection, dbConfig, pipelines[i], options[i])); else promiseArray.push(mongoAggregate(connection, dbConfig, pipelines[i])); } return Promise.all(promiseArray) } /** * It will execute find many documents query on the given connection, database and collection * @note It will NOT disconnect * @stable * @async * @param connection * @param dbConfig * @param pipeline */ export function mongoFindManyDocuments(connection: any, dbConfig: any, query: any, options?: any) { const db = connection.db(dbConfig.database); const collection = db.collection(dbConfig.collection); return collection.find(query, options); } /** * This will run find many documents queries on the same connection, database and collection in parallel and return array of promises * @stable * @async * @param connection * @param dbConfig * @param pipelines */ export const mongoFindManyDocumentsParallelRunner = (connection: any, dbConfig: any, queries: Array, options?: Array) => { let promiseArray: Array> = []; for (let i = 0; i < queries.length; i++) { if(options) promiseArray.push(mongoFindManyDocuments(connection, dbConfig, queries[i], options[i])); else promiseArray.push(mongoFindManyDocuments(connection, dbConfig, queries[i])); } return Promise.all(promiseArray) } /** * It will execute delete many documents based on a query on the given connection, database and collection * @note It will NOT disconnect * @stable * @async * @param connection * @param dbConfig * @param query */ export function mongoDeleteManyDocuments(connection: any, dbConfig: any, query: any, options?: any) { const db = connection.db(dbConfig.database); const collection = db.collection(dbConfig.collection); return collection.deleteMany(query, options); } /** * It will execute insert many documents on the given connection, database and collection * @note It will NOT disconnect * @stable * @async * @param connection * @param dbConfig * @param documents */ export function mongoInsertManyDocuments(connection: any, dbConfig: any, documents: any, options?: any) { const db = connection.db(dbConfig.database); const collection = db.collection(dbConfig.collection); return collection.insertMany(documents, options); } /** * It will execute update many documents on the given connection, database and collection * @note It will NOT disconnect * @stable * @async * @param connection * @param dbConfig * @param query */ export function mongoUpdateManyDocuments(connection: any, dbConfig: any, filterQuery: any, update: any, options?: any) { const db = connection.db(dbConfig.database); const collection = db.collection(dbConfig.collection); return collection.updateMany(filterQuery, update, options); }