MongoDb - 查询 - 即使它不为空也是空的结果

时间:2018-04-27 01:26:42

标签: mongodb spring-repositories

我尝试为MongoDB编写一些聚合查询。我是新手,所以我有问题找到问题所在:

{
    "aggregate": "__collection__",
    "pipeline": [
        {
            "$unwind": "$studia"
        },
        {
            "$group": {
                "_id": {
                    "course": "$course",
                    "start": "$start"
                },
                "n": {
                    "$sum": 1
                }
            }
        },
        {
            "$sort": {
                "n": -1
            }
        }
    ]
}

此查询由Java生成。它应该计算和总结在某个日期开始的一些课程的次数。但它返回空结果。那是错的。

我的文件是:

  {
        "_id": "1006262",
        "name": "Kugujalu",
        "lname": "Najahalovu",
        "studia": [
            {
                "id": "992",
                "start": {
                    "$date": "1998-08-31T22:00:00.000Z"
                },
                "end": {
                    "$date": "2003-06-10T22:00:00.000Z"
                },
               "course": "MCH",
            }
        ],
        "_class": "com.mongodb.MongoStudent"
    }
 {
        "_id": "1006263",
        "name": "Elkjuu",
        "lname": "Xjikuju",
        "studia": [
            {
                "id": "992",
                "start": {
                    "$date": "1998-08-31T22:00:00.000Z"
                },
                "end": {
                    "$date": "2003-06-10T22:00:00.000Z"
                },
               "course": "MCH",
            }
        ],
        "_class": "com.mongodb.MongoStudent"
    }

所以我想要查询返回结果如:

"start": 1998-08-31T22:00:00.000Z, "course": "MCH", "n": 2

任何想法,哪里有问题?

这会生成我的查询:

Aggregation agg = newAggregation(unwind("studia"), group("course","start").count().as("n"),
                sort(Sort.Direction.DESC, "n"));

0 个答案:

没有答案