简体   繁体   中英

Mongoose: how to populate on nested array ( deep-level)

I have a trouble with populate deep level. I searched and read many pages but it can't resolve.

Here is my Schema:

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

The target that i need return slot data with all association. After see hint from this link, I created the code to do that:

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);
        })
});

And here is the result:

[ { 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 } ]

As you see, It doesn't populate content of sensors of box (sensors: [ [Object], [Object], [Object], [Object])

Althought, from box, i still populate sensor very well

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 } ]

I also use plugin mongoose-deep-populate , but it stills give the same result. Please help me populate information of sensors from Slot.

Thank a lot

Sorry, My code already populate data of sensors from slots. The mistake come from console.log, it can't print out the fully-result. I still totally access to data by dot ('.') to its properties.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM