Search code examples
javascriptmongodbfunctionpromiserealm

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


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!


Solution

  • Solved it already. I just made my parent function async. And the 'await' worked. Thanks.