[英]MongoDB query work but not with Mongoose
I'm trying to query a collection with Mongoose, there is the collection sample output with a simple db.course.find()
query 我正在尝试使用Mongoose查询集合,有使用简单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"
}
I have a MongoDB query that work well 我有一个运行良好的MongoDB查询
db.course.find({'calendar_id.$id': ObjectId("581c5972fd1c59295c34f1b8")}).sort({date: 1})
I am trying to do the same query with Mongoose, in my NodeJS app I made this query but this one return an empty array because the ObjectId
is not working well. 我试图用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
is from my model file and is like this Course
来自我的模型文件,就像这样
const Course = mongoose.model('course', {
calendar_id: Schema.ObjectId,
date: Number,
title: String,
teacher: String,
start_at: String,
end_at: String
});
How can I make this Mongoose query to work ? 如何使此Mongoose查询正常工作? The model is maybe not properly formed ? 该模型可能格式不正确?
Use Course.find({'calendar_id': id})
instead of Course.find({'calendar_id.$id': id})
. 使用Course.find({'calendar_id': id})
而不是Course.find({'calendar_id.$id': id})
。
For mongoose $id does not exist. 对于猫鼬$ id不存在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.