I'm using the mongo driver together with NodeJs to retrieve database collections.
Here is a simple code to get all users:
const getUsers = async context => {
let users = await db.collection("users");
let result = await users.find({
deletedAt: null
});
console.log(result)
return result
};
The result
is a cursor that needs to be iterated.
Is there a way to retrieve all records without the need of iterating, something like toArray()
or toJSON()
?
You can use .toArray()
as you expect (docs):
To get all documents at once, users can use the toArray method.
const getUsers = async context => {
let users = await db.collection("users");
let cursor = users.find({
deletedAt: null
});
let result = await cursor.toArray();
console.log(result)
return result
};
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加