Vaibhav
Vaibhav

Reputation: 2143

count no of elements in nested documents in mongodb

I have data in below format

 {
        "result" : [ 
            {                    
                "advertiser" : "lordandtaylor"
                ,
                "networks" : [ 
                    "Pepperjam"
                ],
                "publishers" : [ 
                    "best-price.com", 
                    "allthingswholesale.com"
                ]
            }]
    }

Now I want to display this in below format

{"advertiser":"lordandtaylor" , "networks":"1" , "publishers":"2"}

Upvotes: 1

Views: 63

Answers (1)

Vaibhav
Vaibhav

Reputation: 2143

I got the solution

db.link.aggregate([
{
    {   
    $project : { _id: 0, advertiser : "$_id.advertiser", networks : {$size : "$networks"} , advertisers : {$size : "$publishers"} } 
}
]);

Upvotes: 1

Related Questions