func GetAllRelevantSessions(levelSessionsCollection *mgo.Collection) (topHumanSessions, topOgreSessions []lib.GameSession) { teams := [2]string{"humans", "ogres"} for _, teamName := range teams { queryParameters := bson.M{"level.original": "53558b5a9914f5a90d7ccddb", "submitted": true, "team": teamName} selection := bson.M{"team": 1, "totalScore": 1} sort := bson.M{"totalScore": -1} pipe := levelSessionsCollection.Pipe([]bson.M{{"$match": queryParameters}, {"$project": selection}, {"$sort": sort}, {"$limit": numberOfTopGamesToRank}}) var err error var documentCount int if teamName == "humans" { err = pipe.All(&topHumanSessions) documentCount = len(topHumanSessions) } else { err = pipe.All(&topOgreSessions) documentCount = len(topOgreSessions) } if err != nil { panic(err) } fmt.Println("Retrieved", documentCount, teamName, "sessions!") } return topHumanSessions, topOgreSessions }
func main() { var ( mongoSession *mgo.Session database *mgo.Database collection *mgo.Collection err error ) if mongoSession, err = mgo.Dial("localhost"); err != nil { panic(err) } database = mongoSession.DB("mgo_examples_05") collection = database.C("todos") var todos []Todo todos = append(todos, Todo{Id: bson.NewObjectId(), Task: "First task for today", Created: time.Now(), Due: time.Now().Add(time.Hour * 24)}) todos = append(todos, Todo{Id: bson.NewObjectId(), Task: "Second task for today", Created: time.Now(), Due: time.Now()}) todos = append(todos, Todo{Id: bson.NewObjectId(), Task: "Third task for today", Created: time.Now(), Due: time.Now()}) todos = append(todos, Todo{Id: bson.NewObjectId(), Task: "Fourth task for today", Created: time.Now(), Due: time.Now()}) todos = append(todos, Todo{Id: bson.NewObjectId(), Task: "Fifth task for today", Created: time.Now(), Due: time.Now()}) for _, todo := range todos { if _, err = collection.UpsertId(todo.Id, &todo); err != nil { panic(err) } } // START OMIT pipeline := []bson.M{ {"$group": bson.M{ "_id": bson.M{"$dayOfYear": "$d"}, "count": bson.M{"$sum": 1}, }}, } var ( result TodoDueCounts results []TodoDueCounts ) iter := collection.Pipe(pipeline).Iter() for { if iter.Next(&result) { results = append(results, result) } else { break } } err = iter.Err() if err != nil { panic(err) } // END OMIT fmt.Printf("%# v", pretty.Formatter(results)) }