Reading Stored Documents
Part of the Databases with MongoDB section of Coddy's Node.js journey. Lesson 4 of 29.
You have selected a collection. To read its documents, call find(). It returns a cursor: an object describing results you can read. toArray() reads them into a JavaScript array.
const list = await books.find().toArray();
// [{ title: 'Dune', ... }, { title: 'Kindred', ... }, ...]The read takes time, so await gives you the finished array. find by itself is not that array. Each stored document also has an _id assigned by MongoDB; leave it in place for now. The ready route sends whatever your function returns. Inspect the same books in the Data tab.
Challenge
EasyThis is a small practice project. Its setup is provided; edit only exercise.js.
Return every document in books from the async function. Run and GET /result: the JSON array contains Dune, Kindred and Emma. Return the documents without rebuilding them. A cursor is not a JSON array.
Try it yourself
export async function exercise(books) {
return [];
}
All lessons in Databases with MongoDB
Practice on your own: Express playground