为什么mongodb3.2.3报错不支持$filter呢
 发布于 9 年前  作者 pauky  4891 次预览  最后一次回复是 9 年前  来自 问答 

数据格式是: { time: {type: Date, required: true}, num: { type: Number, required: true}, countArticle: [ { _id: {type: String}, // 文章id hotScore: { type: Number} } ] } 目的是在聚合查询时过滤掉countArticle中_id不为’558a7590668170df629127d9的项

执行的mongo聚合查询命令是:

db.countHistory.aggregate([
                            {
                                // 修改输入文档的结构。可以用来重命名、增加或删除域,也可以用于创建计算结果以及嵌套文档。
                                $project: {
                                    time: 1,
                                    countArticle: {
                                        $filter: {
                                            input: "$countArticle",
                                            as: "item",
                                            cond: { $eq: [ "$$item._id", '558a7590668170df629127d9' ] }
                                        }
                                    }
                                }
                            }
                        ]
                    );

下面的命令在同个版本的测试环境下可执行,而在阿里云服务器上部署的mongodb则报错。报错信息: {“error”:true,“msg”:“MongoError: exception: invalid operator ‘$filter’”,“errorCode”:-1}