我有一个如下所述的用户模型和发布模型(已导入必需的库)
用户架构
const UserSchema = new mongoose.Schema({
name: String,
email: String,
post: [ mongoose.Types.ObjectId, ref: 'posts' ]
});
发布架构
const UserSchema = new mongoose.Schema({
title: String,
content: String,
postedOn: Date
});
我想获取有关用户ID的帖子。但是我不希望整个Post文档都得到回报。我只想要属性“ Title”和“ Date”,
我尝试了以下命令:
const posts = await User.findById(user_id).populate('post');
但是它将返回整个集合。谁能告诉我如何从用户模型中仅获取帖子(子文档)的“标题”和“日期”属性?
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句