Ajosephraj
Ajosephraj

Reputation: 21

MongoDB - Query to count the nested documents in an array

MongoDB - Layout

MongoDB - Layout

I'm a newbie to MongoDB, so pardon me if I'm using wrong terminologies here.

Given the above layout (attached image), I need to come up with two queries:

Use case 1 : A query that would output the count of total number of elements under someArrayField for a given document identifier. For example, for the above layout, I expect the query to return 2 for someArrayField, because it has 2 elements under it as nested documents.

Use case 2 : A query that would output the count of total number of elements under someArrayField for every document under Collection_1 (Collection_1 can contain multiple documents with the similar layout).

I know, I need to somehow use "Aggregation Pipeline" to get the desired results, but due to the lack of experience with NoSQL, I'm struggling a bit here.

So, any help is greatly appreciated!

Upvotes: 2

Views: 4056

Answers (1)

matthPen
matthPen

Reputation: 4343

Given the following test set :

    { 
    "_id" : ObjectId("5b453cc7799fb211dc44a1e8"), 
    "id" : 1.0, 
    "nestedDoc" : {
        "nestedArray" : [
            {
                "field1" : "value1", 
                "field2" : "value2", 
                "field3" : "value3"
            }, 
            {
                "field1" : "value11", 
                "field2" : "value21", 
                "field3" : "value31"
            }, 
            {
                "field1" : "value12", 
                "field2" : "value22", 
                "field3" : "value32"
            }
        ]
    }
}
{ 
    "_id" : ObjectId("5b453d23799fb211dc44a1e9"), 
    "id" : 2.0, 
    "nestedDoc" : {
        "nestedArray" : [
            {
                "field1" : "value1", 
                "field2" : "value29", 
                "field3" : "value39"
            }, 
            {
                "field1" : "value118", 
                "field2" : "value281", 
                "field3" : "value381", 
                "field4" : "value281"
            }, 
            {
                "field1" : "value172", 
                "field2" : "value272", 
                "field3" : "value372"
            }
        ]
    }
}

Use case 1 : Run the following aggregation query :

db.test1.aggregate(
    [
        // Stage 1 : filter documents
        {
            $match: {
            id:1
            }
        },
        // Stage 2 : count array size and return only it.
        {
            $project: {
                arraySize:{$size:"$nestedDoc.nestedArray"},
                _id:0
            }
        },
    ]
);

**Use case 2 ** Run the following :

db.test1.aggregate(
    [
        // Stage 1 count array size for each document
        {
            $project: {
                _id:0,
                arraySize:{$size:"$nestedDoc.nestedArray"}
            }
        },
        // Stage 2 group all documents and sum arraySize
        {
            $group: {
            _id:null,
            totalArraysSize:{$sum:"$arraySize"}
            }
        },
    ]
);

Upvotes: 3

Related Questions