Skip to content

Commit

Permalink
add mongo count
Browse files Browse the repository at this point in the history
  • Loading branch information
birm committed Apr 23, 2024
1 parent 00472ff commit dbd518a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions caracal.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ var HANDLERS = {
"mongoUpdate": dataHandlers.General.update,
"mongoDelete": dataHandlers.General.delete,
"mongoDistinct": dataHandlers.General.distinct,
"mongoCount": dataHandlers.General.count,
"filterHandler": auth.filterHandler,
"permissionHandler": permissionHandler,
"editHandler": auth.editHandler,
Expand Down
28 changes: 28 additions & 0 deletions service/database/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,34 @@ class Mongo {
}
}

/**
* Runs the MongoDB count() method to count documents.
*
* @async
* @param {string} database Name of the database
* @param {string} collectionName Name of the collection to run operation on
* @param {document} query Specifies selection filter using query operators.
* To return all documents in a collection, omit this parameter or pass an empty document ({}).
* @param {boolean} [transform=false] check to transform the IDs to ObjectID in response
*
* {@link https://docs.mongodb.com/manual/reference/method/db.collection.count/ Read MongoDB Reference}
*/
static async count(database, collectionName, query, transform = true) {
try {
query = transformIdToObjectId(query);

const collection = getConnection(database).collection(collectionName);
const count = await collection.count(query);

data =[{"count": count}];

return data;
} catch (e) {
console.error(e);
throw e;
}
}

/**
* Runs a distinct find operation based on given query
*
Expand Down

0 comments on commit dbd518a

Please sign in to comment.