如何在猫鼬中获取子文档种群的特定字段

问题描述 投票:1回答:1

我具有如下所述的用户模型和发布模型(已导入必需的库)

用户架构

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');

但是它返回整个集合。谁能告诉我如何从用户模型中仅获取帖子(子文档)的“标题”和“日期”属性?

mongodb mongoose mongoose-schema mongoose-populate
1个回答
0
投票

猫鼬族群通过常规的field selection允许特定的field name syntax

© www.soinside.com 2019 - 2024. All rights reserved.