如何从mongodb聚合函数返回字符串值而不是Json

时间:2017-10-08 17:23:57

标签: javascript node.js mongodb

我创建了一个函数来返回“minHospitalization'来自以下函数的db.Response字段是' [{" minHospitalization":1}]'

我想将值返回为' 1'而不是以json格式返回值。我如何实现它?

var getHospitalStayDuration = function (req, res, callback) {

    var treatmentName = req.query.procedurename

    treatmentDescModel.aggregate([
        { "$match": { "isDisable": "N", "treatmentName": treatmentName } },
        { "$project": { "_id": 0, "minHospitalization": 1} }

    ], function (err, result) {

        if (err) {
           //do smthing
        } else if (!result.length) {
            //do something
        }
        else {
             //result= [{"minHospitalization":1}]   

          //callback(JSON.parse(JSON.stringify(result)).minHospitalization);
            callback(JSON.parse(JSON.stringify(result)))
            //How to return 1 instead of result??

            }
    })

}

1 个答案:

答案 0 :(得分:0)

您可以使用key

执行此操作
callback(result[0].minHospitalization)