I have executed this code
db.courses.find({isPublished: true , tags: "backend"} , {name: 1 , author:1}).sort({name: 1})
in MongoDB shell and it gave me the required output. Now I am trying to execute the same code using mongoose and it gives me this
[
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
}
]
Although it gives me the required output along with some extra not understandable things which makes it non-readable.I am actually following a NodeJS course in which the instructer uses Mac and his output is same as the MongoDB output.My code in javascript is
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();
You need to convert Mongoose model to object.
You can convert to object using lean() function.
const result = await Course.find({isPublished: true
, tags: "backend"})
.sort({name: 1 })
.select({name: 1 , author: 1 })
.lean()
Also, it can be done by other way as,
let result = await Course.find({isPublished: true
, tags: "backend"})
.sort({name: 1 })
.select({name: 1 , author: 1 })
result = result.map(r => r.toObject())
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.