store path of all parent ids + self Id and when I sort by path I get my expected result something like this :
[
{ _id: "id1", parentId: null, text: "text 1", path: "id1/" },
{ _id: "id4", parentId: "id1", text: "text 4", path: "id1/id4/" },
{ _id: "id5", parentId: "id1", text: "text 5", path: "id1/id5/" },
{ _id: "id7", parentId: "id5", text: "text 7", path: "id1/id5/id7/" },
{ _id: "id2", parentId: null, text: "text 2", path: "id2/" },
{ _id: "id6", parentId: "id2", text: "text 6", path: "id2/id6/" },
{ _id: "id3", parentId: null, text: "text 3", path: "id3/" }
];
But I want to sort by Date (asc,desc) and this parent-child structure remains, is that possible ?
this is query :
db.getCollection('comments').aggregate([
{
$sort: { path: 1 }
}
])
I try this but parent-child structure destroyed:
db.getCollection('comments').aggregate([
{
$sort: { createdAt:-1, path: 1 }
}
])
How was it "destroyed"? Path not exists in result?
Обсуждают сегодня