Created
March 12, 2017 02:55
-
-
Save DavadDi/440f25db50a56a9852ebfba4cad4b757 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"time" | |
"fmt" | |
"gopkg.in/mgo.v2" | |
"gopkg.in/mgo.v2/bson" | |
) | |
type SuiteGroup struct { | |
ID *bson.ObjectId `bson:"_id,omitempty" json:"_id"` | |
Name *string `bson:"name,omitempty" json:"name,omitempty"` | |
SuiteID *bson.ObjectId `bson:"_suiteId" json:"_suiteId"` | |
Kind *string `bson:"kind,omitempty" json:"kind,omitempty"` | |
// pipeline get, Suites []Suite | |
Suite []Suite `bson:"suite" json:"suites"` | |
} | |
type Suite struct { | |
ID *bson.ObjectId `bson:"_id" json:"_id,omitempty"` | |
Created *time.Time `bson:"created,omitempty" json:"created,omitempty"` | |
} | |
// See also | |
// https://docs.mongodb.com/manual/aggregation/ | |
// https://docs.mongodb.com/manual/core/aggregation-pipeline/ | |
// https://github.com/go-mgo/mgo/issues/248 | |
func main() { | |
session, err := mgo.Dial("127.0.0.1") | |
if err != nil { | |
panic(err) | |
} | |
defer session.Close() | |
// Optional. Switch the session to a monotonic behavior. | |
session.SetMode(mgo.Monotonic, true) | |
c := session.DB("test").C("suitegroups") | |
pipeline := []bson.M{ | |
bson.M{"$match": bson.M{"name": "Admin"}}, | |
bson.M{"$lookup": bson.M{"from": "suites", "localField": "_suiteId", "foreignField": "_id", "as": "suite"}}, | |
} | |
var resp SuiteGroup | |
c.Pipe(pipeline).One(&resp) | |
fmt.Printf("SuiteID [%s] SuiteObj %#v\n", resp.SuiteID.String(), resp.Suite[0]) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment