简体   繁体   中英

Mongoose Populate method return object in object

I'm using mongoose populate -

let allLeaders = await DAOManager.populate(
        models.Users,
        { firstName: "Mayank" },
        {firstName: 1, lastName: 1, district: 1},
        { lean : true},
        { path: "userId", select: "county designation gender" }
    );

In user model -

let Users = new schema({
userId: { type: schema.Types.ObjectId, ref: 'hubiloUsers' }})

Output :

 {
    "_id": "5a05799b936a8c707f0e022f",
    "userId": {
        "_id": "5a057915c31eb5601da9b75f",
        "designation": "CTO ",
        "gender": "male",
        "county": "India"
    },
    "firstName": "Mayank", 
    "lastName": "Sharma", 
    "district": "ABC"
    }

Problem --- In output I got userid object. Is there anyway I can get the userId output data outside. so I can sent the data to the views or frontend. Thank you very much in advance

您可以为聚合使用$ project。

I've asked the developer of Mongoose about this before, and he says it's by design. I do agree with you that it sometimes makes more sense as a flat structure but what can you do!

You can read the issue here: https://github.com/Automattic/mongoose/issues/7281

You will have to change it to a flat structure yourself after the operation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM