因此,我从角度反应形式发送数据,例如: Angular reactive form UI image
和 Data being sent to backend in browser console image
我为任务制定了方案:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
let taskSchema = new Schema({
taskId:{
type:String,
unique:true
},
task:{
type:String,
default:''
},
subTask:[{
type: Schema.Types.ObjectId,
ref: 'SubTask'
}]
}
module.exports = mongoose.model('Task',taskSchema);
子任务的架构为:
let subTaskSchema = new Schema({
title:String,
subSubTask:[{
type: Schema.Types.ObjectId,
ref: 'SubSubTask'
}]
})
module.exports = mongoose.model('SubTask',subTaskSchema);
子任务模式为:
let subSubTaskSchema = new Schema({
title:String
})
module.exports = mongoose.model('SubSubTask',subSubTaskSchema);
现在,我对如何使用猫鼬在mongodb中保存嵌套的对象数据数组感到困惑吗?
答案 0 :(得分:1)
您可以这样定义模式
const userSchema = new mongoose.Schema({
likedBooks: [{
type: mongoose.Types.ObjectId,
ref: 'books'
}],
email: {
type: String,
required: true
},
name: {
type: String,
required: true
}
});
exports.User = mongoose.model('users', userSchema);
然后您可以通过
填充数据user = User.find({ email: req.body.email }).populate('likedBooks');
在这里点赞的图书包含每本书的_id
const bookSchema = new mongoose.Schema({
isbn: {
type: Number,
required: true
},
name: {
type: String,
required: true
},
author: {
type: String,
required: true
},
publisher: {
type: String,
default: ""
},
imageUrl: {
type: String,
required: true
},
description: {
type: String,
default: ""
}
});
exports.Book = mongoose.model('books', bookSchema);
对于两种模式,我都没有放置_id,因为它是由mongodb自动生成的,并且用作参考
答案 1 :(得分:0)
保存之前,对象模型应该看起来像这样。
{
taskId: 1,
task: 'Do something',
subTask: [{
title: 'Write a essay',
subSubTask: [{
title: 'Collect details to write an essay'
}]
}]
}
希望有帮助...