在猫鼬中填充数组
Populate array in mongoose
如何填充数组 "ascents" 请帮助我。
{"_id":"5d92775ab93e250910838d98",
"seminars":["5d854deecfed0e2494b03e38","5d85533e56fa1c24588824ff"],
"ascents":[
{"_id":"5d8bd237f55e4326a4faf7d0","dateAscent":"2019-09-20T00:00:00.000Z"},
{"_id":"5d8bd250f55e4326a4faf7d1","dateAscent":"2019-09-20T00:00:00.000Z"},
{"_id":"5d8bd258f55e4326a4faf7d2","dateAscent":"2019-09-20T00:00:00.000Z"},
{"_id":"5d8bd26af55e4326a4faf7d3","dateAscent":"2019-0920T00:00:00.000Z"}
],
"status":true,
"user":"5d84f154d275fd125835b4ec","__v":17
}
我的模型:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var profileSchema = new Schema({
user: { type: Schema.Types.ObjectId, ref: 'User', required: true },
seminars: [{ type: Schema.Types.ObjectId, ref: 'Seminar', required: false }],
ascents: [{ascent:{ type: Schema.Types.ObjectId, ref: 'Ascent', required: false },dateAscent:{ type: Date, required: true, default: Date.now }}],
status: { type: Boolean, default: true }
});
module.exports = mongoose.model('Profile', profileSchema);
谁能帮帮我,我该怎么做?
考虑分开定义 Ascent
模式,以便它有自己的模型
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);
到 populate ascents
您必须确保将有效的 _ids
添加到 ascents
数组
当您添加到新的 Ascents
文档时。
编写查询时,为了用相关文档填充 ascents
数组
你会这样做:
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
文档时,您必须将 _ids
推入与 relatProfile
文档分开的 ascents
数组。请参阅文档 example
如何填充数组 "ascents" 请帮助我。
{"_id":"5d92775ab93e250910838d98",
"seminars":["5d854deecfed0e2494b03e38","5d85533e56fa1c24588824ff"],
"ascents":[
{"_id":"5d8bd237f55e4326a4faf7d0","dateAscent":"2019-09-20T00:00:00.000Z"},
{"_id":"5d8bd250f55e4326a4faf7d1","dateAscent":"2019-09-20T00:00:00.000Z"},
{"_id":"5d8bd258f55e4326a4faf7d2","dateAscent":"2019-09-20T00:00:00.000Z"},
{"_id":"5d8bd26af55e4326a4faf7d3","dateAscent":"2019-0920T00:00:00.000Z"}
],
"status":true,
"user":"5d84f154d275fd125835b4ec","__v":17
}
我的模型:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var profileSchema = new Schema({
user: { type: Schema.Types.ObjectId, ref: 'User', required: true },
seminars: [{ type: Schema.Types.ObjectId, ref: 'Seminar', required: false }],
ascents: [{ascent:{ type: Schema.Types.ObjectId, ref: 'Ascent', required: false },dateAscent:{ type: Date, required: true, default: Date.now }}],
status: { type: Boolean, default: true }
});
module.exports = mongoose.model('Profile', profileSchema);
谁能帮帮我,我该怎么做?
考虑分开定义 Ascent
模式,以便它有自己的模型
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);
到 populate ascents
您必须确保将有效的 _ids
添加到 ascents
数组
当您添加到新的 Ascents
文档时。
编写查询时,为了用相关文档填充 ascents
数组
你会这样做:
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
文档时,您必须将 _ids
推入与 relatProfile
文档分开的 ascents
数组。请参阅文档 example