Hello I am trying to learn how to use mongoose and when I follow their basic official tutorial, with callbacks everything is fine. But when I follow a course that uses promises with async/await fashion, whenever I console log a created document, or when I query it, even with .require() I just get too much stuff returned.
I have simplified my code to the minimum, hopefully if you have mongo running and a node folder with mongoose you can execute it straight away.
/* jshint esversion: 8 */
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/freecodecamp-learning-mongoose')
.then (()=> console.log('Connected to MongoDB...'))
.catch(err => console.error('Could not connect to mongoDB', err));
const Schema1 = new mongoose.Schema({
name: String,
});
const DocumentModel = mongoose.model('Document',Schema1);
async function createDocument(){
const document = new DocumentModel({
name: 'document name'
});
const result = await document.save();
console.log(result);
}
createDocument();
async function getAllDocuments(){
const documents = await DocumentModel.find().select({name:1});
console.log(documents);
}
// getAllDocuments();
when I run that I always get the right documents but with too much stuff, even when i query with select option:
[
model {
'$__': InternalCache {
strictMode: true,
selected: [Object],
shardval: undefined,
saveError: undefined,
validationError: undefined,
adhocPaths: undefined,
removing: undefined,
inserting: undefined,
version: undefined,
getters: {},
_id: 5ce11b094407036258c839fe,
populate: undefined,
populated: undefined,
wasPopulated: false,
scope: undefined,
activePaths: [StateMachine],
pathsToScopes: {},
ownerDocument: undefined,
fullPath: undefined,
emitter: [EventEmitter],
'$options': true
},
isNew: false,
errors: undefined,
_doc: { _id: 5ce11b094407036258c839fe, name: 'document name' },
'$init': true
}
]