V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
just1
V2EX  ›  问与答

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

  •  
  •   just1 · Dec 28, 2019 · 1494 views
    This topic created in 2319 days ago, the information mentioned may be changed or developed.

    想要从数据库中随机出来一条符合条件的记录,其中有一个条件是记录的 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')都不行。

    Supplement 1  ·  Dec 28, 2019
    破案了,真机没问题,应该是模拟器的 bug
    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2515 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 08:27 · PVG 16:27 · LAX 01:27 · JFK 04:27
    ♥ Do have faith in what you're doing.