San*_*nka 5 json go mongodb mgo
这个问题基于MongoDB,如何通过选择多个条件来检索所选项目.它就像Mysql中的IN条件
SELECT*FROM venuelist WHERE venueid IN(venueid1,venueid2)
我附加了我使用过的json数据结构.[参考:MONGODB的JSON结构].
例如,它在场地列表中有一个场所列表,它有几个属性场所ID和用户代理名称和总计数之和作为值.用户代理意味着用户Os,浏览器和设备信息.在这种情况下,我使用os发行版.在这种情况下,我计算linux,ubuntu指望特定的sceneid.
就是这样,
"sum" : [
    {
        "name" : "linux",
        "value" : 12
    },
    {
        "name" : "ubuntu",
        "value" : 4
    }
],
最后,我想通过在MongoDB中的一个查找查询中选择venueid列表来计算所有linux用户数.
例如,我想通过调整场地ID VID1212或VID4343来选择所有linux用户数
参考:MONGODB的JSON结构
{
    "_id" : ObjectId("57f940c4932a00aba387b0b0"),
    "tenantID" : 1,
    "date" : "2016-10-09 00:23:56",
    "venueList" : [
        {
            "id" : “VID1212”,
            "sum" : [
                {
                      "name" : "linux",
                      "value" : 12
                },
                {
                    "name" : "ubuntu",
                    "value" : 4
                }
            ],
            “ssidList” : [    // this is list of ssid’s in venue
                {
                    "id" : “SSID1212”,
                    "sum" : [
                        {
                            "name" : "linux",
                            "value" : 8
                        },
                        {
                            "name" : "ubuntu",
                            "value" : 6
                        }
                    ],
                    “macList” : [  // this is mac list inside particular ssid  ex: this is mac list inside the SSID1212
                        {
                            "id" : “12:12:12:12:12:12”,
                            "sum" : [
                                {
                                    "name" : "linux",
                                    "value" : 12
                                },
                                {
                                    "name" : "ubuntu",
                                    "value" : 1
                                }
                            ]
                        }
                    ]
                }
            ]
        },
        {
            "id" : “VID4343”,
            "sum" : [
                {
                     "name" : "linux",
                     "value" : 2
                }
            ],
            "ssidList" : [
                {
                    "id" : “SSID4343”,
                    "sum" : [
                        {
                            "name" : "linux",
                            "value" : 2
                        }
                    ],
                    "macList" : [
                        {
                            "id" : “43:43:43:43:43:34”,
                            "sum" : [
                                {
                                    "name" : "linux",
                                    "value" : 2
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    ]
}
我正在使用golang作为语言来使用mgo.v2包来使用mongoldb处理数据
预计出局是:
产量
- linux:12 + 2 = 14
- ubuntu:4 + 0 = 4
不要考虑venuelist中的内部列表.
您需要使用聚合框架,在其中运行聚合管道,该管道首先使用运算符根据venueListid过滤集合中的文档$match。
第二条管道将需要展平venueList和sum子文档数组,以便将文档中的数据作为非规范化条目在管道中进一步处理。运算$unwind符在这里很有用。
展开后需要进一步使用过滤器$match,以便仅允许您要聚合的文档进入下一个管道。
主管道将是$group运算符阶段,它聚合过滤后的文档以使用累加器运算符创建所需的总和$sum。为了获得所需的结果,您需要使用一元运算符来创建独立的计数字段,因为这将根据名称值$cond将文档数量提供给表达式。$sum
总而言之,考虑运行以下管道:
db.collection.aggregate([
    { "$match": { "venueList.id": { "$in": ["VID1212", "VID4343"] } } },
    { "$unwind": "$venueList" },
    { "$match": { "venueList.id": { "$in": ["VID1212", "VID4343"] } } },
    { "$unwind": "$venueList.sum" },    
    {
        "$group": {
            "_id": null,
            "linux": {
                "$sum": {
                    "$cond": [ 
                        { "$eq": [ "$venueList.sum.name", "linux" ] }, 
                        "$venueList.sum.value", 0 
                    ]
                }
            },
            "ubuntu": {
                "$sum": {
                    "$cond": [ 
                        { "$eq": [ "$venueList.sum.name", "ubuntu" ] }, 
                        "$venueList.sum.value", 0 
                    ]
                }
            }
        }
    }
])
对于 mGo 的使用,您可以使用http://godoc.org/labix.org/v2/mgo#Collection.Pipe中的指南转换上述管道
要获得更灵活、性能更好的替代方案,其执行速度比上述方案快得多,并且还考虑总和列表的未知值,请按如下方式运行替代管道
db.collection.aggregate([
    { "$match": { "venueList.id": { "$in": ["VID1212", "VID4343"] } } },
    { "$unwind": "$venueList" },
    { "$match": { "venueList.id": { "$in": ["VID1212", "VID4343"] } } },
    { "$unwind": "$venueList.sum" },    
    { 
        "$group": {
            "_id": "$venueList.sum.name",
            "count": { "$sum": "$venueList.sum.value" }
        }
    },
    { 
        "$group": {
            "_id": null,
            "counts": {
                "$push": {
                    "name": "$_id",
                    "count": "$count"
                }
            }
        }
    }
])
| 归档时间: | 
 | 
| 查看次数: | 384 次 | 
| 最近记录: |