Search code examples
javascriptnode.jsmongoosemongoose-schema

Why is Mongoose returning "null" continuing to return null regardless of what I do?


I am able to connect to my MongoDB database and am sure that I am connecting to the correct one. When I try to get a document from the database I always get "null". I am sure that there are documents in the collection as I can see them in Compass.

const theschema = new Schema({
    sensor: {
        type: String,
        required: true
    }
})

const model = mongoose.model('Sensor', theschema)


model.findOne({}, function(err, data) {
    console.log(data)
})

Above is the code I am currently using for this. I'd appreciate any help possible. Thanks! :)


Solution

  • Just found the problem. The issue was mongoose pluralises collection names therefore it was looking in the wrong collection.

    The solution I used was to add this line of code before defining the model theschema.set('collection', 'Sensor'). This force sets the collection so Mongoose doesn't pluralise it