Return certain fields with .populate() from Mongoose

前端 未结 10 1574
栀梦
栀梦 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条回答
  •  面向向阳花
    2021-01-30 10:27

    In the following query i retrieved articles which match the condition show=true the retrieved data title and createdAt also retrieve the category of article only the title of category and it's id.

    let articles = await articleModel
            .find({ show: true }, { title: 1, createdAt: 1 })
            .populate("category", { title: 1, _id: 1 });
    

提交回复
热议问题