Mongoose always returning an empty array NodeJS

Solution 1:

The call to mongoose.model establishes the name of the collection the model is tied to, with the default being the pluralized, lower-cased model name. So with your code, that would be 'models'. To use the model with the files collection, change that line to:

var Model = mongoose.model("Model", fileSchema, "files");

or

var Model = mongoose.model("file", fileSchema);

Solution 2:

Simply inorder to avoid pluralization complexity use this:

var Model = mongoose.model("Model", fileSchema, "pure name your db collection");

It's very confusing.[at least for me.]

Solution 3:

Had kinda same problem. The solutions above didnt work for me. My app never returns error even if the query is not found. It returns empty array. So i put this in my code:

if(queryResult.length==0) return res.status(404).send("not found");

Solution 4:

This issue is probably coming from the fact that you are creating a mongoose model without specifying the name of the collection.

Try changing : const Model = mongoose.model("Model", fileSchema);

To this : const Model = mongoose.model("Model", fileSchema, "NameOfCollection");