我有这个:
const ProjectSchema = new Schema({
projectId: String,
author: { type: Schema.Types.ObjectId, ref: 'User', es_indexed: true, es_schema: getUserModel().schema, es_select: 'first_name last_name email' },
participants: [String],
category: String,
title: String,
description: String,
});
和用户看起来像这样:
const UserSchema = new Schema({
userId: String,
first_name: String,
last_name: String,
email: String,
password: String,
});
在我的数据库中,我看到了这个:
{
"_id" : ObjectId("58d4456fd3d52632e010d377"),
"author" : ObjectId("58ce87e7f86e5d36f6ccfb81"),
"projectId" : "J0MXYM1S872Y3",
"category" : "gaming",
"title" : "The Project",
"description" : "This is the project.",
"participants" : [ ],
"__v" : 0
}
ObjectId("58ce87e7f86e5d36f6ccfb81")
是一个有效的 ID,我可以找到具有此 ID 的用户。
在 elastisearch 头上,我看到了这个:
"_source": {
"projectId": "J0MXYM1S872Y3",
"author": { },
"participants": [ ],
"category": "gaming",
"title": "The Project",
"description": "This is the project."
}
为什么作者是空的???
编辑
我在底部添加了这个:
ProjectSchema.plugin(mongoosastic, {
esClient: esClient,
populate: [
{path: 'users', select: 'first_name last_name email'}
],
});