简体   繁体   English

重命名填充返回值 Mongoose

[英]Rename populate returned values Mongoose

I am trying to return returned properties, much like in Mysql 's AS .我正在尝试返回返回的属性,就像在MysqlAS But with renamed object properties.但具有重命名的对象属性。

Query询问

Games.find({leagueID:leagueID, result:{$ne: null}}).populate('home_id away_id').sort({date: -1}).execAsync()

Output输出

{
    home_id: {
        ...some details
    },
    away_id: {
        ...some details
    }
}

Desired Output期望输出

{
    home: {
        ...some details
    },
    away: {
        ...some details
    }
}

So how can I get the desired outcome?那么我怎样才能得到想要的结果呢?

您可以使用聚合并像这样操作输出字段

db.collection.aggregate([{ $project:{_id:0, home:"$home_id", away:"$away_id"} }])

My solution is to use the transform function.我的解决方案是使用转换功能。

GamesSchema.set('toJSON', {
    transform: function(doc, ret, options) {   
        if (mongoose.Types.ObjectId.isValid(ret.home)) {
            ret.homeId = ret.home;
            delete ret.home;
        }
        if (mongoose.Types.ObjectId.isValid(ret.away)) {
            ret.awayId = ret.away;
            delete ret.away;
        }
    }
});

Without populate:没有人口:

Input输入

{
    "_id": "sD95OhsGrWVIqmTLVeuQdkna",
    "leagueID": 1000,
    "home": "404d1d9f68c3bb386b50f440" // ObjectId
    "away": "504d1d9f68c3bb386b50f450" // ObjectId
}

Output输出

{
    "_id": "sD95OhsGrWVIqmTLVeuQdkna",
    "leagueID": 1000,
    "homeId": "404d1d9f68c3bb386b50f440"
    "awayId": "504d1d9f68c3bb386b50f450"
}

With populate:随着人口:

Input输入

{
    "_id": "sD95OhsGrWVIqmTLVeuQdkna",
    "leagueID": 1000,
    "home": "404d1d9f68c3bb386b50f440" // ObjectId
    "away": "504d1d9f68c3bb386b50f450" // ObjectId
}

Output输出

{
    "_id": "sD95OhsGrWVIqmTLVeuQdkna",
    "leagueID": 1000,
    "home": {
        "_id": "404d1d9f68c3bb386b50f440",
        "name": "Home"
    }
    "away": {
        "_id": "504d1d9f68c3bb386b50f450",
        "name": "Away"
    }
}

Try lodash's _.mapKeys, like this:试试 lodash 的 _.mapKeys,像这样:

const newObject = _.mapKeys(oldObject.toJSON(), (value, key) => {
            if (key === 'oldKey') return 'newKey';
            return key;
          });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Mongoose 查找,然后填充,然后重命名子文档 - Mongoose find, then populate, then rename subdocument Mongoose 填充值并修改对象 - Mongoose Populate values and modifying the Object 用于填充列表值的 Mongoose 查询 - Mongoose query to populate list values 用控制器返回的值填充图表 - Populate the chart with values returned by controller 如果可能,我如何以其他方式重命名猫鼬中的返回字段? - how do i,if possible, other ways, rename a returned field in mongoose? 无法用猫鼬返回的数据填充数组 - Can't populate an array with data returned from mongoose 无论如何使用 mongoose 填充不同的值 - Is there anyway to populate distinct values using mongoose 返回的 object 具有符合 ZCCADCDEDB567ABAE643E15DCF0974E503Z 条件的嵌套值 - Returned object with nested values that comply condition with Mongoose 在 mongoose 中使用.populate() 时如何获取和添加具有附加属性的 ObjectIds 到数组(由.populate() 返回),其文档在 - While using .populate() in mongoose how to fetch and add, ObjectIds with additional properties to array(returned by .populate()) whose document in 如何重命名 Google 电子表格(文件)并使用工作表中的值填充其内容 - How to rename Google spreadsheets (files) and populate their contents with values from a sheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM