Add option to retreive _id from DB

Signed-off-by: Fred Boniface <fred@fjla.uk>
This commit is contained in:
Fred Boniface 2023-05-31 20:30:16 +01:00
parent b32c34fe7d
commit 085e6aa80d

View File

@ -13,12 +13,14 @@ const { MongoClient } = require('mongodb');
const client = new MongoClient(uri); const client = new MongoClient(uri);
const db = client.db(dbName); const db = client.db(dbName);
async function query(collection, query){ async function query(collection, query, returnId = false){
await client.connect(); await client.connect();
log.out(`dbAccess.query: Connecting to collection: '${collection}'`, 'info'); log.out(`dbAccess.query: Connecting to collection: '${collection}'`, 'info');
var qcoll = db.collection(collection); var qcoll = db.collection(collection);
var qcursor = qcoll.find(query); var qcursor = qcoll.find(query);
if (!returnId) {
qcursor.project({_id: 0}); qcursor.project({_id: 0});
}
log.out(`dbAccess.query: Running Query: ${JSON.stringify(query)}`, 'info'); log.out(`dbAccess.query: Running Query: ${JSON.stringify(query)}`, 'info');
increment(collection); increment(collection);
return (await qcursor.toArray()); return (await qcursor.toArray());