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