2017-02-17 93 views
0

我已經閱讀了許多貓鼬深度填充文檔和文章,但我似乎無法得到它在這種情況下工作,但我有一種感覺,因爲當數據存儲時, dealSchema沒有ID。思考?貓鼬深入Mongoosastic

由於這裏的目標是在elasticsearch中使用mongoosastic和索引信息,所以我使用plugin傳入了填充參數。

//mongoose version 4.8.0 

var dealSchema = new Schema({ 
    deal: {type: Schema.ObjectId, ref: 'Deal'}, 
    discounts: [discountSchema] 
}, {_id:false}); 


var offerSchema = new Schema({ 
    name: String, 
    description: String, 
    partner: {type: Schema.ObjectId, ref: 'Partner'}, 
    venues: [{type: Schema.ObjectId, ref: 'Venue'}], 
    deals: [dealSchema], 
    modified: Date, 
    created: Date 
}); 


//=================================================// 
mongoose.connect('mongodb://127.0.0.1:27017/production'); 
offerSchema.plugin(mongoosastic, { 
    hosts: [ 'http://127.0.0.1:9200' ], 
    index: 'myIndex', 
    type: 'offer', 
    populate: [ 
     { path: 'partner', model: 'Partner', select: 'name' }, 
     { path: 'venues', model: 'Venue', select: 'name' }, 
     { path: 'deals.deal', model: 'Deal', select: 'name' } 
    ] 
}); 

///////// indexed data in ES /////////// 
// 1. partners and venues are populated as expected, name only. 
// 2. deals.deal is not populated as intended 

{ 
    "_index": "myIndex", 
    "_type": "offer", 
    "_id": "568454a104439f0300a57e86", 
    "_score": 2.1182644, 
    "_source": { 
    "name": "offer name", 
    "description": "This is an offer.", 
    "partner": { 
     "_id": "566ddbf61e11f0030020fcc5", 
     "name": "partner one" 
    }, 
    "venues": [ 
     { 
     "_id": "566df15d1e11f00300211c13", 
     "name": "venue one" 
     }, 
     { 
     "_id": "566df2f1b6e09103003595a6", 
     "name": "venue two" 
     } 
    ], 
    "deals": [ 
     { 
     "deal": "566df755b6e091030035cbed", 
     "discounts": [ 
      { 
      "amount": 0, 
      "attribute": "566ddeff1e11f0030020fccf" 
      }, 
      { 
      "amount": 0, 
      "attribute": "566ddef91e11f0030020fcce" 
      } 
     ] 
     } 
    ], 
    "modified": "2016-05-31T23:04:55.476Z", 
    "created": "2015-12-30T22:03:13.354Z" 
    } 
} 
+0

你會得到什麼,你期待什麼結果。這個問題似乎不完整 –

+0

道歉。我收到了人口稠密的地點和合作夥伴,名字只是如預期的那樣。然而,deal.deal仍然只是一個我會。沒有人口和沒有錯誤。我一回到家就會發布回覆。 – remotevision

+0

您確定Deal集合中存在交易嗎? –

回答

0

下面的代碼工作。我需要在重新運行之前刪除我的elasticsearch索引。

mongoose.connect('mongodb://127.0.0.1:27017/production'); 
    offerSchema.plugin(mongoosastic, { 
     hosts: [ 'http://127.0.0.1:9200' ], 
     index: 'myIndex', 
     type: 'offer', 
     populate: [ 
      { path: 'partner', model: 'Partner', select: 'name' }, 
      { path: 'venues', model: 'Venue', select: 'name' }, 
      { path: 'deals.deal', model: 'Deal', select: 'name' } 
     ] 
    }); 
0

(發佈代表OP)的

填充這種方式,實際上按預期填充deals.deal。因此,在聲明式填充時,這可能是mongoosastic問題。

Offer.findOne({'_id': '568454a104439f0300a57e86'}).populate('deals.deal').exec(function(err, doc){ 
    console.log(JSON.stringify(doc)); 
});