javascript - 为什么在控制台中打印 mongoose 的模型对象时会看到额外的字段。我正在将节点 JS 与猫鼬一起使用。我怎样才能隐藏这些属性
问题描述
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/playgroundDB')
.then(()=>console.log('Connected to Mongo DB....'))
.catch(( err)=>console.log('Could not connect to MongoDb', err));
const courseSchema = new mongoose.Schema({
name : String,
author : String,
tags :[String],
date:{ type: Date, default:Date.now},
isPublished: Boolean
});
const Course= mongoose.model('Course', courseSchema);
const course = new Course({
name : "xxxx",
author : 'rahul123',
tags :['Frontend', 'Backend'] ,
isPublished : true
});
course.save().then(res=>console.log(res)).catch(err=> console.log(err));
当我执行上面的代码时。我的数据保存在 db 中,当我使用 打印它时(res=>console.log(res))
,我在控制台中看到了额外的字段。
下面是我在控制台中的输出
model {
'$__': InternalCache {
strictMode: true,
selected: undefined,
shardval: undefined,
saveError: undefined,
validationError: undefined,
adhocPaths: undefined,
removing: undefined,
inserting: true,
version: undefined,
getters: {},
_id: 5dea6a7256bf9212c81361a9,
populate: undefined,
populated: undefined,
wasPopulated: false,
scope: undefined,
activePaths: StateMachine {
paths: {},
states: [Object],
stateNames: [Array],
forEach: [Function],
map: [Function]
},
pathsToScopes: {},
ownerDocument: undefined,
fullPath: undefined,
emitter: EventEmitter {
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: 0
},
'$options': true
},
isNew: false,
errors: undefined,
_doc: {
tags: [
'Frontend',
'Backend',
toBSON: [Function: toBSON],
_atomics: {},
_parent: [Circular],
_cast: [Function: _cast],
_markModified: [Function: _markModified],
_registerAtomic: [Function: _registerAtomic],
'$__getAtomics': [Function: $__getAtomics],
hasAtomics: [Function: hasAtomics],
_mapCast: [Function: _mapCast],
push: [Function: push],
nonAtomicPush: [Function: nonAtomicPush],
'$pop': [Function: $pop],
pop: [Function: pop],
'$shift': [Function: $shift],
shift: [Function: shift],
pull: [Function: pull],
splice: [Function: splice],
unshift: [Function: unshift],
sort: [Function: sort],
addToSet: [Function: addToSet],
set: [Function: set],
toObject: [Function: toObject],
inspect: [Function: inspect],
indexOf: [Function: indexOf],
remove: [Function: pull],
_path: 'tags',
isMongooseArray: true,
validators: [],
_schema: [SchemaArray]
],
date: 2019-12-06T14:49:22.524Z,
_id: 5dea6a7256bf9212c81361a9,
name: 'xxxx',
author: 'rahul123',
isPublished: true,
__v: 0
}
}
正如您在上一节中看到的,我在上一节中看到了我保存的数据。但为什么我会看到这么多额外的属性。我已经用 Mongo DBversion 3.6.16
和4.2
.
我怎样才能摆脱这些领域。
解决方案
I had the same issue. The clean way to fix this is to add toObject()
in the console statement as below:
(res=>console.log(res.toObject()))
This should remove the extra unwanted fields.
推荐阅读
- c++ - 在 T 的静态成员中使用 sizeof(T) 的 c++ 错误
- r - 根据行号矩阵对数据帧进行子集化并将结果保存在一个列表中
- ssl - 如何使 HAProxy 的 SSL 重定向和路径重写(使用 reqrep)同时工作?
- python-2.7 - 操作超时 Splunk + Python
- mysql - 在 mySQL Server 客户端上设置远程访问引发错误 1064 (42000)
- android - ACTION_IMAGE_CAPTURE 返回图像质量差的位图,我在哪里可以获得高分辨率图像?
- android - 从应用服务器自动发送推送通知
- vb.net - 将列表框项目分成两部分并将它们粘贴到两个列表框中
- c# - GetCursorInfo WinForms 与 WPF C#
- java - 如何根据用户输入在循环中声明无限变量?