如何在猫鼬中使用 find() 函数获取选定的输出?
How to just get a the selected output using find() function in mongoose?
我已经执行了这段代码
db.courses.find({isPublished: true , tags: "backend"} , {name: 1 , author:1}).sort({name: 1})
in MongoDB shell 它给了我所需的输出。现在我正在尝试使用 mongoose 执行相同的代码,它给了我这个
[
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
}
]
虽然它给了我所需的输出以及一些额外的不可理解的东西,这使得它非 readable.I 我实际上正在学习 NodeJS 课程,其中讲师使用 Mac 并且他的输出是相同的因为 MongoDB output.My 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();
您需要将 Mongoose 模型转换为对象。
您可以使用 lean() 函数转换为对象。
const result = await Course.find({isPublished: true
, tags: "backend"})
.sort({name: 1 })
.select({name: 1 , author: 1 })
.lean()
此外,可以通过其他方式完成,
let result = await Course.find({isPublished: true
, tags: "backend"})
.sort({name: 1 })
.select({name: 1 , author: 1 })
result = result.map(r => r.toObject())
我已经执行了这段代码
db.courses.find({isPublished: true , tags: "backend"} , {name: 1 , author:1}).sort({name: 1})
in MongoDB shell 它给了我所需的输出。现在我正在尝试使用 mongoose 执行相同的代码,它给了我这个
[
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
}
]
虽然它给了我所需的输出以及一些额外的不可理解的东西,这使得它非 readable.I 我实际上正在学习 NodeJS 课程,其中讲师使用 Mac 并且他的输出是相同的因为 MongoDB output.My 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();
您需要将 Mongoose 模型转换为对象。
您可以使用 lean() 函数转换为对象。
const result = await Course.find({isPublished: true
, tags: "backend"})
.sort({name: 1 })
.select({name: 1 , author: 1 })
.lean()
此外,可以通过其他方式完成,
let result = await Course.find({isPublished: true
, tags: "backend"})
.sort({name: 1 })
.select({name: 1 , author: 1 })
result = result.map(r => r.toObject())