【发布时间】:2023-04-09 02:22:01
【问题描述】:
我有一个线程集合,每个线程都有一个嵌套的评论文档数组。 我只想返回一个基于其 ID 的评论文档。 我有线程 ID 和评论 ID。 唉,我似乎做不到 - 搜索我已经想出了以下但我得到一个错误。
{ [MongoError: Unsupported projection option: $elemMatch] name: 'MongoError' }
这似乎是一个非常典型的用例,谁能指出我哪里出错了?
var thread_id = vo.thread_id;
var _id = vo._id;
threads.model.find({_id:thread_id}).select({ comments: { $elemMatch: {_id:_id}}}).exec(function (err, thread) {
console.log("***************************************");
console.log(err);
console.log(thread);
done();
});
【问题讨论】:
-
经过进一步研究,我运行的 mongo db 的安装似乎是 1.6.2,$elemMatch 是在 2.2 版中引入的。 docs.mongodb.org/manual/reference/projection/elemMatch