[英]how do I translate my query to use with mongodb in Node.js
this is how my mongo query looks like 这是我的mongo查询的样子
db.test.findOne({"User.David":{$elemMatch:{"action":"todo","status":"Done"}}})
I'm implementing this in a node.js api where the user can retrieve docs based on username and status 我正在node.js api中实现此功能,用户可以在其中基于用户名和状态检索文档
below is what I've tried. 下面是我尝试过的。
var query = {};
var value = `User.${userName}`;
query[value] = `{$elemMatch:{"action":"todo","status":"${status}"}}`
db.collection(test).findOne(query).then((result)=>{
}
problem with this is the query looks like 问题是查询看起来像
db.test.findOne({'User.David':'{$elemMatch:{"action":"todo","status":"Done"}}'})
the quotes are a problem ' ' here. 引号在这里是个问题。
You shouldn't use a string template around the whole query value expression since that will result in a string. 您不应该在整个查询值表达式周围使用字符串模板,因为那样会产生字符串。 Instead, just construct your query like so:
相反,只需像这样构造查询:
query[value] = { $elemMatch: { action: "todo", status } }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.