如何将相同的模式引用为猫鼬中的类型[重复]

Posted

技术标签:

【中文标题】如何将相同的模式引用为猫鼬中的类型[重复]【英文标题】:How to refere same schema as a type in moonsoose [duplicate] 【发布时间】:2020-02-28 05:51:36 【问题描述】:

我有一个评论列表对象,其中存储了 cmets,并且任何回复该评论的人都会将其存储在子项中


    "_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: Cast to embedded failed for value " comment: \'hi darling\',\n children: [],\n 用户:\n _id: \'5dbb81c8c597533bf4c38e75\',\n 用户名: \'arunkavale\',\n 头像:\n \'https://s3.amazonaws.com/uifaces/faces/twitter/johnsmithagency/128.jpg\' " at path "children 错误.. 我不知道该怎么做。请帮助我

【问题讨论】:

【参考方案1】:

修改Subdocuments数组的定义方式如下,试一试

   children:[ChildrenSchema]

【讨论】:

以上是关于如何将相同的模式引用为猫鼬中的类型[重复]的主要内容,如果未能解决你的问题,请参考以下文章

按猫鼬中的引用属性过滤[重复]

更新猫鼬中的嵌套模式[重复]

在猫鼬中,如何根据相关集合中的值查找记录?

如何使用父引用填充猫鼬中的一对多关系?

如何将 JSON 模式转换为猫鼬模式

猫鼬中的嵌套子模式