在Golang中使用MongoDB,获取最近15分钟内插入的记录。

huangapple go评论91阅读模式
英文:

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)),
        },
    },
},

huangapple
  • 本文由 发表于 2021年8月31日 16:16:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/68995163.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定