猫鼬查询没有完全填充

时间:2019-09-10 08:38:31

标签: mongodb mongoose mongoose-populate

我有以下查询:

const comments = await Comment.findById("comment1").populate({
  path: "replies",
  populate: [
    {
      path: "replies"
    }
  ]
});

此查询产生以下JSON响应:

{
    "parentCommentId": null,
    "_id": "comment1",
    "user": "user1",
    "text": "OP",
    "post": "post1",
    "replies": [
        {
            "parentCommentId": "comment1",
            "replyCount": 1,
            "_id": "comment2",
            "user": "user2",
            "text": "reply",
            "post": "post1",
            "replies": [
                {
                    "parentCommentId": "comment2",
                    "_id": "comment3",
                    "user": "user3",
                    "text": "reply to a reply",
                    "post": "post1",
                    "replies": [
                        "5d7757a2e6835f6d98ce723f" // <--- this is not populated
                    ]
                }
            ]
        }
    ]
}

由于某种原因,它仅将Comment文档向下填充两个级别,但是我需要将其向下填充直到replies的长度为0。

我的模式:

const commentSchema = new Schema({
    parentCommentId: {
        type: String,
        default: null
    },
    replyCount: {
        type: Number,
        default: 0
    },
    user: {
        type: mongoose.Schema.Types.ObjectId,
        ref: "user"
    },
    text: {
        type: String,
        required: true
    },
    post: {
        type: mongoose.Schema.Types.ObjectId,
        ref: "post"
    },
    replies: [
        {
        type: mongoose.Schema.Types.ObjectId,
        ref: "comment"
        }
    ]
});

不知道我在这里做错了什么。

0 个答案:

没有答案