【发布时间】:2021-05-28 16:10:48
【问题描述】:
我已经执行了这段代码
db.courses.find({isPublished: true , tags: "backend"} , {name: 1 , author:1}).sort({name: 1})
在 MongoDB shell 中,它给了我所需的输出。现在我正在尝试使用猫鼬执行相同的代码,它给了我这个
[
model {
'$__': InternalCache {
strictMode: true,
selected: [Object],
shardval: undefined,
saveError: undefined,
validationError: undefined,
adhocPaths: undefined,
removing: undefined,
inserting: undefined,
version: undefined,
getters: {},
_id: 5a68fde3f09ad7646ddec17e,
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: 5a68fde3f09ad7646ddec17e,
name: 'ASP.NET MVC Course',
author: 'Mosh'
},
'$init': true
},
model {
'$__': InternalCache {
strictMode: true,
selected: [Object],
shardval: undefined,
saveError: undefined,
validationError: undefined,
adhocPaths: undefined,
removing: undefined,
inserting: undefined,
version: undefined,
getters: {},
_id: 5a68fdc3615eda645bc6bdec,
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: 5a68fdc3615eda645bc6bdec,
name: 'Express.js Course',
author: 'Mosh'
},
'$init': true
},
model {
'$__': InternalCache {
strictMode: true,
selected: [Object],
shardval: undefined,
saveError: undefined,
validationError: undefined,
adhocPaths: undefined,
removing: undefined,
inserting: undefined,
version: undefined,
getters: {},
_id: 5a68fdd7bee8ea64649c2777,
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: 5a68fdd7bee8ea64649c2777,
name: 'Node.js Course',
author: 'Mosh'
},
'$init': true
},
model {
'$__': InternalCache {
strictMode: true,
selected: [Object],
shardval: undefined,
saveError: undefined,
validationError: undefined,
adhocPaths: undefined,
removing: undefined,
inserting: undefined,
version: undefined,
getters: {},
_id: 5a68fe2142ae6a6482c4c9cb,
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: 5a68fe2142ae6a6482c4c9cb,
name: 'Node.js Course by Jack',
author: 'Jack'
},
'$init': true
}
]
虽然它给了我所需的输出以及一些额外的难以理解的东西,这使得它不可读。我实际上正在学习 NodeJS 课程,其中讲师使用 Mac,他的输出与 MongoDB 输出相同。我的代码在javascript是
const mongoose = require("mongoose");
const fun = async() => {
await mongoose.connect("mongodb://localhost:27017/Exercise");
const schema = mongoose.Schema({
name: String ,
author: String ,
tags: [String] ,
date: Date ,
isPublished: Boolean ,
price: Number
});
const Course = mongoose.model("Course" , schema);
const result = await Course.find({isPublished: true
, tags: "backend"})
.sort({name: 1 })
.select({name: 1 , author: 1 });
console.log(result);
}
fun();
【问题讨论】:
标签: javascript node.js mongodb mongoose nosql