0
我需要具有IDS所以我嘗試下面的代碼,但數據不能插入 代碼 我的模型的嵌套子模式..發生錯誤在嵌套子模式集合使用貓鼬
var connection= handler.getConnection();
console.log(connection);
autoIncrement.initialize(connection);
var subSchema = mongoose.Schema({
course_Submodule: [{
type: String,
required: true,
}]
},{ _id : false });
subSchema.plugin(autoIncrement.plugin, {
model: 'Submodule',
field: 'Id_submodule',
startAt: 1,
incrementBy: 1
});
var courseSchema = new Schema({
course_Name: String,
course_Code: String,
course_Submodule: [subSchema],
course_Author: String,
id_subject: String,
id_user: String,
});
courseSchema.plugin(autoIncrement.plugin, {
model: 'Course',
field: 'Id_course',
startAt: 1,
incrementBy: 1
});
var Course = connection.model('Course', courseSchema);
var Submodule = connection.model('Submodule', subSchema);
module.exports = Course;
BT在DB數據被插入這樣
"_id" : ObjectId("578efe6da667fff80d09d5ed"),
"Id_course" : 214,
"course_Name" : "chemistry1",
"course_Code" : "ch1",
"course_Author" : "David",
"id_subject" : "3",
"course_Submodule" : [
{
"Id_submodule" : 14,
"course_Submodule" : [ ]
},
{
"Id_submodule" : 15,
"course_Submodule" : [ ]
}
],
"__v" : 0
嘗試這些代碼我不能插入course_Submodule.Is值療法這方面的任何anotherway。幫助我,請
如果您希望將其他模式的結果插入到您的現有模式中,則無需複製項目,但只能存儲其對象ID,並且可以在從數據庫中提取對象時填充對象。如果你想我可以爲你提供一個如何實現這個的快速例子。 –
我想存儲這樣{ 子模塊_id: 「mainId」 subdocArray: { _id: 「unwantedId」, 字段: 「值」 }, { _id: 「unwantedId」, 字段: 「價值」 } ] } – luee
是的,我想要thal代碼@AjitejKaushik – luee