I have collection of poems. The document in the collection has the following structure:
{
"_id" : "Romeo and Juliet",
"acts" : [
{
"title" : "ACT I",
"scenes" : [
{
"title" : "SCENE I. Verona. A public place.",
"action" : [
{
"character" : "SAMPSON",
"says" : [
"Gregory, o' my word, we'll not carry coals."
]
},
{
"character" : "GREGORY",
"says" : [
"No, for then we should be colliers."
]
},
// ...
{
"character" : "GREGORY",
"says" : [
"To move is to stir; and to be valiant is to stand:",
"therefore, if thou art moved, thou runn'st away."
]
},
{
"character" : "SAMPSON",
"says" : [
"A dog of that house shall move me to stand: I will",
"take the wall of any man or maid of Montague's."
]
},
{
"character" : "GREGORY",
"says" : [
"That shows thee a weak slave; for the weakest goes",
"to the wall."
]
}
// ...
]
}
// ...
]
}
// ...
]}
I want to count number of acts and scenes for each poem. I tried to do something like this, but the result is incorrect.
db.poems.aggregate([{$unwind:"$acts"}, {$unwind:"$acts.scenes"}, {$group: {_id: "$_id", pa: {$push: {poemAct:"$acts"}}, ps: {$push: {poemScenes:"$acts.scenes"}}}}, {$project: {numberOfActs: {$size: "$pa"}, numberOfScenes: {$size: "$ps"}}}]).pretty()
Help somebody, please :D