聚合返回空数组-猫鼬 [英] aggregate returns empty array - mongoose
本文介绍了聚合返回空数组-猫鼬的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下两个简单查询:
I have the following two simple queries:
Comment.aggregate([{$match: { _id: req.params.id }}])
.exec(function(err, result) {
// result is empty
});
Comment.find({ _id: req.params.id })
.exec(function (err, result) {
// correct result returned
});
我的问题是,聚合函数返回一个空数组.他们不应该返回相同的结果吗?
My problem is, that the aggregate-Function returns an empty array. Aren't they supposed to return the same result?
推荐答案
是的,但是您需要将id(它是一个字符串)强制转换为objectID:)
Yes, but you need to cast the id (which is a string) to an objectID :)
let idToSearch = mongoose.Types.ObjectId(req.params.id)
Comment.aggregate([{$match: { _id: idToSearch }}])
.exec(function(err, result) {
// result is now correct :)
});
这篇关于聚合返回空数组-猫鼬的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文