Get existing document from MongoDB with Mongoose

Viewed 67

I have an existing document on MongoDB atlas that I need to get in its entirety. When I try my current code I get a socket timeout, i.e. it keeps buffering.

There is only one document in my collection.

My theory is that the error is in my Mongoose Schema, but I don't know what to put there since I don't want any "filters", I want everything.

The name of the collection in Atlas is Playstation and the id of the document I want to get is: 5f5e2d281386efc27bb3ce45

const express = require('express')
const router = express.Router()
const Playstation = require('../models/playstation')

//Getting All
router.get('/', async (req, res) => {
    try {
        const playstations = await Playstation.findById("5f5e2d281386efc27bb3ce45")
        res.json(playstations)
    } catch (err) {
        res.status(500).json({ message: err.message })
    }
})

module.exports = router;

My schema & model:

const mongoose = require('mongoose')

const playstationSchema = new mongoose.Schema({

})

module.exports = mongoose.model('Playstation', playstationSchema)
1 Answers

I believe the problem is your mongoose schema. What you want is a schemaless collection, since you dont want to define the schema, and you want everything in the document, you can define the collection as schema-less, with strict:false option in your mongoose schema;

Try this:

const mongoose = require('mongoose')

const playstationSchema = new mongoose.Schema({

},{ strict : false })

module.exports = mongoose.model('Playstation', playstationSchema);

You can read more about this option on Mongoose Documentation

Related