英文:
MongoDB on Golang, get records that are inserted in the last 15 minutes
问题
我正在尝试获取最近15分钟内插入的记录。
尽管它给我返回了最近15分钟内插入的记录,但是在15分钟过去后,它仍然显示了在15分钟后插入的记录。我的代码如下:
if len(userIds) > 0 {
dashboardAggregations = append(dashboardAggregations, bson.M{"$match": bson.M{"_id": bson.M{"$in": userIds}}})
}
cursor, err = locationCollection.Aggregate(dbContext, dashboardAggregations)
if err != nil {
return nil, err
}
if len(dashboardAggregations) > 3 {
dashboardAggregations = dashboardAggregations[:len(dashboardAggregations)-1]
}
以下是我的聚合结构体:
var dashboardAggregations = bson.A{
bson.M{
"$match": bson.M{
"created_at": bson.M{
"$gte": primitive.NewDateTimeFromTime(time.Now().Add(-15 * time.Minute)),
},
},
},
bson.M{
"$sort": bson.M{
"serial": -1,
},
},
bson.M{
"$group": bson.M{
"_id": "$userId",
"users": bson.M{"$push": "$$ROOT"},
},
},
}
提前感谢您的任何帮助。
英文:
I'm trying to get records that are inserted in the last 15 minutes.
Although it gives me records that are inserted in the last 15 minutes, after 15 minutes passes, it still shows me records that are inserted after 15 minutes. My code:
if len(userIds) > 0 {
dashboardAggregations = append(dashboardAggregations, bson.M{"$match": bson.M{"_id": bson.M{"$in": userIds}}})
}
cursor, err = locationCollection.Aggregate(dbContext, dashboardAggregations)
if err != nil {
return nil, err
}
if len(dashboardAggregations) > 3 {
dashboardAggregations = dashboardAggregations[:len(dashboardAggregations)-1]
}
Following is my aggregation struct:
var dashboardAggregations = bson.A{
bson.M{
"$match": bson.M{
"created_at": bson.M{
"$gte": primitive.NewDateTimeFromTime(time.Now().Add(-15 * time.Minute)),
},
},
},
bson.M{
"$sort": bson.M{
"serial": -1,
},
},
bson.M{
"$group": bson.M{
"_id": "$userId",
"users": bson.M{"$push": "$$ROOT"},
},
},
}
I appreciate any help in advance.
答案1
得分: 1
创建一个新的匹配聚合,检查用户:
bson.M{
"$match": bson.M{
"created_at": bson.M{
"$gte": primitive.NewDateTimeFromTime(time.Now().Add(-15 * time.Minute)),
},
"user_id": "123123133123",
},
}
如果用户参数不存在,则使用以下聚合:
bson.M{
"$match": bson.M{
"created_at": bson.M{
"$gte": primitive.NewDateTimeFromTime(time.Now().Add(-15 * time.Minute)),
},
},
}
英文:
create new match aggregation of checking user
bson.M{
"$match": bson.M{
"created_at": bson.M{
"$gte": primitive.NewDateTimeFromTime(time.Now().Add(-15 * time.Minute)),
},
"user_id": "123123133123",
},
},
and this aggregation if user parameter don't exist
bson.M{
"$match": bson.M{
"created_at": bson.M{
"$gte": primitive.NewDateTimeFromTime(time.Now().Add(-15 * time.Minute)),
},
},
},
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论