Need a proper way of expressing Pipeline for the following aggregation query

324 views
Skip to first unread message

R. Martinez

unread,
Aug 4, 2015, 3:48:47 AM8/4/15
to mgo-users
Hello,

I've been doing program conversion from (PHP) to go-lang for the past 4 days just so I can learn "golang" in a short period of time 
while still being productive. So forgive me if my question is leaning towards how to express MongoDB aggregation concepts in "go".

I have this aggregation query:



db.apps.aggregate(
  [
    { $match: { "vh.pt_id" : {$gt: 0}} },
    
    { $group: {
        _id: {$concat: [{$substr: ["$vh.pt_id",0,-1]}, "_", {$substr: ["$vh.vdid",0,-1]}, "_",  "$vh.vvl"]},
        yids: {$first: "$vh.yids"},
        yvls: {$first: "$vh.yvls"},
        pt_id: {$first: "$vh.pt_id"},
        mkid: {$first: "$vh.mkid"},
        mdid: {$first: "$vh.mdid"},
        sbid: {$first: "$vh.sbid"},
        eid: {$first: "$vh.eid"},
        leid: {$first: "$vh.leid"},
      }
    },
    { $out: "tmp_ptvh_map_stage2"}
  ]
);


and my current code is:



  pipeline := []bson.D{
    bson.M{"$match" : bson.M{"vh.pt_id"} : bson.M{"$gt" : 0}},
    
    bson.M{"$group" : bson.M{
 
      "_id" : bson.M{
        "$concat" : []bson.D{
          bson.M{"$substr" : ???????}
        }
      },
 
      "yids" : bson.M{"$first" : "$vh.yids"},
      "yvls" : bson.M{"$first" : "$vh.yvls"},
      "pt_id" : bson.M{"$first" : "$vh.pt_id"},
      "mkid" : bson.M{"$first" : "$vh.mkid"},
      "mdid" : bson.M{"$first" : "$vh.mdid"},
      "sbid" : bson.M{"$first" : "$vh.sbid"},
      "eid" : bson.M{"$first" : "$vh.eid"},
      "leid" : bson.M{"$first" : "$vh.leid"},
      }
    },
                 ....



I really don't know how to express the following lines to go/mgo/bson:

        _id: {$concat: [{$substr: ["$vh.pt_id",0,-1]}, "_", {$substr: ["$vh.vdid",0,-1]}, "_",  "$vh.vvl"]},


I'm stuck with these:

      "_id" : bson.M{
        "$concat" : []bson.D{
          bson.M{"$substr" : ???????}
        }
      },

With my limited go-lang knowledge, I know that arrays and slice will only accept one data type. But I'm sure how to fully express 

      {$substr: ["$vh.pt_id",0,-1]}

into go/mgo/bson:

      bson.M{"$substr": []?????}
 

Thanks in advance!

Regards,
Raul



Gustavo Niemeyer

unread,
Aug 18, 2015, 11:24:24 PM8/18/15
to mgo-...@googlegroups.com
Hi there,

Slices are fully supported by mgo/bson, so you can use slice of structs, slice of maps, and even slice of plain values.

For example, this would work with your example:

    bson.M{"$substr": []interface{}{"$vh.pt_id", 0, -1}}

The double braces may look strange if you're new to Go, but the reason is more obvious if you consider that the above is equivalent to this:

    type anyValue interface{}
    type someList []anyValue

    bson.M{"$substr": someList{$vh.pt_id", 0, -1}}

Please let us know if you need anything else.


--
You received this message because you are subscribed to the Google Groups "mgo-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mgo-users+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

R. Martinez

unread,
Aug 19, 2015, 12:27:42 AM8/19/15
to mgo-users

Hi Gustavo,

Thank you for replying and thanks for the alternative type definition, it will really help make things readable for Go-newbies like myself.

Regards!
Reply all
Reply to author
Forward
0 new messages