2017-03-23 75 views
0

我有这样的:Mongoosastic参考不保存

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'} 
    ], 
}); 

回答

0

所以要填充调试我已经找到了mongoosastic.js的一天,它需要后,并作为填入路径I不应该把收集名称,而是像这样的字段名称:

ProjectSchema.plugin(mongoosastic, { 
    esClient: esClient, 
    populate: [ 
     {path: 'author', select: 'first_name last_name email'} 
    ], 
});