通过在golang中检查MongoDB中的多个属性值来检索项目列表 [英] Retrieve item list by checking multiple attribute values in MongoDB in golang
问题描述
这个基于MongoDB的问题,如何通过选择多个条件来检索选定的项目。它就像在Mysql中的IN条件。
SELECT * FROM venuelist WHERE venueid IN(venueid1,venueid2)
我附加了json使用的数据结构。 就是这样,
sum:[
{
name:linux,
value:12
$ name $ b $ value $ 4
$ b $ >
最后,我想通过在MongoDB中的一个find查询中选择venueid列表来计算所有linux用户数。
例如,我想通过调节场所ID VID1212 或 Ref:JSON STRUCTUE OF MONGODB 我使用golang作为语言来使用mgo.v2包处理mongoldb的数据 预计输出为: 输出 您需要使用聚合框架,您将在其中运行一个聚合管道,该聚合管道首先根据 第二个管道需要展平 使用 主要管道是 完全可以考虑运行以下管道: 对于mGo的用法,您可以将以上管道使用 http://godoc.org/labix.org/v2中的指导/mgo#Collection.Pipe 对于更灵活,更好的高性能替代方案,其执行速度比以上,还考虑了总和lis的未知值t,运行替代流水线如下所示: This question based on MongoDB,How to retrieve selected items retrieve by selecting multiple condition.It is like IN condition in Mysql SELECT * FROM venuelist WHERE venueid IN (venueid1, venueid2) 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, 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 I am using golang as language to manipulation data with mongoldb using mgo.v2 package expected out put is : output Don't consider inner list in venuelist.
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 The second pipeline would entail flattening the A further filter using The main pipeline would be the Putting this altogether, consider running the following pipeline: 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
这篇关于通过在golang中检查MongoDB中的多个属性值来检索项目列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
{
_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
}
]
}
]
}
]
}
]
}
venueList
使用 $ 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
]
$ u $ $ $ $ b $ $ $ $ $ b $ $ $ cond $ $ b $ { :[$ venueList.sum.name,ubuntu]},
$ venueList.sum.value,0
]
}
}
}
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
}
}
}
}
])
"sum" : [
{
"name" : "linux",
"value" : 12
},
{
"name" : "ubuntu",
"value" : 4
}
],
{
"_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
}
]
}
]
}
]
}
]
}
venueList
ids using the $match
operator. 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. $match
is necessary after unwinding so that only the documents you want to aggregate are allowed into the next pipeline.$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.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
]
}
}
}
}
])
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"
}
}
}
}
])