我想通过传递变量来查找。我有这样的数据,
using Newtonsoft.Json;
RootObject obj = JsonConvert.DeserializeObject<RootObject>(jsonString);
Console.WriteLine(obj.actList.Count);
在请求中,我正在获取这样的数据,
{
"projectId": "12345-service-request",
"projectVersion": [{
"version":"1",
"localConfig": [{
"port": "3003",
"mongoURI": "mongodb://localhost:27017/serviceRequest",
"MQ": "RMQ",
"logLevel": "2",
"version": "1.1",
"created": "03-06-2018 03:11:00 PM",
"active": "N"
},
{
"port": "3004",
"mongoURI": "mongodb://localhost:27017/serviceRequest",
"MQ": "IMQ",
"logLevel": "1",
"version": "1.2",
"created": "07-06-2018 03:11:00 PM",
"active": "Y"
}]
}]
}
因此,我正在请求中更改{
"projectId" : "32804-service-request-service",
"env" : "local"
"cloudParam" : {
"MQ" : "RMQ",
"logLevel" : "2",
"active": "N"
}
}
的密钥。
cloudParam
现在,我想在“查找”查询中传递dbQry和projectId。 我像下面这样传递,但是什么也没返回,
let dbQry = renameKeys(cloudParam, function(key, val) {
return "projectVersion.localConfig."+ key;
});
console.log(dbQry);
// Output : { 'projectVersion.localConfig.MQ' : 'RMQ', 'projectVersion.localConfig.logLevel': '2' , 'projectVersion.localConfig.active': 'N' }
那么,如何在Mongoose的Find方法中传递变量?
编辑- 预期输出:
ProjectConfig.find({'projectId' : projectId,dbQry}).exec(function(err,pc){
console.log("## DB Result : "+pc);
});
注意: 如果我获得完整的文档作为输出,也可以,但是如何在find查询中传递参数。
答案 0 :(得分:0)
尝试如下
ProjectConfig.findOne({ 'projectId': projectId }).exec(function (err, pc) {
let version = "1.1"; //your version
let MQ = "RMQ";//your mq
let versionData = {};
mydate.projectVersion.forEach(item => {
let match = item.localConfig.find(ele=> (ele.version == version && ele.MQ == MQ ));
versionData = (match != null) ? match : {}
});
console.log(versionData);
});
控制台日志结果
{
"port": "3003",
"mongoURI": "mongodb://localhost:27017/serviceRequest",
"MQ": "RMQ",
"logLevel": "2",
"version": "1.1",
"created": "03-06-2018 03:11:00 PM",
"active": "N"
}
工作示例链接