本文整理匯總了Golang中labix/org/v2/mgo.Collection.Pipe方法的典型用法代碼示例。如果您正苦於以下問題:Golang Collection.Pipe方法的具體用法?Golang Collection.Pipe怎麽用?Golang Collection.Pipe使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類labix/org/v2/mgo.Collection
的用法示例。
在下文中一共展示了Collection.Pipe方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: GetAllRelevantSessions
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
}
示例2: main
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))
}