3

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

1
  • what is the format of expected output? Commented Feb 10, 2018 at 20:16

2 Answers 2

8

You can try below aggregation. No $unwind required here.

$map and $size to output array with scenes sizes and $sum to count the array values.

db.poems.aggregate({
  "$project":{
    "numberOfActs":{"$size":"$acts"},
    "numberOfScenes":{
      "$sum":{
        "$map":{
          "input":"$acts",
          "in":{"$size":"$$this.scenes"}
        }
      }
    }
  }
})
Sign up to request clarification or add additional context in comments.

Comments

2
db.poems.find().forEach(function(poem) {
    var res = {};
    res._id = poem._id, 
    res.acts = poem.acts.length;
    res.scenes = poem.acts.scenes.length;
    printjson(res)
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.