I want to access the data of a MongoDB Database using Mongoose. In every Mongoose Tutorial, we first need to create a schema and then a model out of it. But, I already have the data in the database. What I m trying s creating an empty schema and passing the collection name as the third parameter while creating the model out of it. But for some unknown reason I can't access the data, when I import the model class in another file. Model File:
const mongoose = require("mongoose");
// Create a Schema
const CompanySchema = new mongoose.Schema({}, { strict: false });
const Company = mongoose.model("Company", CompanySchema, "companies");
// console.log(Company)
module.exports = Company;
NOTE: Companies is the name of collection I wanna access, see image. db image
In this file I m just trying to read the data:
const express = require('express');
const router = express.Router();
const Companies = require('../models/Companies')
// Homepage Route
router.get('/', (req, res) => res.render('home'))
// List Route
router.get('/list', (req, res) =>
res.render('list')
);
// Register Route
router.get('/register', (req, res) =>
res.render('register')
);
console.log(Companies.find({}))
module.exports = router;
The console gives me this (I think it's a mongoose object):
Query {
_mongooseOptions: {},
_transforms: [],
_hooks: Kareem { _pres: Map {}, _posts: Map {} },
_executionCount: 0,
mongooseCollection: NativeCollection {
collection: null,
Promise: [Function: Promise],
_closed: false,
opts: {
schemaUserProvidedOptions: [Object],
capped: false,
autoCreate: undefined,
Promise: [Function: Promise],
'$wasForceClosed': undefined
},
name: 'companies',
collectionName: 'companies',
conn: NativeConnection {
base: [Mongoose],
collections: [Object],
models: [Object],
config: [Object],
replica: false,
options: null,
otherDbs: [],
relatedDbs: {},
states: [Object: null prototype],
_readyState: 0,
_closeCalled: false,
_hasOpened: false,
plugins: [],
id: 0,
_listening: false
},
queue: [],
buffer: true,
emitter: EventEmitter {
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: undefined,
[Symbol(kCapture)]: false
}
},
model: Model { Company },
schema: Schema {
obj: {},
paths: { _id: [ObjectId], __v: [SchemaNumber] },
aliases: {},
subpaths: {},
virtuals: { id: [VirtualType] },
singleNestedPaths: {},
nested: {},
inherits: {},
callQueue: [],
_indexes: [],
methods: {},
methodOptions: {},
statics: {},
tree: { _id: [Object], __v: [Function: Number], id: [VirtualType] },
query: {},
childSchemas: [],
plugins: [ [Object], [Object], [Object], [Object], [Object] ],
'$id': 1,
s: { hooks: [Kareem] },
_userProvidedOptions: { strict: false },
options: {
strict: false,
typePojoToMixed: true,
typeKey: 'type',
id: true,
noVirtualId: false,
_id: true,
noId: false,
validateBeforeSave: true,
read: null,
shardKey: null,
autoIndex: null,
minimize: true,
discriminatorKey: '__t',
versionKey: '__v',
capped: false,
bufferCommands: true,
pluralization: true
},
'$globalPluginsApplied': true
},
op: 'find',
options: {},
_conditions: {},
_fields: undefined,
_update: undefined,
_path: undefined,
_distinct: undefined,
_collection: NodeCollection {
collection: NativeCollection {
collection: null,
Promise: [Function: Promise],
_closed: false,
opts: [Object],
name: 'companies',
collectionName: 'companies',
conn: [NativeConnection],
queue: [],
buffer: true,
emitter: [EventEmitter]
},
collectionName: 'companies'
},
_traceFunction: undefined,
'$useProjection': true
}