Mongoose - 如何查询自引用关系?
Mongoose - How to query self referencing relationship?
我想为评论及其回复实现 parent/child(自引用)关系,这是架构的代码:
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const BlogCommentSchema = new Schema({
body: String,
dateTime: { type: Date, default: Date.now },
replies: [this]
});
const BlogComment = mongoose.model("blogComments", BlogCommentSchema);
module.exports = BlogComment;
获取评论数或评论回复数的最简单方法是什么?
如有任何帮助,我们将不胜感激。
说明:这是一个基于架构保存文档的示例:
{
"replies": [
{
"replies": [
{
"replies": [],
"_id": "5e558aa01f804205102b4a78",
"body": "Comment 1.1.1",
"dateTime": "2020-02-25T20:59:12.056Z"
}
],
"_id": "5e558aa01f804205102b4a79",
"body": "Comment 1.1",
"dateTime": "2020-02-25T20:59:12.057Z"
},
{
"replies": [
{
"replies": [
{
"replies": [
{
"replies": [
{
"replies": [],
"_id": "5e558aa01f804205102b4a7a",
"body": "Comment 1.2.1.1.1",
"dateTime": "2020-02-25T20:59:12.057Z"
}
],
"_id": "5e558aa01f804205102b4a7b",
"body": "Comment 1.2.1.1.1",
"dateTime": "2020-02-25T20:59:12.057Z"
}
],
"_id": "5e558aa01f804205102b4a7c",
"body": "Comment 1.2.1.1",
"dateTime": "2020-02-25T20:59:12.057Z"
}
],
"_id": "5e558aa01f804205102b4a7d",
"body": "Comment 1.2.1",
"dateTime": "2020-02-25T20:59:12.058Z"
}
],
"_id": "5e558aa01f804205102b4a7e",
"body": "Comment 1.2",
"dateTime": "2020-02-25T20:59:12.058Z"
}
],
"_id": "5e558aa01f804205102b4a7f",
"body": "Comment 1",
"dateTime": "2020-02-25T20:59:12.058Z",
"__v": 0
}
我需要得到这些数字(回复总数是 7,Comment 1.1
的数量是 1,...):
Comment 1 (count is 7)
- Comment 1.1 (count 1)
- Comment 1.1.1
- Comment 1.2 (count 4)
- Comment 1.2.1
- Comment 1.2.1.1
- Comment 1.2.1.1.1
- Comment 1.2.1.1.1.1
我终于设法通过在我的架构中添加一个 virtual type 并使用递归方法获得 replies
:
的总数来获得结果
const getRepliesCount = (comment, count = 0) => {
if (comment.replies.length === 0) {
return 1;
}
for (const reply of comment.replies) {
count += getRepliesCount(reply, count);
}
return count;
};
BlogCommentSchema.virtual("total").get(function() {
return getRepliesCount(this) + 1;
});
虽然我希望可能有一种内置的方式或正确的方式来使用 MongoDB/Mongoose 来做到这一点。
我想为评论及其回复实现 parent/child(自引用)关系,这是架构的代码:
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const BlogCommentSchema = new Schema({
body: String,
dateTime: { type: Date, default: Date.now },
replies: [this]
});
const BlogComment = mongoose.model("blogComments", BlogCommentSchema);
module.exports = BlogComment;
获取评论数或评论回复数的最简单方法是什么?
如有任何帮助,我们将不胜感激。
说明:这是一个基于架构保存文档的示例:
{
"replies": [
{
"replies": [
{
"replies": [],
"_id": "5e558aa01f804205102b4a78",
"body": "Comment 1.1.1",
"dateTime": "2020-02-25T20:59:12.056Z"
}
],
"_id": "5e558aa01f804205102b4a79",
"body": "Comment 1.1",
"dateTime": "2020-02-25T20:59:12.057Z"
},
{
"replies": [
{
"replies": [
{
"replies": [
{
"replies": [
{
"replies": [],
"_id": "5e558aa01f804205102b4a7a",
"body": "Comment 1.2.1.1.1",
"dateTime": "2020-02-25T20:59:12.057Z"
}
],
"_id": "5e558aa01f804205102b4a7b",
"body": "Comment 1.2.1.1.1",
"dateTime": "2020-02-25T20:59:12.057Z"
}
],
"_id": "5e558aa01f804205102b4a7c",
"body": "Comment 1.2.1.1",
"dateTime": "2020-02-25T20:59:12.057Z"
}
],
"_id": "5e558aa01f804205102b4a7d",
"body": "Comment 1.2.1",
"dateTime": "2020-02-25T20:59:12.058Z"
}
],
"_id": "5e558aa01f804205102b4a7e",
"body": "Comment 1.2",
"dateTime": "2020-02-25T20:59:12.058Z"
}
],
"_id": "5e558aa01f804205102b4a7f",
"body": "Comment 1",
"dateTime": "2020-02-25T20:59:12.058Z",
"__v": 0
}
我需要得到这些数字(回复总数是 7,Comment 1.1
的数量是 1,...):
Comment 1 (count is 7)
- Comment 1.1 (count 1)
- Comment 1.1.1
- Comment 1.2 (count 4)
- Comment 1.2.1
- Comment 1.2.1.1
- Comment 1.2.1.1.1
- Comment 1.2.1.1.1.1
我终于设法通过在我的架构中添加一个 virtual type 并使用递归方法获得 replies
:
const getRepliesCount = (comment, count = 0) => {
if (comment.replies.length === 0) {
return 1;
}
for (const reply of comment.replies) {
count += getRepliesCount(reply, count);
}
return count;
};
BlogCommentSchema.virtual("total").get(function() {
return getRepliesCount(this) + 1;
});
虽然我希望可能有一种内置的方式或正确的方式来使用 MongoDB/Mongoose 来做到这一点。