У меня есть запрос монго, который имеет два раскручивания, чтобы получить массив глав. Мне нужно получить сюжетный "слаг" текущей главы. Я попытался использовать «story.slug»: 1 в части $project, но это просто вернуло мне массив слагов, которыми владеет пользователь.
Документ монго выглядит так:
{
name: 'Test User',
slug: 'test_user',
email: '[email protected]',
password: 'test',
story: [
{
title: 'Story Title',
blurb: 'Epic story of a girl',
slug: 'epic_story_of_a_girl', // Need this ....
active: true,
chapters: [{
title: 'Story',
chapter: '1',
content: 'This is the story of a girl. Cried a river and drowned the whole world.',
slug: '1',
}]
}
Мой запрос монго выглядит так:
db.users.aggregate([
{$project: {
email: 1,
name: 1,
slug: 1,
chapters: "$story.chapters"
}},
{$unwind: "$chapters"},
{$unwind: "$chapters"},
{$match: {"chapters.active": true}},
{$sort: {"chapters._id": -1}},
{$limit: 20}
])
Результат выглядит следующим образом:
{
"name" : "Test User",
"slug" : "test_user",
"email" : "[email protected]",
"chapters" : {
"title" : "Story",
"chapter" : "1",
"content" : "This is the story of a girl. Cried a river and drowned the whole world.",
"slug" : "1"
}
}
Но я хочу, чтобы результат выглядел так или похоже, и я понятия не имею, как это сделать.
{
"name" : "Test User",
"slug" : "test_user",
"email" : "[email protected]",
"storySlug": "epic_story_of_a_girl" // This thing
"chapters" : {
"title" : "Story",
"chapter" : "1",
"content" : "This is the story of a girl. Cried a river and drowned the whole world.",
"slug" : "1"
}
}