我有一个评论列表对象,用于存储评论,而对此评论进行回复的人将其存储在子项中
{
"_id": "5dbc479babc1c22683b73cf3",
"comment": "wow .. this is awsome",
"children": [
{
"_id": "5dbc481ec3bb512780ebda22",
"comment": "second child",
"children": [
{
"_id": "5dbc481ec3bb512780ebda22",
"comment": "hi darling",
"children": [],
"user": {
"_id": "5dbb81c8c597533bf4c38e75",
"username": "arunkavale",
"avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/johnsmithagency/128.jpg"
},
"updatedDate": "2019-11-01T14:58:38.188Z",
"createdDate": "2019-11-01T14:58:38.188Z"
}
],
"user": {
"_id": "5dbb81c8c597533bf4c38e75",
"username": "arunkavale",
"avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/johnsmithagency/128.jpg"
},
"updatedDate": "2019-11-01T14:58:38.188Z",
"createdDate": "2019-11-01T14:58:38.188Z"
},
{
"_id": "5dbc481ec3bb512780ebda22",
"comment": "yep",
"children": [],
"user": {
"_id": "5dbb81c8c597533bf4c38e75",
"username": "arunkavale",
"avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/johnsmithagency/128.jpg"
},
"updatedDate": "2019-11-01T14:58:38.188Z",
"createdDate": "2019-11-01T14:58:38.188Z"
}
],
"user": {
"_id": "5dbb9683b44bfa2a3dce55bd",
"username": "mayank",
"avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/alxndrustinov/128.jpg"
},
"createdDate": "2019-11-01T14:56:27.580Z",
"updatedDate": "2019-11-01T14:58:38.188Z",
"__v": 0
}
这是我设计的架构
var mongoose = require('mongoose');
let UserSchema = new mongoose.Schema({
username:{
type:String
},
avatar:{
type:String
}
});
var ChildrenSchema = new mongoose.Schema({
"comment":{
type:String
},
parentId:{
type: mongoose.Schema.Types.ObjectId,
},
children:{
type:[ChildrenSchema]
},
user:{
type:UserSchema
}
},{timestamps: { createdAt: 'createdDate', updatedAt: 'updatedDate' }});
let CommentSchema = new mongoose.Schema({
user:{
type:UserSchema,
},
"comment":{
type:String
},
children:{
type:[ChildrenSchema]
}
},{timestamps: { createdAt: 'createdDate', updatedAt: 'updatedDate' }});
var Comment = mongoose.model('Comment', CommentSchema);
module.exports = {Comment};
在这里,我试图给子类型输入相同的ChildrenSchema,但是它不起作用,它会抛出 {CastError:强制转换为嵌入值“ {注释失败:\'hi darling \',\ n儿童: [],\ n用户:\ n {_id:\'5dbb81c8c597533bf4c38e75 \',\ n用户名:\'arunkavale \',\ n头像:\ n \'https://s3.amazonaws.com/uifaces/faces/twitter/johnsmithagency/128.jpg \'}}“” 错误..我没有怎么做。请帮助我
答案 0 :(得分:0)
按如下所示修改定义Subdocuments数组的方式并尝试
children:[ChildrenSchema]