These are chat archives for Automattic/mongoose

29th
Dec 2016
kailash k yogeshwar
@kailashyogeshwar85
Dec 29 2016 04:52

I have a model Posts with users fields which is reference to User model

when i query the Posts and use populate('users some_other_fields')



  Post
    .findOne({_id: "12344"})
    .populate('users comments')
    .then(posts => {
      // i get password field too in users object how can i hide the password field in returned object
      // {
      //    title: "title",
      //     users: [{ 
      //      name:"name"
      //      password: "password"
      //      }],
      //     comments: []
      //   }
    })

how can i do it using select i have used

Model.find({}).populate('users').select({users.password : 0}).then(data = > {
  // result with password field too
 });
kailash k yogeshwar
@kailashyogeshwar85
Dec 29 2016 06:21
got the solution
Post.find({}).populate({path: 'users',model: 'User',select: '-password'}).then( post => { })