Get a list of items by checking multiple attribute values ​​in MongoDB in golang

This question is based on MongoDB, How to get selected items by selecting, selecting multiple conditions. This is similar to the IN condition in Mysql

SELECT * FROM venuelist WHERE siteid IN (siteid1, siteid2)

I added the json data structure that I used. [Ref: JSON STRUCTUE OF MONGODB] .

As an example, it has a local list in the list of places, it has several place identifiers for the place and the sum of the user agent name and the total as a value. User agents mean Os user, browser information, and device information. In this case, I used the os distribution. In this case, I counted linux, ubuntu was counting on a specific eventid.

This is true,

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

Finally, I want to get the count of all linux users by selecting the siteid list in a single search query in MongoDB.

As an example, I want to select the entire number of Linux users by setting if the place identifier is VID1212 or VID4343

Reference: 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 use golang as a data processing language using mongoldb using the mgo.v2 package

Expected Result:

Output

  • linux: 12 + 2 = 14
  • ubuntu: 4 + 0 = 4

Do not take into account the internal list in venuela.

+5
source share
1 answer

You will need to use the aggregation infrastructure, where you will run the aggregation pipeline, which first filters documents in the collection based on venueList identifiers using the $match operator.

The second pipeline will entail smoothing the arrays of the venueList and sum sub- venueList so that the data in the documents is processed further down the pipeline as denormalized records. The $unwind operator is useful here.

After unwinding, you need the following filter using $match , so that only the documents you want to copy are allowed into the next pipe.

The main pipeline will be the stage of the $group operator, which combines the filtered documents to create the desired amounts using the battery operator $sum . For the desired result, you will need to use a shadow operator, for example, $cond , to create independent counter fields since which will feed the number of documents into the $sum expression depending on the value of the name.

Introducing this as a whole, consider 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 use with mGo, you can convert the above pipeline using the manual at http://godoc.org/labix.org/v2/mgo#Collection.Pipe


For a more flexible and better alternative, which runs much faster than above, and also takes into account unknown values ​​for the list of sums, 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" } } } } ]) 
+2
source

Source: https://habr.com/ru/post/1257930/


All Articles