• FieldPath field names may not contain '.' in $group


    FieldPath field names may not contain '.' in $group

    由 匿名 (未验证) 提交于 2018-05-23 09:55:55

    • 登录 发表评论
    • 102 次浏览

    可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):

    选择语言中文(简体)日语英语中文(繁体)

    由 Google 翻译翻译强力驱动

    问题:

    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
                }
            }
        }
    );

    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?

    回答1:

    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
            }
        }
    ]);

    The following should work as well:

    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 }
            }
        }
    ]);
  • 相关阅读:
    python3安装 feedparser
    numpy
    Git详细教程(1)---个人Git的基本使用
    JavaScript高级程序设计---学习笔记(五)
    JavaScript高级程序设计---学习笔记(四)
    JavaScript高级程序设计---学习笔记(三)
    JavaScript高级程序设计---学习笔记(二)
    JavaScript高级程序设计---学习笔记(一)
    JavaScript 基础阶段测试题
    JS实现购物车特效
  • 原文地址:https://www.cnblogs.com/grj001/p/12225119.html
Copyright © 2020-2023  润新知