数据库结结构_openid:可能多个
[{_id: "1533796e54ea401fa55be4060f668991" , _openid: "oaVUp46Ztu4INHIdx-lTIIio555I" , addTime: "2019-11-01 21:24:09" , validTime: 1573046649027}, {_id: "92a464912ccf4852bbcf90c060885729" , _openid: "oaVUp46Ztu4INHIdx-lTIIio555I" , addTime: "2019-11-01 21:24:20" , validTime: 1573046660592}, {_id: "ccfc8b521b9c4fd59036995dd7139437" , _openid: "oaVUp46Ztu4INHIdx-lTIIio555I" , addTime: "2019-11-01 21:26:18" , validTime: 1573046778242}, {_id: "afd969472b5447239a92a9444897ae78" , _openid: "oaVUp46Ztu4INHIdx-lTIIio555I" , addTime: "2019-11-01 21:27:36" , validTime: 1573046856953}, {_id: "86d702aa2d0a4216836982b9a1d0ce4b" , _openid: "oaVUp46Ztu4INHIdx-lTIIio555I" , addTime: "2019-11-01 21:28:53" , validTime: 1573046933944}, {_id: "160b8653e7ad43e69bd94ea75a99fd45" , _openid: "oaVUp4zrpkGhKsCuF6Sx8AGBJXSg" , addTime: "2019-11-01 21:39:48" , validTime: 1573047588321}, {_id: "01f88ec3eb0c46d598fe47954207a570" , _openid: "oaVUp4zrpkGhKsCuF6Sx8AGBJXSg" , addTime: "2019-11-01 21:39:49" , validTime: 1573047589202}, {_id: "9ef11a0193364cbca5f44c4852aebb50" , _openid: "oaVUp4zrpkGhKsCuF6Sx8AGBJXSg" , addTime: "2019-11-01 21:39:49" , validTime: 1573047589454}, {_id: "3f62884ee0f844e6b3e1c0d2ff6227a4" , _openid: "oaVUp4zrpkGhKsCuF6Sx8AGBJXSg" , addTime: "2019-11-01 21:39:49" , validTime: 1573047589649}, {_id: "83359af659ea4683b8ed73e4ad58c5b1" , _openid: "oaVUp4zrpkGhKsCuF6Sx8AGBJXSg" , addTime: "2019-11-01 21:39:49" , validTime: 1573047589823}, {_id: "c1ea5e825477445facf60fdab37a1b2d" , _openid: "oaVUp4zrpkGhKsCuF6Sx8AGBJXSg" , addTime: "2019-11-01 21:39:50" , validTime: 1573047590047}, {_id: "5b0bc911d72a4aa291b102cdfa1eeeb5" , _openid: "oaVUp47N8U125EABrHTCXWr8SJDY" , addTime: "2019-11-01 21:39:50" , validTime: 1573047590223}, {_id: "158d8640e78c43f2ba647de2eafcecaa" , _openid: "oaVUp47N8U125EABrHTCXWr8SJDY" , addTime: "2019-11-01 21:39:50" , validTime: 1573047590907}, {_id: "ba86af1baa7142449cf9e3a955c6b90b" , _openid: "oaVUp47N8U125EABrHTCXWr8SJDY" , addTime: "2019-11-01 21:39:59" , validTime: 1573047599150}, {_id: "1141f0644fb746f7af1fe58798698991" , _openid: "oaVUp47N8U125EABrHTCXWr8SJDY" , addTime: "2019-11-01 21:40:34" , validTime: 1573047634853}, {_id: "5b1c8adcc274461fa04062a0e6b25b11" , _openid: "oaVUp47N8U125EABrHTCXWr8SJDY" , addTime: "2019-11-01 21:40:35" , validTime: 1573047635463}, {_id: "d89467992afb4dc4b8a8125c7a70abc2" , _openid: "oaVUp47N8U125EABrHTCXWr8SJDY" , addTime: "2019-11-01 21:40:50" , validTime: 1573047650073}, {_id: "a9dca14680b2401b855881f3543d9d8f" , _openid: "oaVUp47N8U125EABrHTCXWr8SJDY" , addTime: "2019-11-01 21:40:55" , validTime: 1573047655545}] |
要求: 1.取一组数据 里面的_openid不能重复 2.并且validTime是相同_openid里面最大的 云函数搜索条件该如何编写??谢谢
https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-client-api/database/aggregation/operators/first.html
参考这个,应该可以满足你的需求
类似如下写法:
const $ = db.command.aggregate
db
.collection(
'collection_name'
)
.aggregate()
.sort({
_openid: 1, validTime: -1
})
.group({
_id:
"$_openid"
,
__id: $.first(
'$_id'
),
validTime: $.first(
'$validTime'
),
addTime: $.first(
'$addTime'
)
})
.end()
注意加索引
你这个集合设计得有问题啊
插入前不能判断_openid是否存在吗
存在就更新,不存在就新增,不就没有现在这麻烦事了??
db.collection('xxx').aggregate()
.group({
_openid:'$_openid',
validTime: $.max('$validTime')
})
.end()
db.collection('xxx').aggregate()
.match({
_openid:'xxxxxxxxx'
})
.group({
_openid:'$_openid',
validTime: $.max('$validTime')
})
.end()
拜托大神们!!!
首先根据vaildTime从大到小排序,然后用Set来去重