Morteza Mashayekhi
Morteza Mashayekhi

Reputation: 934

Count query for nested document in MongoDB

I want to count how many Items are available in the sub-document Recommendations in the following document.

{
   catgoryId: 13,
   Recommendations:
   {
       {ItemId: 135, value= 0.8},
       {ItemId: 136, value= 0.7},
       ....
   }
}

}

Upvotes: 0

Views: 64

Answers (1)

Cyphercrack
Cyphercrack

Reputation: 58

You can use the aggregation method:

db.test.aggregate(
  [
   { 
     $project: {
       categoryId:1,
       count: {$size:"$Recommendations"}
     }
   }
]);

Upvotes: 1

Related Questions