Home:ALL Converter>MongoDB Realm Functions: How to query the document count of a collection

MongoDB Realm Functions: How to query the document count of a collection

Ask Time:2021-04-03T09:28:17         Author:nypogi

Json Formatter

I tried implementing using this:

const mongodb = context.services.get("mongodb-atlas");
const itemsCollection = mongodb.db("natcocuDB").collection("members");

And I tried to put it inside an object

let memberToInsert = arg;
memberToInsert.memberID = itemsCollection.count({}) + 1;

Then the result is this:

memberID : [object Promise]1"

So the count function is a Promise. I tried to "await" the count but the function editor of realm produces an error.

Error

The error says it's missing some ";".

So I tried so separate it by creating an async function for the count.

async function getDocumentCount(collection) {
  return await collection.count({}) + 1;
  
}

But the result is the same only an object:

memberID : Object

Do you have any idea how can I get the document count? Thanks!

Author:nypogi,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/66926976/mongodb-realm-functions-how-to-query-the-document-count-of-a-collection
nypogi :

Solved it already. I just made my parent function async. And the 'await' worked. Thanks.",
2021-04-04T14:11:18
yy