javascript - 我只想 console.log() 我的数据存在于我的 MongoDB 数据库中
问题描述
我在 mongodb 的数据库中输入数据,并希望 console.log() 输入的数据,但每当我尝试这样做时,我的数据确实会进入控制台,但具有我不需要的各种其他属性。我只想带上我控制台上输入的数据!
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();
我想在控制台上显示的是(我的 mongo 数据库中的实际数据)
[ {"_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"} } ]
但是我得到的是我的数据,但是有这些额外的信息:-
[
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
}
]
当我 console.log 我的班级时,我不想要这些额外的信息以及我的数据!
解决方案
我看到了问题。我正在运行 mongoose 5.0.1,刚刚更新了 mongoose 包,它停止显示其他属性!
推荐阅读
- airflow - DAG cli 和追赶
- php - 管理员如何在登录时增加当前会话?
- forms - 在 octoberCMS 的 datepicker 中手动输入日期后禁用保存表单
- json - 使用 Mule 中的 Dataweave 将分隔符填充转换为 JSON 格式
- javascript - node.js 中 python 的 __getitem__
- vhdl - VHDL - (modelsim) - 在 CASE 测试台期间显示的致命错误
- java - Pojo 到 Json 架构
- exception - Apache Flink 抛出“分区已完成”异常
- android - 如何在 Scrollview 中停止滚动 Recyclerview?
- figma - Figma 如何准确裁剪图片?