【发布时间】:2020-11-18 20:04:32
【问题描述】:
我有这个聚合操作,它给了我正确的输出,但顺序不一致。重载时,嵌套的输出数组(postteriorThread)改变了文档的顺序,好像没韵没道理!
我对为什么顺序不断变化感到困惑,我想知道为什么会发生这种情况,但我想我只是对它进行排序,我这样做了,但我无法将它重新组合在一起。
我将在下面向您展示我的两个损坏的解决方案,但本质上我想要输出 1,但顺序正确。我正在使用猫鼬,但这不应该有所作为。
谢谢。
1:顺序不一致的解决方案
const posteriorThread = await Comment.aggregate([
{
$match: {
_id: post.threadDescendant,
},
},
{
$graphLookup: {
from: 'comments',
startWith:'$threadDescendant',
connectFromField: 'threadDescendant',
connectToField: '_id',
as: 'posteriorThread',
},
},
]);
输出:1
posteriorThread [
{
"_id": "000",
"name": "foo bar",
"text": "testing one",
"threadDescendant": "123",
"posteriorThread": [
{
"_id": "234",
"name": "foo bar",
"text": "testing four",
"threadDescendant": "345"
},
{
"_id": "345",
"name": "foo bar",
"text": "testing three",
},
{
"_id": "123",
"name": "foo bar",
"text": "testing two",
"threadDescendant": "234"
},
]
}
]
2:更正旧但丢失根文档
const posteriorThread = await Comment.aggregate([
{
$match: {
_id: post.threadDescendant,
},
},
{
$graphLookup: {
from: 'comments',
startWith: '$threadDescendant',
connectFromField: 'threadDescendant',
connectToField: '_id',
as: 'posteriorThread',
},
},
{
$unwind: '$posteriorThread',
},
{
$sort: { 'posteriorThread.depth': 1 },
},
{
$group: { _id: '$_id', posteriorThread: { $push: '$posteriorThread' } },
},
]);
输出 2:
posteriorThread [
{
"_id": "000",
"posteriorThread": [
{
"_id": "123",
"name": "foo bar",
"text": "testing two",
"threadDescendant": "234"
},
{
"_id": "234",
"name": "foo bar",
"text": "testing four",
"threadDescendant": "345"
},
{
"_id": "345",
"name": "foo bar",
"text": "testing three",
},
]
}
]
【问题讨论】:
标签: javascript node.js json mongodb mongoose