$match and $lookup in Go

1,515 views
Skip to first unread message

vineet daniel

unread,
Feb 8, 2016, 1:31:27 PM2/8/16
to golan...@googlegroups.com, mongod...@googlegroups.com, mgo-...@googlegroups.com
Hello,

How can we write similar query in Go (mgo) :

db.followers.aggregate([   {$match: {     fserid: ObjectId("56b329fa2620e5438555992a")}   },    {$lookup: {     from: "users",      localField: "fuserid",     foreignField: "_id",     as: "postcode_docs"}   } ]);




​I tried this but it throws tons of errors :

iter := uc.session.DB("
​DB
").C("
​Collection
").Pipe([]bson.M{"$match": {"userid": bson.ObjectIdHex("56af269e2620e541de0755c1")}}, []bson.M{{"$lookup": bson.M{"from": "users", "localField": "fuserid", "foreignField": "_id", "as": "postcode_docs"}}}).Iter()

Regards
Vineet Daniel
Cell          : +918527565696
Websites : 
Blog   |   Linkedin  |  Twitter

Wan Bachtiar

unread,
Feb 9, 2016, 12:14:53 PM2/9/16
to mongodb-user, golan...@googlegroups.com, mgo-...@googlegroups.com

How can we write similar query in Go (mgo) :

Hi Vineet,

Below is a sample code that runs aggregation using $match and $lookup based on your query:

collection := session.DB("test").C("followers")
pipeline := []bson.M{ 
        bson.M{"$match": 
            bson.M{"fuserid": bson.ObjectIdHex("56b329fa2620e5438555992a")}},
        bson.M{"$lookup": 
            bson.M{ "from": "users", "localField": "fuserid", "foreignField": "_id", "as": "postcode_docs"}},
}
pipe := collection.Pipe(pipeline)
resp := []bson.M{}
err = pipe.All(&resp)

The snippet above was tested on Golang v1.5.3,  gopkg.in/mgo.v2, and MongoDB v3.2.1


​I tried this but it throws tons of errors :

If you are still having issues, it would be useful to post the actual errors you are getting.

Best regards,

Wan

Ron Dyck

unread,
May 26, 2016, 2:33:10 PM5/26/16
to mgo-users, golan...@googlegroups.com, mongod...@googlegroups.com
I'm having a similar problem, specifically the error is:
interface conversion: interface is bson.ObjectId, not string
My query works fine in a terminal (or Robomongo) but not in  mgo. Both localField and foreignField are ObjectId's then am I correct in understanding mgo requires a string? If so how would I accomplish that?

Any help is appreciated.

Regards,
ron

Wan Bachtiar

unread,
May 29, 2016, 10:12:05 PM5/29/16
to mongodb-user, mgo-...@googlegroups.com, golan...@googlegroups.com

My query works fine in a terminal (or Robomongo) but not in mgo. Both localField and foreignField are ObjectId’s then am I correct in understanding mgo requires a string?

Hi Ron,

Both the localField and foreignField are field names (string) from documents, not an ObjectId(). See also $lookup for more description.
You can also utilise the sample mgo $lookup listed previously in this thread as a start.

If you still have any questions, could you provide the following :

  • The example aggregation query. i.e. that works via mongo shell
  • The mgo aggregation snippet code that generates the error.
  • The mgo version. i.e. mgo.v2

Regards,

Wan.

Reply all
Reply to author
Forward
0 new messages