数据库查询条件中字段名能由变量替代吗?
发布于 6 年前 作者 mingu 13515 次浏览 来自 问答

实际中,要动态地确定查询数据库表中的某个字段。例如示例文档中:

const _ = db.command

db.collection('todos').where(_.or([  {    progress: _.lte(50)  },  {    style: {      color: _.in(['white', 'yellow'])    }  } ]))  .get({    success(res) {      console.log(res.data)    }  })

能否 var field1=‘progress’;

然后表达式中 progress:_.lte(50),换成类似field1:_.lte(50),这种该如何实现啊?


2 回复

非常感谢!

[field1]:_.lte(50)

回到顶部