最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Rename populate returned values Mongoose - Stack Overflow

programmeradmin2浏览0评论

I am trying to return returned properties, much like in Mysql's AS. 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 oute?

I am trying to return returned properties, much like in Mysql's AS. 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 oute?

Share Improve this question asked Sep 25, 2015 at 7:44 Jamie HutberJamie Hutber 28.1k54 gold badges194 silver badges313 bronze badges 1
  • How about just caliing them "home" and "away" to begin with since your only intent seems to be to populate that with the related object. Otherwise you basically need to convert the mongoose documents returned to standard objects, create new keys, copy data, then remove old keys. Seems that just naminng them what you want is the most logical thing to do for your end result. – Blakes Seven Commented Sep 25, 2015 at 7:48
Add a ment  | 

3 Answers 3

Reset to default 2

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"
    }
}

You can use aggregation and manipulate the output field like this

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

Try lodash's _.mapKeys, like this:

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

评论列表(0)

  1. 暂无评论