如何在控制台中仅记录来自MONGODB的对象属性

时间:2019-07-06 11:45:18

标签: mongodb console.log objectid

我只想记录来自MongoDB的对象(例如课程对象),但是写的时候

  > "console.log(courses)"

它也会从数据库中记录不必要的东西(例如严格模式,选定对象等),这些东西不包括在我的对象属性中,因此只能记录对象属性(即id,name)

我曾尝试编写类似console.log(courses._doc)的代码,但没有用

 model {
    '$__': InternalCache {
      strictMode: true,
      selected: [Object],
      shardval: undefined,
      saveError: undefined,
      validationError: undefined,
      adhocPaths: undefined,
      removing: undefined,
      inserting: undefined,
      version: undefined,
      getters: {},
      _id: 5d207fc27c9ecf2f688d1f5f,
      populate: undefined,
      populated: undefined,
      wasPopulated: false,
      scope: undefined,
      activePaths: [StateMachine],
      pathsToScopes: {},
      session: null,
      ownerDocument: undefined,
      fullPath: undefined,
      emitter: [EventEmitter],
      '$options': [Object]
    },
    isNew: false,
    errors: undefined,
    _doc: { _id: 5d207fc27c9ecf2f688d1f5f, name: 'Node Course' },
    '$init': true
  },

我希望输出仅为

 _doc:{_id:5d207fc27c9ecf2f688d1f5f,name:'Node Course' }

2 个答案:

答案 0 :(得分:0)

我猜你在用猫鼬。如果是这种情况,则可以基于此issue使用courses.toObject()

答案 1 :(得分:0)

面对相同的问题,您需要像这样遍历结果:

courses.forEach(course => {
    console.log(course.toObject());
});