Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在mongoose中填充数组_Mongoose_Mongoose Schema_Mongoose Populate - Fatal编程技术网

在mongoose中填充数组

在mongoose中填充数组,mongoose,mongoose-schema,mongoose-populate,Mongoose,Mongoose Schema,Mongoose Populate,我怎样才能填充阵列上升帮助我 {id:5d92775ab93e250910838d98, 研讨会:[5D854EECFED0E2494B03E38,5d85533e56fa1c24588824ff], 上升:[ {id:5D8BD237F55E4326A4 FAF7D0,上升日期:2019-09-20T00:00:00.000Z}, {id:5D8BD250F55E4326A4 FAF7D1,上升日期:2019-09-20T00:00:00.000Z}, {id:5D8BD25F55E4326A

我怎样才能填充阵列上升帮助我

{id:5d92775ab93e250910838d98, 研讨会:[5D854EECFED0E2494B03E38,5d85533e56fa1c24588824ff], 上升:[ {id:5D8BD237F55E4326A4 FAF7D0,上升日期:2019-09-20T00:00:00.000Z}, {id:5D8BD250F55E4326A4 FAF7D1,上升日期:2019-09-20T00:00:00.000Z}, {id:5D8BD25F55E4326A4 FAF7D2,上升日期:2019-09-20T00:00:00.000Z}, {id:5D8BD26AF55E4326A4 FAF7D3,上升日期:2019-0920T00:00:00.000Z} ], 状态:正确, 用户:5d84f154d275fd125835b4ec } 我的模型:

var mongoose=需要“mongoose”; var Schema=mongoose.Schema; var profileSchema=新模式{ 用户:{type:Schema.Types.ObjectId,ref:'user',必需:true}, 研讨会:[{type:Schema.Types.ObjectId,ref:'Seminar',required:false}], 上升:[{ascent:{type:Schema.Types.ObjectId,ref:'ascent',必需:false},dateAscent:{type:Date,必需:true,默认:Date.now}], 状态:{type:Boolean,默认值:true} }; module.exports=mongoose.model'Profile',profileSchema;
有人能帮我怎么做吗?

考虑分离定义提升模式,使其有自己的模型

const ascentSchema = new Schema({
    dateAscent:{ 
        type: Date, 
        required: true, 
        default: Date.now 
     }
})

module.exports = mongoose.model('Ascent',ascentSchema  )
然后,您的配置文件架构将如下所示:

var profileSchema = new Schema({
    user: { type: Schema.Types.ObjectId, ref: 'User', required: true },
    seminars: [{ type: Schema.Types.ObjectId, ref: 'Seminar', required: false }],
    ascents: [{type: Schema.Types.ObjectId, ref: 'Ascent'}],
    status: { type: Boolean, default: true }
});

module.exports = mongoose.model('Profile', profileSchema);
要进行上升,您必须确保向上升数组添加有效的_ID 当您添加到新的上升文档时

在编写查询时,为了用相关文档填充Ascences数组 您可以这样做:

Profile.find().
  populate({
    path: 'ascents',
    match: { }, //you can even filter the documents in the array using this match option
    // Explicitly exclude `_id`
    select: '-_id',
    options: { limit: 5 } //Also limit the amount of documents returned
  }).
  exec();

让我知道这种方法是否有效。请记住,在添加新的Ascent文档时,必须将_id推送到ascents数组,该数组是relatProfile文档的一部分。查看文档

您到底想要什么?如何查询和填充或如何定义模式?我想知道如何查询和填充?让我提供一个答案和一些建议