Return certain fields with .populate() from Mongoose

前端 未结 10 1540
栀梦
栀梦 2021-01-30 10:11

I\'m getting returned a JSON value from MongoDB after I run my query. The problem is I do not want to return all the JSON associated with my return, I tried searching the docs a

10条回答
  •  -上瘾入骨i
    2021-01-30 10:27

    If you only want a few specific fields to be returned for the populated documents, you can accomplish this by passing the field name syntax as the second argument to the populate method.

    Model
    .findOne({ _id: 'bogus' })
    .populate('the_field_to_populate', 'name') // only return the Persons name
    ...
    

    See Mongoose populate field selection

提交回复
热议问题