Express + MongoDB
By default, Mongoose applies a function (utils.toCollectionName()
) to the name of a model to determine the collection name that Mongoose will use for that model. For a model called Room
, that collection name will be rooms
.
However, your collection is called room
(singular), so there's a mismatch there.
To fix this, you can explicitly specify which collection should be used using the collection
schema option:
const roomSchema = new mongoose.Schema({ roomId: String,}, { collection : 'room'});
The problem is that the name of your collection is room
, as in singular. The correct name for the collection is rooms
, plural.
Mongoose infers from the model name Room
that there should be a collection named rooms
in the database. If there isn't, it just fails silently and returns an empty array.
If you simply change the name of your collection in the database from room
to rooms
, your code will work.
You can do so with the renameCollection command.