【问题标题】:Mongoose: how to populate on nested array ( deep-level)Mongoose:如何填充嵌套数组(深层)
【发布时间】:2016-03-01 09:24:29
【问题描述】:

我在填充深层时遇到问题。我搜索并阅读了许多页面,但无法解决。

这是我的架构:

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

如您所见,它不会填充框传感器的内容(传感器:[[Object],[Object],[Object],[Object])

虽然,从盒子里,我仍然很好地填充传感器

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 中填充传感器信息。

非常感谢

【问题讨论】:

  • 为什么需要填充?即使您看不到sensor 中的全部数据,数据也会在那里。我真的不明白你只是试图在console中查看条目数据
  • 不,我正在编写一个用于访问的 api 数据库,我需要关联插槽中的所有数据并将其分配给回调。
  • 请详细说明您的用例
  • 哦,你为什么要关心我使用的目标?只是如何填充更深的情况(这里是第二级)。我认为我们应该集中精力寻找解决方案。
  • console.log 不会给出所有最深的对象结构。如果测试只是原因,那么我建议您使用node-inspector。您可以将调试器点放在您想要的任何位置,并在调试器工具的scope 可折叠部分以任何深度探索对象。

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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2015-03-26
    • 2020-06-07
    • 2019-12-06
    • 2018-06-07
    • 2014-12-19
    • 2016-10-12
    • 2019-09-23
    • 1970-01-01
    相关资源
    最近更新 更多