【发布时间】:2019-11-13 19:31:45
【问题描述】:
所以我有一个用于保存文章的集合,这个集合中的一个键是userId,我在其中保存用户集合中的_id。
const articleSchema: Schema = new Schema(
{
body: { type: String },
title: { type: String, required: true },
subTitle: { type: String, required: true },
userId: { type: Number, required: true, index: true },
},
{
timestamps: true,
}
);
当我得到其中一篇文章时,我还想获取存储在用户集合中的用户名和其他信息。
const userSchema: Schema = new Schema(
{
email: { type: String, required: true, unique: true, index: true },
country: { type: String },
fullName: { type: String, required: true },
password: { type: String, required: true },
},
{
timestamps: true,
}
);
我必须对数据库进行 2 次调用还是有某种方法可以只调用一次?类似于 SQL 中的联接
const account = await articleSchema // <-----can I also select the userSchema?
.findOne(
{ userId }
)
.exec();
如果可能的话,我想在一次调用中获取文章和撰写文章的用户详细信息
【问题讨论】:
标签: mongodb