I want to console.log() only my data present in my MongoDB database

Viewed 436

I am entering data in my database in mongodb and want to console.log() the data entered but whenever I try to do it, my data do come to console but with various other properties, which I do not need. I just want to bring the data entered on my console !

const mongoose = require("mongoose");

mongoose
  .connect("mongodb://localhost/playground")
  .then(() => {
    console.log("database connected");
  })
  .catch(err => {
    console.log("Could not connect", err);
  });

const courseSchema = new mongoose.Schema({
  name: String,
  author: String,
  tags: [String],
  date: { type: Date, default: Date.now },
  isPublished: Boolean
});

// To Create a class of Course, we need to use the model method
const Course = mongoose.model("Course", courseSchema);

async function createCourse() {
  //Lets create a object to the Course class
  // Async Routine
  const course = new Course({
    name: "React Course",
    author: "Mosh",
    tags: ["FrontEnd", "React"],  
    isPublished: true
  });

  // Log Tree : Schema -> Model -> Class -> Object !....

  const result = await course.save();
  console.log(result);
}



async function getCourse() {
  const course = await Course.find();

//Bringing the data from the data base.  

  console.log(course);
}

getCourse();

What I want to be displayed on my console is (Actual Data in my mongo data base)

[ {"_id":{"$oid":"5e847c6e6ed0fa2c785647e4"},
"tags":["Backend","Node"],
"date":{"$date":{"$numberLong":"1585740910542"}},
"name":"Node Course",
"author":"Mosh","isPublished":true,
"__v":{"$numberInt":"0"}   
} , 

{"_id":{"$oid":"5e84868970369f3a30e25270"},
"tags":["FrontEnd","React"],
"date":{"$date":{"$numberLong":"1585743497438"}},
"name":"React Course",
"author":"Mosh",
"isPublished":true,"__v":{"$numberInt":"0"} } ]

BUT What I get is my data BUT with this extra information :-

[
  model {
    '$__': InternalCache {
      strictMode: true,
      selected: {},
      shardval: undefined,
      saveError: undefined,
      validationError: undefined,
      adhocPaths: undefined,
      removing: undefined,
      inserting: undefined,
      version: undefined,
      getters: {},
      _id: 5e847c6e6ed0fa2c785647e4,
      populate: undefined,
      populated: undefined,
      wasPopulated: false,
      scope: undefined,
      activePaths: [StateMachine],
      pathsToScopes: {},
      ownerDocument: undefined,
      fullPath: undefined,
      emitter: [EventEmitter],
      '$options': true
    },
    isNew: false,
    errors: undefined,
    _doc: {
      tags: [Array],
      date: 2020-04-01T11:35:10.542Z,
      _id: 5e847c6e6ed0fa2c785647e4,
      name: 'Node Course',
      author: 'Mosh',
      isPublished: true,
      __v: 0
    },
    '$init': true
  },
  model {
    '$__': InternalCache {
      strictMode: true,
      selected: {},
      shardval: undefined,
      saveError: undefined,
      validationError: undefined,
      adhocPaths: undefined,
      removing: undefined,
      inserting: undefined,
      version: undefined,
      getters: {},
      _id: 5e84868970369f3a30e25270,
      populate: undefined,
      populated: undefined,
      wasPopulated: false,
      scope: undefined,
      activePaths: [StateMachine],
      pathsToScopes: {},
      ownerDocument: undefined,
      fullPath: undefined,
      emitter: [EventEmitter],
      '$options': true
    },
    isNew: false,
    errors: undefined,
    _doc: {
      tags: [Array],
      _id: 5e84868970369f3a30e25270,
      name: 'React Course',
      isPublished: true,
      __v: 0
    },
    '$init': true
  }
]

I dont want this extra information along with my data, when I console.log my Class !

1 Answers

I see the problem. I was running mongoose 5.0.1, just updated the mongoose package and It stopped displaying other properties !

Related