Mongoose:如何填充嵌套数组(深层)

时间:2015-11-27 08:12:48

标签: javascript node.js mongodb mongoose mongoose-populate

我遇到填充深层次的问题。我搜索并阅读了很多页面,但它无法解决。

这是我的架构:

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 ,但它仍然会给出相同的结果。 请帮我填写Slot传感器的信息。

非常感谢

1 个答案:

答案 0 :(得分:0)

抱歉,我的代码已经从插槽中填充传感器数据。错误来自console.log,它无法打印出完整的结果。我仍然完全通过点('。')访问其属性的数据。