我使用https://github.com/Kamva/mgm作为我的Golang项目的数据库,MongoDB作为数据库。我一直在寻找一种聚合和过滤集合的方法。
在这里,我对一篇文章的作者(用户)进行了工作聚合:
authorCollName := mgm.Coll(&Models.User{}).Name()
articles := []Models.Article{}
err = mgm.Coll(&Models.Article{}).SimpleAggregate(&articles, builder.Lookup(authorCollName, "authorid", "_id", "author"))但是我想在一个特定的类别上过滤它,就像下面这个简单的发现:
err = collection.SimpleFind(&articles, bson.D{{"category", objID}})模型:
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"`
}有人能给我一个提示吗,我如何解决它,我可以使用聚合和过滤器在一起?
发布于 2022-07-04 13:32:35
解决办法:
err = mgm.Coll(&Models.Article{}).SimpleAggregate(&articles, builder.Lookup(authorCollName, "authorid", "_id", "author"), M{operator.Match: M{"category": objID}})https://stackoverflow.com/questions/72854269
复制相似问题