查询条件合并

db.commond

920 发布: 2024/8/17 11:32 本文总阅读量
查询条件编写
const dbSearchFields = ['member_id.nickname', 'position_id.post_name', 'company_id.name']
let query = '小雨';

let regNew = dbSearchFields.map(name =>  {
    let dic = {[name]:new RegExp(query)}
    return dic;
})
// 模糊查询
regNew = db.command.or(regNew);
// 合并id
regNew = db.command.and(regNew,{'position_id._id':db.command.eq(this.job_id)})
数据实际执行的语句
{"$db":[{"$method":"command"},{"$method":"and","$param":[{"$db":[{"$method":"command"},{"$method":"or","$param":[[{"member_id.nickname":{"$regexp":{"source":"小雨","flags":""}}},{"position_id.post_name":{"$regexp":{"source":"小雨","flags":""}}},{"company_id.name":{"$regexp":{"source":"小雨","flags":""}}}]]}]},{"position_id._id":{"$db":[{"$method":"command"},{"$method":"eq","$param":["66bd73216e5d2d2a6798b9ec"]}]}}]}]}