[英]Aggregation and filter in Golang in MongoDB
i use https://github.com/Kamva/mgm for my Golang project with MongoDB as database.我将https://github.com/Kamva/mgm用于我的 Golang 项目,并将 MongoDB 作为数据库。 I´ve been searching a way to aggregate and filter a collection.我一直在寻找一种聚合和过滤集合的方法。
Here my working Aggregation of the Author (a user) of a article:这是我对文章作者(用户)的工作聚合:
authorCollName := mgm.Coll(&Models.User{}).Name()
articles := []Models.Article{}
err = mgm.Coll(&Models.Article{}).SimpleAggregate(&articles, builder.Lookup(authorCollName, "authorid", "_id", "author"))
But i want filter it on a specific category like here in this simple find:但我想在这个简单的查找中过滤到一个特定的类别,比如这里:
err = collection.SimpleFind(&articles, bson.D{{"category", objID}})
The Models:模型:
type Article struct {
mgm.DefaultModel `bson:",inline"`
Name string `json:"name" bson:"name"`
AuthorId primitive.ObjectID `json:"authorId" bson:"authorid"`
Author []User `json:"author" form:"author"`
Category primitive.ObjectID `json:"category,omitempty"`
}
type User struct {
mgm.DefaultModel `bson:",inline"`
Forename string `json:"forename"`
Name string `json:"name"`
Email string `json:"email"`
Password string `json:"password"`
}
Can someone give me a hint, how i can solve it that i can use aggregation and a filter together?有人可以给我一个提示,我该如何解决我可以一起使用聚合和过滤器的问题?
Thank you prsad_!谢谢prsad_! You save my day!你拯救了我的一天! The solution:解决方案:
err = mgm.Coll(&Models.Article{}).SimpleAggregate(&articles, builder.Lookup(authorCollName, "authorid", "_id", "author"), M{operator.Match: M{"category": objID}})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.