如何使用expressjs在mongoose中选择两列值作为键值对

时间:2017-09-08 05:01:06

标签: node.js express mongoose

我的架构如下所示 Resource.js

var mongoose = require("mongoose"),
Schema = mongoose.Schema,
objectId = mongoose.Schema.ObjectId;
var lableShema = new Schema({
labelName: { type: String },
language: { type: String, },
resourceKey: { type: String, },
resourceValue: { type: String, }
}, {
    versionKey: false
});

var lableShema = mongoose.model('LabelKeyResource', lableShema);
module.exports = lableShema;
db中的

我有这样的数据,

{
    "_id": "59b1270b4bb15e1358e47cbd",
    "labelName": "submit",
    "__v": 0,
    "resourceKey": "submit_btn",
    "resourceValue": "Submit",
    "language": "engilish"
}

我使用选择功能是

 lableResource.find({ language: req.params.ln}, function (err, data) {
    if (err) {
        res.send(err);
        return;
    }
 res.send(data);

但我希望这种格式如何...

{"submit_btn":"Submit","select_lbl":"Please Select"}

1 个答案:

答案 0 :(得分:0)

您可以在从Mongo获取数据后格式化数据。 这就是你如何做到的:

var obj = {
   [data.resourceKey]: data.resourceValue,
   select_label: "Please Select"
};

这将为您提供对象:{"submit_btn":"Submit","select_lbl":"Please Select"}