Mongoose not returning just documents but the model object too

Mongoose not returning just documents but the model object too
0

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
  }
]

I actually tried your code and had no problems logging just the documents both for createDocument() and the getAllDocuments() functions without changing a single line to your code, I used glitch , you can take a look here, but you’d need your own mongoDB atlas connection

1 Like

Thanks a lot for your help!. I spent a few hours trying to find out what was wrong!!!. Very annoying!

could you post your solution ? maybe others will run into the same issue in the future…

i have not had the chance to test it yet but I was given an answer at stackoverflow:

Mongoose queries return instance of the Mongoose Document Class. You need to use lean in order to avoid the instance of the Document Class. Use the following code inside your getAllDocuments function:

const documents = await DocumentModel.find().select({name:1}).lean()

https://stackoverflow.com/a/56216641/10470242