Proper way of using $or
operator in mgm
#54
Answered
by
mehran-prs
amirhossein-shakeri
asked this question in
Q&A
-
Imagine we have a model & a collection of docs. How do we use type Person struct {
Name string `json:"name" bson:"name"`
Age int `json:"age" bson:"age"`
Weight float64 `json:"weight" bson:"weight"`
} Forexample we need to get all docs which have an age of above 20 OR their weight is less than 70, then what is the best way to do that using mgm?🤔 |
Beta Was this translation helpful? Give feedback.
Answered by
mehran-prs
Nov 18, 2021
Replies: 1 comment 2 replies
-
Hi @amirhossein-shakeri-work type Person struct {
mgm.DefaultModel `bson:",inline"`
Name string `json:"name" bson:"name"`
Age int `json:"age" bson:"age"`
Weight float64 `json:"weight" bson:"weight"`
}
func main() {
persons := make([]*Person, 0)
err := mgm.Coll(&Person{}).SimpleFind(&persons, bson.M{"$or": bson.A{
bson.M{"age": bson.M{"$gt": 20}},
bson.M{"weight": bson.M{"$lt": 70}},
}})
} |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
amirhossein-shakeri
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @amirhossein-shakeri-work
It can be something like this: