我有如下所述的用户模型和发布模型(已导入必需的库)
用户架构
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文档都得到回报。我只想要属性“标题”和“日期”
我尝试了以下命令:-
const posts = await User.findById(user_id).populate('post');
但是它返回整个集合。谁能告诉我如何从用户模型中仅获取帖子(子文档)的“标题”和“日期”属性?
答案 0 :(得分:0)
猫鼬人口通过通常的field selection
允许特定的field name syntaxconst posts = await User.findById(user_id).populate('post', 'Title Date');