Sometimes we don't want to display the full object stored in mongo to our user. One way of handling this is to query all the relevant documents, iterate on all of them programmatically and filter out all the unwanted attributed.
Coming from a SQL world, I prefer to let the database engine do the work for me. It turns out it's quite easy to do using mongoose. Indeed, the find()
method of a model accepts a second string parameter. This string must contain the name of all the attributes you want to keep. Note that the separator is a space.
This would be the syntax:
const xyzArticles = await Article.find({userUuid:"XYZ"}, "title content authorBio");
Comments
Post a Comment