我有两个分开的猫鼬模型。用户和个人资料。配置文件架构引用了用户架构。
UserSchema
const UserSchema = new mongoose.Schema(
{
name: {
type: String,
required: true,
},
email: {
type: String,
required: true,
unique: true,
},
password: {
type: String,
required: true,
},
},
{
toJSON: { virtuals: true },
toObject: { virtuals: true },
}
);
UserSchema.virtual("profile", {
ref: "profile",
localField: "_id",
foreignField: "user",
justOne: true,
});
那是配置文件架构
const ProfileSchema = new mongoose.Schema(
{
user: {
type: mongoose.Schema.Types.ObjectId,
ref: "user",
},
image: {
type: String,
},
company: {
type: String,
},
website: {
type: String,
},
start: {
type: Number,
required: true,
},
graduation: {
type: Number,
required: true,
},
})
我想要查询结果。没有个人资料的用户。我可以用javascript完成。但是我想要它作为mongo查询。我该如何实现?
谢谢