2

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!

1 Answer 1

3

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"}
            }
        },
    ]
);
Sign up to request clarification or add additional context in comments.

2 Comments

Perfect! - That's what I was looking for usecase_1. However, I was trying to tweak your solution for usecase_2 to get the count per document and not able to successfully do it.
ok, i misunderstood use case 2. if you need array size for each document, just remove the group stage.

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.