深いレベルの移入に問題があります。多くのページを検索して読みましたが、解決できません。
ここに私のスキーマがあります:
var boxSchema = new mongoose.Schema({
name: {
type: String,
unique: true
}//, required: true
, height: {
type: Number
},//, required: true
width: {
type: Number
},
sensors: [
{
type: mongoose.Schema.ObjectId,
ref: 'Sensor',
}
]
});
var slotSchema = new mongoose.Schema({
slotPosition: {
type: Number,
unique: true
},
startDate: {
type: Date,
default: Date.now
},
harvestDate: {
type: Date,
default: null
},
Box_id: {
type: mongoose.Schema.ObjectId,
ref: 'Box'
},
TypePlant_id: {
type: mongoose.Schema.ObjectId,
ref: 'TypePlant'
}
});
var typePlantSchema = new mongoose.Schema({
name: {
type: String,
unique:true
},
expectedHarvestDuration: {
type: Number,
default: 0
},
expectedPh: {
type: Number
},
expectedTemp: {
type: Number
},
expectedLight: {
type: Number
},
image: {
type:String
}
});
var plantRecordSchema = new mongoose.Schema({
date: {
type: Date,
default: Date.now
},
rootLength: {
type: Number
},
plantLength: {
type: Number
},
Slot: {
type: mongoose.Schema.ObjectId,
ref: 'Slot'
}
});
私が必要とするターゲットは、すべての関連付けを含むスロット データを返します。このリンクからヒントを参照した後、それを行うためのコードを作成しました。
Slot.find({_id:mongoose.Types.ObjectId("5657e204b359fd6c103d0de5")})
.populate('TypePlant_id')
.populate('Box_id')
.exec(function(err,doc){
Slot.populate(doc,
{
path:'Box_id.sensors',
model:'Sensor'
},function(err,results){
console.log(err)
console.log(results);
})
});
結果は次のとおりです。
[ { startDate: Fri Nov 27 2015 11:54:28 GMT+0700 (SE Asia Standard Time),
harvestDate: null,
__v: 0,
slotPosition: 1,
Box_id:
{ sensors: [ [Object], [Object], [Object], [Object] ],
__v: 0,
width: 200,
height: 100,
name: 'testBox',
_id: 5657e204b359fd6c103d0de0 },
TypePlant_id:
{ expectedHarvestDuration: 20,
__v: 0,
image: '/images/peppersalad.png',
expectedTemp: 20,
expectedPh: 15,
name: 'dua leo',
_id: 5657e204b359fd6c103d0ddb },
_id: 5657e204b359fd6c103d0de5 } ]
ご覧のとおり、ボックスのセンサー (センサー: [ [オブジェクト]、[オブジェクト]、[オブジェクト]、[オブジェクト])」のコンテンツは入力されません。
とはいえ、ボックスから、私はまだセンサーを非常にうまく設定しています
Box.find({_id: mongoose.Types.ObjectId("5657e204b359fd6c103d0de0")})
.populate("sensors")
.exec(function(err,doc){
console.log(doc);
});
-----------------------------------------
Result
-----------------------------------------
[ { sensors:
[ { boxId: 5657e204b359fd6c103d0de0,
__v: 0,
description: 'A temperature sensor for Outbox',
name: 'OutTemperature',
_id: 5657e204b359fd6c103d0de2 },
{ boxId: 5657e204b359fd6c103d0de0,
__v: 0,
description: 'A temperature sensor for Inbox',
name: 'InTemperature ',
_id: 5657e204b359fd6c103d0de1 },
{ boxId: 5657e204b359fd6c103d0de0,
__v: 0,
description: 'sensor for measuring pH',
name: 'pHSensor',
_id: 5657e204b359fd6c103d0de3 },
{ boxId: 5657e204b359fd6c103d0de0,
__v: 0,
description: 'sensor for measuring Light consumption',
name: 'Light sensor',
_id: 5657e204b359fd6c103d0de4 } ],
__v: 0,
width: 200,
height: 100,
name: 'testBox',
_id: 5657e204b359fd6c103d0de0 } ]
プラグインmongoose-deep-populateも使用しますが、それでも同じ結果が得られます。スロットからセンサーの情報を取り込むのを手伝ってください。
どうもありがとうございます