FieldPath字段名称不得包含“.".在$ group中 [英] FieldPath field names may not contain '.' in $group

查看:129
本文介绍了FieldPath字段名称不得包含“.".在$ group中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下看起来像这样的mongo数据

I have the following mongo data which looks like this

{
    eventType : "mousedown",
    eventArgs : {
        type : "touchstart",
        elementId : "id1"
    },
    creationDateTime : ISODate("2017-02-24T07:05:49.986Z")
}

我编写了以下查询以进行组计数.

I wrote the following query to perform group count.

db.analytics.aggregate
(
    {
        $match :
        {
            $and : 
            [
                {"eventArgs.type" : 'touchstart'}, 
                {eventType : 'mousedown'}, 
                {creationDateTime : {$gte : ISODate("2017-02-24T000:00:00.000Z")}}
            ]
        }
    },
    {
        $group : 
        {
            _id : 
            {
                "eventsArgs.elementId" : "$elementId"
            },
            count : 
            {
                $sum : 1
            }
        }
    }
);

我在$group时遇到错误,该信息指出

I'm getting error for $group, which states that

FieldPath field names may not contain '.'

如果我无法指定'.'.在

If I were not able to specific '.' in

        $group : 
        {
            _id : 
            {
                "eventsArgs.elementId" : "$elementId"
            },

这样做的正确方法是什么?

What is the correct way to do so?

推荐答案

由于您只有一个分组字段,因此最好的方法是只在该字段上使用_id分组键,然后创建另一个 $project 将会重塑的管道从前一个管道中的_id键进入所需的所需子文档.例如

Since you have a single group field, the best way is to just use the _id group key on that field and then create another $project pipeline that will reshape the _id key from the previous pipeline into the desired subdocument that you want. For example

db.analytics.aggregate([
    {
        "$match": {
            "eventArgs.type": 'touchstart', 
            "eventType": 'mousedown', 
            "creationDateTime": { "$gte": ISODate("2017-02-24T000:00:00.000Z") } 
        }
    },
    {
        "$group": {
            "_id": "$eventArgs.elementId",
            "count": { "$sum": 1 }
        }
    },
    {
        "$project": {
            "eventsArgs.elementId": "$_id",
            "count": 1, "_id": 0
        }
    }
]);

以下内容也应工作:

db.analytics.aggregate([
    {
        "$match": {
            "eventArgs.type": 'touchstart', 
            "eventType": 'mousedown', 
            "creationDateTime": { "$gte": ISODate("2017-02-24T000:00:00.000Z") } 
        }
    },
    {
        "$group": {
            "_id": {
               "eventArgs": {
                   "elementId": "$eventArgs.elementId"
               }
            },
            "count": { "$sum": 1 }
        }
    }
]);

这篇关于FieldPath字段名称不得包含“.".在$ group中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆