I am using mongojs in nodejs to access mongodb. I am trying to find records based on the "SOLUTION_TEST Number" as the where condition.

    db.stories.find({"SOLUTION_TEST Feature" : req.query.data},function(e,docs){
        console.log(docs);
        if(docs[0]){
            res.end(docs[0]);
        }
        else{
            res.end();
        }    
    });

According to mongojs, i have to pass string into the where clause. that where i pass req.query.data which is string. but i want ot pass an number because that feild type is a number. from the shell i am able to make the call successfully.

How can i pass the value as a number so that mongodb understands?

Entries in stories DB: enter image description here

有帮助吗?

解决方案

You just need to parse req.query.data into a number; you can use parseInt for that. But you also need to convert docs[0] to a string before passing it to res.end:

db.stories.find({"SOLUTION_TEST Feature" : parseInt(req.query.data, 10)},function(e,docs){
    console.log(docs);
    if(docs[0]){
        res.end(JSON.stringify(docs[0]));
    }
    else{
        res.end();
    }    
});
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top