查询mongo cmd中的工作,但不在调用方法中查询

时间:2015-02-01 06:48:09

标签: node.js mongodb mongoose

我在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 }});

它总是返回null。

架构集合:



    {
        "_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"
                }
            }
        ]
    }

1 个答案:

答案 0 :(得分:1)

您应该将请求对象中diagnosis数组的所有元素转换为ObjectIds而不是字符串。然后在Mongo上执行查询

这是一个将字符串数组转换为ObjectIds数组的代码片段

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 }});