具有下划线的MongoDB聚合字段

时间:2017-02-08 11:55:47

标签: mongodb aggregation-framework

我的文档中有一个字段@Override @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "ID", nullable = false) public D getDObj() { return DObj; } ,如下所示:

_type_

我需要在该字段上进行聚合:

{
    "name" : "0",
    "_type_" : "product"
}

当该字段没有下划线时,它会起作用,但这样我得到了

db.readImport.aggregate([
    {
        $match: {
            "$_type_": "product"
        }
    },
    ...
]);
  • 如何使用unknown top level operator: $_type_
  • 访问字段_type_

1 个答案:

答案 0 :(得分:1)

$ match stage阶段你不需要$

db.readImport.aggregate([
    {
        $match: { "_type_": "product" }
    },
    ...
]);

因为$ match stage接受简单查询作为参数。其他聚合阶段(例如$ group)接受表达式。表达式使用filed path来访问输入文档的字段。

相关问题