I am currently using Mongoose 7.3.1 and have inserted a few documents into the MongoDB database and I am unable to log the documents using console.log()
by simply using the Fruit.find({})
and logging it to the console all it spits out is a huge data set of unwanted objects.
The unwanted output is this
Query {
_mongooseOptions: {},
_transforms: [],
_hooks: Kareem { _pres: Map(0) {}, _posts: Map(0) {} },
_executionStack: null,
mongooseCollection: Collection {
collection: null,
Promise: [Function: Promise],
modelName: 'Fruit',
_closed: false,
opts: {
autoIndex: true,
autoCreate: true,
schemaUserProvidedOptions: {},
capped: false,
Promise: undefined,
'$wasForceClosed': undefined
},
name: 'fruits',
collectionName: 'fruits',
conn: NativeConnection {
base: [Mongoose],
collections: [Object],
models: [Object],
config: {},
replica: false,
options: null,
otherDbs: [],
relatedDbs: {},
states: [Object: null prototype],
_readyState: 2,
_closeCalled: false,
_hasOpened: false,
plugins: [],
id: 0,
_queue: [Array],
_listening: false,
_connectionOptions: [Object],
_connectionString: 'mongodb://localhost:27017/fruitsDB',
client: [MongoClient],
'$initialConnection': [Promise]
},
queue: [],
buffer: true,
emitter: EventEmitter {
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: undefined,
[Symbol(kCapture)]: false
}
},
model: Model { Fruit },
schema: Schema {
obj: {
name: [Function: String],
rating: [Function: Number],
review: [Function: String]
},
paths: {
name: [SchemaString],
rating: [SchemaNumber],
review: [SchemaString],
_id: [ObjectId],
__v: [SchemaNumber]
},
aliases: {},
subpaths: {},
virtuals: { id: [VirtualType] },
singleNestedPaths: {},
nested: {},
inherits: {},
callQueue: [],
_indexes: [],
methods: {},
methodOptions: {},
statics: {},
tree: {
name: [Function: String],
rating: [Function: Number],
review: [Function: String],
_id: [Object],
__v: [Function: Number],
id: [VirtualType]
},
query: {},
childSchemas: [],
plugins: [ [Object], [Object], [Object], [Object], [Object] ],
'$id': 1,
mapPaths: [],
s: { hooks: [Kareem] },
_userProvidedOptions: {},
options: {
typeKey: 'type',
id: true,
_id: true,
validateModifiedOnly: false,
validateBeforeSave: true,
read: null,
shardKey: null,
discriminatorKey: '__t',
autoIndex: null,
minimize: true,
optimisticConcurrency: false,
versionKey: '__v',
capped: false,
bufferCommands: true,
strictQuery: false,
strict: true,
pluralization: true
},
'$globalPluginsApplied': true,
_requiredpaths: []
},
op: 'find',
options: {},
_conditions: {},
_fields: undefined,
_updateDoc: undefined,
_path: undefined,
_distinctDoc: undefined,
_collection: NodeCollection {
collection: Collection {
collection: null,
Promise: [Function: Promise],
modelName: 'Fruit',
_closed: false,
opts: [Object],
name: 'fruits',
collectionName: 'fruits',
conn: [NativeConnection],
queue: [],
buffer: true,
emitter: [EventEmitter]
},
collectionName: 'fruits'
},
_traceFunction: undefined,
'$useProjection': true
}
I even tried inspecting the objects for the documents from the database but was unable to find any.
Mongoose official documentation here mentions using the above code to find the documents but it's not showing the documents.
How can I log the documents to the terminal using Mongoose?
The issue you're encountering is due to the asynchronous nature of database calls. The find method of Mongoose returns a Query object which acts like a promise, which you need to handle accordingly. You can try handling it using .then() or async/await. Something similar to this:
Fruit.find({}).then((docs) => {
console.log(docs);
}).catch((err) => {
console.error(err);
});