How to get all documents in Mongoose
Posted on: October 15, 2021 by Deven
In this article, You will learn how to get all documents in Mongoose.
Let assume, you have a mongoose model User that has all your app’s users information. Now, to get all users list from the mongoose collection you have to call User.find() with an empty object as the first parameter.
const User = mongoose.model('User', Schema({
name: String,
email: String,
password : String
}));
const filter = {};
const all = await User.find(filter);
It will search for all the documents that match with the filter Object. But when you pass an empty filter, it will match with all the documents and will return all documents.
Similarly, if you call User.find() and pass no arguments you will get the same result. In that case, you have to use await before User.find()
await User.find()
This is how you can get all documents in Mongoose by using the mongoose find() function.
Share on social media
//