本文介绍了通过在golang中检查MongoDB中的多个属性值来检索项目列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个基于MongoDB的问题,如何通过选择多个条件来检索选定的项目。它就像在Mysql中的IN条件。

我附加了json使用的数据结构。

就是这样,

 sum:[
{
name:linux,
value:12

$ name $ b $ value $ 4





$ b $ >



最后,我想通过在MongoDB中的一个find查询中选择venueid列表来计算所有linux用户数。



例如,我想通过调节场所ID VID1212 来选择所有Linux用户数。

Ref:JSON STRUCTUE OF MONGODB

  {
_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:[//这是ssid在场地中的列表
{
id:SSID1212,
sum:[
{
name:linux,
value:8
},
{
name :ubuntu,
value:6
}
],
macList:[//这是特定ssid内的mac列表ex:这是mac列表在SSID1212
{
id:12:12:12:12:12 :
$

$ name $ b $ value $


$
name:ubuntu,
value:1
}
]
}
]
}
]
},
{
id:VID4343,
sum:[
{
name:linux,
value:2
}
],
ssidList:[
{
id:SSID4343,
总和:[
{
name:linux,
value:2
}
],
macList:[
{
id: 43:43:43:43:43:34,
sum:[
{
name:linux,
value:2
}
]
}
]
}
]
}
]
}

我使用golang作为语言来使用mgo.v2包处理mongoldb的数据


解决方案

您需要使用聚合框架,您将在其中运行一个聚合管道,该聚合管道首先根据
venueList 使用 运算符在这里很有用。


使用 必须在展开后放置,以便只允许要合并的文档进入下一个管道。



主要管道是 运算符阶段,它使用累加器运算符汇总已过滤的文档以创建所需的总和 即可。对于期望的结果,您需要使用像 来创建独立计数字段,因为这会将文档数量提供给



完全可以考虑运行以下管道:

 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
]


$ u $ $ $ $ b $ $ $ $ $ b $ $ $ cond $ $ b $ { :[$ venueList.sum.name,ubuntu]},
$ venueList.sum.value,0
]
}
}
}






对于mGo的用法,您可以将以上管道使用






对于更灵活,更好的高性能替代方案,其执行速度比以上,还考虑了总和lis的未知值t,运行替代流水线如下所示:

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


This question based on MongoDB,How to retrieve selected items retrieve by selecting multiple condition.It is like IN condition in Mysql

I have attached json data structure that I have used.[Ref: JSON STRUCTUE OF MONGODB ].

As an example, it has a venueList then inside the venue list, It has several attribute venue id and sum of user agents name and total count as value.user agents mean user Os,browser and device information. In this case I used os distribution.In that case i was count linux,ubuntu count on particular venueid.

it is like that,

"sum" : [
    {
        "name" : "linux",
        "value" : 12
    },
    {
        "name" : "ubuntu",
        "value" : 4
    }
],

Finally I want to get count of all linux user count by selecting venueid list in one find query in MongoDB.

As example, I want to select all count of linux users by conditioning if venue id VID1212 or VID4343

Ref: JSON STRUCTUE OF MONGODB

{
    "_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
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    ]
}

I am using golang as language to manipulation data with mongoldb using mgo.v2 package

解决方案

You'd need to use the aggregation framework where you would run an aggregation pipeline that first filters the documents in the collection based on the venueList ids using the $match operator.

The second pipeline would entail flattening the venueList and sum subdocument arrays in order for the data in the documents to be processed further down the pipeline as denormalised entries. The $unwind operator is useful here.

A further filter using $match is necessary after unwinding so that only the documents you want to aggregate are allowed into the next pipeline.

The main pipeline would be the $group operator stage which aggregates the filtered documents to create the desired sums using the accumulator operator $sum. For the desired result, you would need to use a tenary operator like $cond to create the independent count fields since that will feed the number of documents to the $sum expression depending on the name value.

Putting this altogether, consider running the following pipeline:

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

For usage with mGo, you can convert the above pipeline using the guidance in http://godoc.org/labix.org/v2/mgo#Collection.Pipe


For a more flexible and better performant alternative which executes much faster than the above, and also takes into consideration unknown values for the sum list, run the alternative pipeline as follows

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

这篇关于通过在golang中检查MongoDB中的多个属性值来检索项目列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 10:33