всякий раз, когда я консоль регистрирую созданный документ, или когда я запрашиваю его, даже с .require (), я просто получаю слишком много возвращаемого материала. Похоже, документы возвращаются внутри объекта модели.
Я сократил свой код до минимума.
/* 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});//.count();
console.log(documents);
}
// getAllDocuments();
это то, что я получаю, если я выполню это ... даже если я сделаю запрос с .select ()
model {
'$__': InternalCache {
strictMode: true,
selected: undefined,
shardval: undefined,
saveError: undefined,
validationError: undefined,
adhocPaths: undefined,
removing: undefined,
inserting: true,
version: undefined,
getters: {},
_id: 5ce11d9cf5675b6482d0fa38,
populate: undefined,
populated: undefined,
wasPopulated: false,
scope: undefined,
activePaths: StateMachine {
paths: {},
states: [Object],
stateNames: [Array],
map: [Function]
},
pathsToScopes: {},
ownerDocument: undefined,
fullPath: undefined,
emitter: EventEmitter {
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: 0
},
'$options': true
},
isNew: false,
errors: undefined,
_doc: { _id: 5ce11d9cf5675b6482d0fa38, name: 'document name', __v: 0 }
Сами запросы работают нормально, и даже если бы я запросил .count (), я бы получил правильную сумму (в данном случае 1).