聚合 returns 空数组 - 猫鼬
aggregate returns empty array - mongoose
我有以下两个简单的查询:
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
});
我的问题是,聚合函数 return 是一个空数组。他们不应该 return 相同的结果吗?
是的,但是您需要将 id(字符串)转换为 objectID :)
let idToSearch = mongoose.Types.ObjectId(req.params.id)
Comment.aggregate([{$match: { _id: idToSearch }}])
.exec(function(err, result) {
// result is now correct :)
});
我有以下两个简单的查询:
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
});
我的问题是,聚合函数 return 是一个空数组。他们不应该 return 相同的结果吗?
是的,但是您需要将 id(字符串)转换为 objectID :)
let idToSearch = mongoose.Types.ObjectId(req.params.id)
Comment.aggregate([{$match: { _id: idToSearch }}])
.exec(function(err, result) {
// result is now correct :)
});