Mongodb 学习笔记 (10)某一时间间距内,以某一ID去重查询记录

测试数据样例为:

{
	"_id" : ObjectId("5d3e79559ee7b47488348112"),
	"member_id" : 27,
	"check_time" : 1564374565,
	"capture_image" : "/2019-7-29/2019-7-29-12h29m25s545085.jpg"
}

需求为:在每5秒的时间间隔中,以member_id作为去重条件,筛查记录。也就是小李在5秒内,只显示他的一条记录。

下面是查询语句:

db.t_pass_record.aggregate([
    { "$group": {
        "_id": {
			"member_id":"$member_id",
            "time":{
				"$subtract": [
					"$check_time",
					{ "$mod": [ "$check_time", 5 ] } //5秒间隔
				]
			}
        },
        "check_time": { "$first": "$check_time" },
        "member_image_id": { "$first": "$member_image_id" },
        "capture_image": { "$first": "$capture_image" }
    }}
])

查询结果为:

{ "_id" : { "member_id" : 33, "time" : 1564543895 }, "check_time" : 1564543898, "member_image_id" : "5d40fc86bd78200001667538", "capture_image" : "group1/M00/00/55/wKgA8F1BC5qALL9sAAFeUgmK_2o631.jpg" }
{ "_id" : { "member_id" : 33, "time" : 1564543700 }, "check_time" : 1564543703, "member_image_id" : "5d40fc86bd78200001667538", "capture_image" : "group1/M00/00/55/wKgA8F1BCtmATwx8AAHfa9vhfTk663.jpg" }
{ "_id" : { "member_id" : 33, "time" : 1564543650 }, "check_time" : 1564543651, "member_image_id" : "5d40fc86bd78200001667538", "capture_image" : "group1/M00/00/55/wKgA8F1BCqWAHwSEAAC96W47m7g298.jpg" }
{ "_id" : { "member_id" : 33, "time" : 1564542510 }, "check_time" : 1564542511, "member_image_id" : "5d40fc86bd78200001667538", "capture_image" : "group1/M00/00/55/wKgA8F1BBi-AO20RAAFnjcwZWk8573.jpg" }
{ "_id" : { "member_id" : 35, "time" : 1564542250 }, "check_time" : 1564542251, "member_image_id" : "5d410496bd7820000166753b", "capture_image" : "group1/M00/00/55/wKgA8F1BBSuAIQEWAAC7r7K82sQ000.jpg" }

 

你可能感兴趣的:(Mongodb 学习笔记 (10)某一时间间距内,以某一ID去重查询记录)