buunguyen / mongoose-deep-populate

Mongoose plugin to enable deep population of nested models ⛺
MIT License
469 stars 44 forks source link

Getting specific fields in deepPopulate #52

Closed mariohmol closed 7 years ago

mariohmol commented 7 years ago

Hi there!

I have kind of cascading collections and i would like to take just one atribute at the end of the chain. Instead it is taking all the fields for the hole chain.

For example, i have:

Chat -> Order -> Sellers -> Users -> email

Chat.findById(id)
.deepPopulate("order.sellers.users.email")

This brings me all the data for the order, seller and users, and not just the email for the user.

Is it possible to achieve that with deep-populate?

Tested whitelist as well but didnt worked, ex:

Chat.findById(id)
.deepPopulate("order.sellers.users", {
whitelist: ["users.email", "order.sellers.users.email", "email"]
})

Thanks!

mariohmol commented 7 years ago

Forget about it.. found that mongoose itself can do it, exemple

    .populate({ 
      path: 'order',
      populate: {
        path: 'buyers',
        select: {'users': 1},
        model: 'Buyer',
        populate: {
          path: 'users',
          select: {'email': 1},
          model: 'User'
        }
      } 
    })

bests!