2017-03-23 77 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'} 
    ], 
});