我正在尝试使用 Golang 编写一个查询,我将在其中过滤今天为特定 profileId 创建的操作并给我计数。问题是我不知道如何编写一个过滤器来过滤掉特定时间范围内的项目。而且我似乎找不到使用 Golang 和 MongoDB 的合适解决方案。
Action 结构中的时间戳字段是创建日期,必须用于过滤掉当天创建的操作。
如果有人能帮助我并让我走上正轨,我将不胜感激。谢谢你。
这是我的代码:
type Action struct {
ID primitive.ObjectID `json:"_id,omitempty" bson:"_id,omitempty"`
ProfileID primitive.ObjectID `json:"profileID,omitempty" bson:"profileID,omitempty"`
Timestamp time.Time `json:"timestamp,omitempty" bson:"timestamp,omitempty"`
TypeID int `json:"type" bson:"type"`
}
func MatchActionAmount(profileId primitive.ObjectID) (int64, error) {
filter := bson.M{"profileID": profileId}
count, err := getActionCountByFilter(filter)
if err != nil {
log.Error("Could not get action count, error: ", err)
return 0, err
}
return count, nil
}
func getActionCountByFilter(filter primitive.M) (int64, error) {
ctx, _ := db.GetTimeoutContext()
return getActionCollection().CountDocuments(ctx, filter)
}
func getActionCollection() *mongo.Collection {
return db.GetMongoCollection("action")
}
潇湘沐