0

Checked couple of old similar questions but no luck in finding the right answers.

I have a mongo query that uses await/async to fetch results from the database, So the code looks like this :

(async () => {
  //get connection object
  const db = await getDatabaseConnection("admindb");

   //get user data object
  const configResponse = await db.collection("config").find({}).toArray();
  
  //prints results
  console.log("Successfully Fetched Configuration", configResponse);
})();

The above code works fine, just that it returns me an array of elements since I have used the .toArray() method.

Sample Result:

[
  {
    _id: "6028d30db7ea89f74df013d9",
    tokenize: false,
    configurations: { theme: {} }
  }
]

Is there a NATIVE WAY (not looking forEach responses) to get result as an object since I will always have only one document returned. I have multiple queries that returns only one document, Hence accessing using the 0th index everytime did not seem the right way.

1

1 Answer 1

3

I am thinking if the findOne() will help you.

Check this out docs

await db.collection("config").findOne({});

Here are some examples

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.