查询在 mongo cmd 中有效,但在调用方法中无效
Query work in mongo cmd but not in calling method
我在 mongodb 工作。
我写了一个查询
db.ekgs.find({_id: ObjectId("54cd1dd3adc274b20a7f650d"),"interpretationList.interpretations" : { $all:[[ObjectId("54c09fb3581c4c8c218d1a39"),ObjectId("54c09fb3581c4c8c218d1a3a")]]}})
它在 mongo cmd 中工作。
但是当我用一种从用户那里获取输入的方法编写它时,它就不起作用了。
req.body 包含:
{
"ekgId":"54cdaed7adc274b20a7f650e",
"diagnosis":
[
"54c09fb3581c4c8c218d1a39",
"54c09fb3581c4c8c218d1a3a"
]
};
在我的方法中我写
modalSchema.find({"_id": req.body.ekgId,'interpretationList.interpretations' :{ $all :req.body.diagnosis , $size: arrLength }});
它总是 return 空。
架构集合:
{
"_id" : ObjectId("54cdaed7adc274b20a7f650e"),
"title" : "Demo Ekg",
"description" : "Demo Description",
"diagnosis" : "Demo Diagnosis",
"ekgImageExtension" : "jpg",
"urgency" : "Demo Urgency",
"therapy" : "Demo Therapy",
"differentialDiagnosis" : "Demo Ddx",
"history" : "Demo History",
"references" : "Demo References",
"fileName" : "",
"imageURL" : "images/Ekgs/54c09d68581c4c8c218d1a38/54c09d68581c4c8c218d1a38.jpg",
"interpretationList" : {
"interpretations" : [
ObjectId("54c09fb3581c4c8c218d1a39"),
ObjectId("54c09fb3581c4c8c218d1a3a")
]
},
"tags" : [
{
"_id" : ObjectId("54c255da581c4c8c218d2397"),
"tagName" : "Tag 1"
}
],
"segment" : [
{
"explanation" : "Demo Explanation 1",
"fileName" : "",
"imageURL" : "",
"level" : {
"_id" : ObjectId("54c25616581c4c8c218d2398"),
"levelName" : "Level 1"
}
},
{
"explanation" : "Demo Explanation 2",
"fileName" : "",
"imageURL" : "",
"level" : {
"_id" : ObjectId("54c25616581c4c8c218d2398"),
"levelName" : "Level 1"
}
}
]
}
您应该将请求对象中 diagnosis
数组的所有元素转换为 ObjectIds
而不是字符串。然后在 Mongo
上执行查询
这是将字符串数组转换为 ObjectId 数组的代码片段
var result = interpretationList.interpretations.map(function (x) {
return ObjectId(x);
});
然后在您的查询中使用 result
对象而不是 req.body.diagnosis
modalSchema.find({"_id": ObjectId(req.body.ekgId),'interpretationList.interpretations' :{ $all :result , $size: arrLength }});
我在 mongodb 工作。 我写了一个查询
db.ekgs.find({_id: ObjectId("54cd1dd3adc274b20a7f650d"),"interpretationList.interpretations" : { $all:[[ObjectId("54c09fb3581c4c8c218d1a39"),ObjectId("54c09fb3581c4c8c218d1a3a")]]}})
它在 mongo cmd 中工作。 但是当我用一种从用户那里获取输入的方法编写它时,它就不起作用了。
req.body 包含:
{
"ekgId":"54cdaed7adc274b20a7f650e",
"diagnosis":
[
"54c09fb3581c4c8c218d1a39",
"54c09fb3581c4c8c218d1a3a"
]
};
在我的方法中我写
modalSchema.find({"_id": req.body.ekgId,'interpretationList.interpretations' :{ $all :req.body.diagnosis , $size: arrLength }});
它总是 return 空。
架构集合:
{
"_id" : ObjectId("54cdaed7adc274b20a7f650e"),
"title" : "Demo Ekg",
"description" : "Demo Description",
"diagnosis" : "Demo Diagnosis",
"ekgImageExtension" : "jpg",
"urgency" : "Demo Urgency",
"therapy" : "Demo Therapy",
"differentialDiagnosis" : "Demo Ddx",
"history" : "Demo History",
"references" : "Demo References",
"fileName" : "",
"imageURL" : "images/Ekgs/54c09d68581c4c8c218d1a38/54c09d68581c4c8c218d1a38.jpg",
"interpretationList" : {
"interpretations" : [
ObjectId("54c09fb3581c4c8c218d1a39"),
ObjectId("54c09fb3581c4c8c218d1a3a")
]
},
"tags" : [
{
"_id" : ObjectId("54c255da581c4c8c218d2397"),
"tagName" : "Tag 1"
}
],
"segment" : [
{
"explanation" : "Demo Explanation 1",
"fileName" : "",
"imageURL" : "",
"level" : {
"_id" : ObjectId("54c25616581c4c8c218d2398"),
"levelName" : "Level 1"
}
},
{
"explanation" : "Demo Explanation 2",
"fileName" : "",
"imageURL" : "",
"level" : {
"_id" : ObjectId("54c25616581c4c8c218d2398"),
"levelName" : "Level 1"
}
}
]
}
您应该将请求对象中 diagnosis
数组的所有元素转换为 ObjectIds
而不是字符串。然后在 Mongo
这是将字符串数组转换为 ObjectId 数组的代码片段
var result = interpretationList.interpretations.map(function (x) {
return ObjectId(x);
});
然后在您的查询中使用 result
对象而不是 req.body.diagnosis
modalSchema.find({"_id": ObjectId(req.body.ekgId),'interpretationList.interpretations' :{ $all :result , $size: arrLength }});