V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
just1
V2EX  ›  问与答

请教一个小程序云开发数据库聚合函数的问题

  •  
  •   just1 · 2019-12-28 13:49:45 +08:00 · 1236 次点击
    这是一个创建于 1593 天前的主题,其中的信息可能已经有所发展或是发生改变。

    想要从数据库中随机出来一条符合条件的记录,其中有一个条件是记录的 type 属性存在于一个 array 数据

    {"name": "abc", "type": "a", "floor": 1}
    {"name": "abcd", "type": "b", "floor": 2}
    {"name": "abcde", "type": "c", "floor": 2}
    

    现在的代码

    let condition = [ 'a', 'b' ];
    const _ = db.command;
    db.collection('db').aggregate().match({
          floor: 1,
          type: _.or(condition.map(item => _.eq(item)))
        }).sample({
          size: 1
        }).then(res => {
          console.log(res)
        })
    

    查询出来的结果发现 type 并没有被约束,达不到预期目的,十分疑惑,除非是将约束条件更改为 type: 'a',这样才能被约束,type: _.eq('a')都不行。

    第 1 条附言  ·  2019-12-28 15:55:13 +08:00
    破案了,真机没问题,应该是模拟器的 bug
    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1364 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 23:32 · PVG 07:32 · LAX 16:32 · JFK 19:32
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.