收藏
回答

云函数搜索条件求助?

数据库结结构_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里面最大的 云函数搜索条件该如何编写??谢谢

回答关注问题邀请回答
收藏

4 个回答

  • ulysses
    ulysses
    2019-11-04

    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()


    注意加索引

    2019-11-04
    有用 1
    回复 1
    • .
      .
      2019-11-04
      已解决 谢谢
      2019-11-04
      回复
  • o0o有脾气的酸奶
    o0o有脾气的酸奶
    2019-11-04

    你这个集合设计得有问题啊

    插入前不能判断_openid是否存在吗

    存在就更新,不存在就新增,不就没有现在这麻烦事了??


    2019-11-04
    有用 1
    回复 6
    • .
      .
      2019-11-04
      这是收集formid的表 当时没有考虑这么多  就是拿到所有数据进行本地处理 后来数据太多了 对性能都有影响了
      2019-11-04
      回复
    • .
      .
      2019-11-04
      现在想能不能通过搜索条件查找优化一下!
      2019-11-04
      回复
    • o0o有脾气的酸奶
      o0o有脾气的酸奶
      2019-11-04回复.
      你的集合硬是那样设计,就用下面的查询吧
      db.collection('xxx').aggregate()
      .group({
          _openid:'$_openid',
          validTime: $.max('$validTime')
      })
      .end()
      2019-11-04
      1
      回复
    • o0o有脾气的酸奶
      o0o有脾气的酸奶
      2019-11-04回复.
      查询某个_openid的
      db.collection('xxx').aggregate()
      .match({
          _openid:'xxxxxxxxx'
      })
      .group({
          _openid:'$_openid',
          validTime: $.max('$validTime')
      })
      .end()
      2019-11-04
      1
      回复
    • o0o有脾气的酸奶
      o0o有脾气的酸奶
      2019-11-04回复.
      试着给_openid和validTime加索引吧
      2019-11-04
      1
      回复
    查看更多(1)
  • .
    .
    2019-11-04

    拜托大神们!!!

    2019-11-04
    有用 1
    回复
  • 山里育
    山里育
    2019-11-04

    首先根据vaildTime从大到小排序,然后用Set来去重

    2019-11-04
    有用
    回复 1
    • .
      .
      2019-11-04
      已解决 谢谢
      2019-11-04
      回复
登录 后发表内容
问题标签