0
votes

Is there a way to read from MongoDB using Mongoose(for node.js) without defining a schema.

If I only want to print out all the data stored in a collection, like how the terminal command db.collectionName.find() works. Can I not pass a schema to achieve that?

1

1 Answers

1
votes

Mongoose expose the mongodb.DB instance via mongoose.connection.db, so you can use directly the mongodb native driver

For example, if you want to print out all the data stored in a collection without defining schema

let mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/testDB').then(() => {
    const db = mongoose.connection.db;
    db.collection('collection-name').find().toArray((err, result) => {
        console.log(result)
    });
}).catch(err => console.log(err.message))

See mongodb native driver documentation for more examples