I'm using MongoDB with Golang.
I have the following struct which my query result will write to:
var slacks []struct {
Name string `json:"name"`
Description string `json:"description"`
PostCount int `json:"postCount"`
} `json:"slacks"`
}
I have documents in my collection which look like:
type slack struct {
ID bson.ObjectId `bson:"_id"`
Name string `bson:"name"`
Description string `bson:"description"`
Posts []post `bson:"posts"`
}
My aim is to count the posts in each slack using a MongoDB query. I've got this so far:
db.C("slacks").Find(&bson.M{"name": &bson.RegEx{
Pattern: ".*" + searchStr + ".*",
Options: "i",
}}).Select(&bson.M{
"name": 1,
"description": 1,
"$size": "posts",
}).All(&slacks)
But {"$size": "posts"}
doesn't do the trick.
How can I query the length of an array/slice of struct in a MongoDB query in Golang?