我正在尝试用猫鼬查询一个集合,有一个简单db.course.find()
查询的集合样本输出
{
"_id" : ObjectId("581c9408fc01b15cb21043e4"),
"calendar_id" : DBRef("calendar", ObjectId("581c5972fd1c59295c34f1b8"), "ecampus"),
"date" : 1478473200000,
"title" : "Conception et planification BI",
"teacher" : "fiolet gilles",
"start_at" : "08:30",
"end_at" : "12:30"
}
我有一个运行良好的MongoDB查询
db.course.find({'calendar_id.$id': ObjectId("581c5972fd1c59295c34f1b8")}).sort({date: 1})
我正在尝试用Mongoose进行相同的查询,在我的NodeJS应用中,我进行了此查询,但是由于返回ObjectId
效果不佳,该返回了一个空数组。
let mongoose = require('mongoose');
let ObjectId = mongoose.Types.ObjectId;
let id = new ObjectId(session.calendar_id);
Course.find({'calendar_id.$id': id}).sort({date: 1}).exec(function (err, courses) {
console.log(err, courses);
createJsonBody(courses);
});
Course
来自我的模型文件,就像这样
const Course = mongoose.model('course', {
calendar_id: Schema.ObjectId,
date: Number,
title: String,
teacher: String,
start_at: String,
end_at: String
});
如何使此Mongoose查询正常工作?该模型可能格式不正确?
使用Course.find({'calendar_id': id})
代替Course.find({'calendar_id.$id': id})
。
对于猫鼬$ id不存在。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句