0

I have a collection in Mongodb called "Data", one of the fields in this collection might has multiple values. I would like to know how many values this field can get at the most (maximum number of values that this field can get). Below is the query that I run:

db.Data.find({"outcome": {$exists: 1, $ne: null} }, {"outcome":1})

Here is the result:

{ 
    "_id" :1, 
    "outcome" : [
        "3-VD"
    ]
}
{ 
    "_id" : 2, 
    "outcome" : [
        "3-VD", 
        "Left main"
    ]
}
{ 
    "_id" : 3, 
    "outcome" : [
        "1-VD",
        "2-VD",
        "3-VD",
    ]
}

I know that with below aggregation query I get number of outcome that each document has, but I want to know what is the maximum number of outcomes in this collection!!!

db.Data.aggregate({$project: { count: { $size:"$outcome" }}})

Shadima
  • 1
  • 1
  • @NeilLunn I had seen the similar question that you refereed to but my question is something else. Updated my question at the end! – Shadima Jun 20 '17 at 08:12
  • `db.Data.aggregate([{ "$group": { "_id": null, "maxcount": { "$max": { "$size": "$outcome" }} }}])`. You [`$group`](https://docs.mongodb.com/manual/reference/operator/aggregation/group/) instead and pass the values from `$size` to [`$max`](https://docs.mongodb.com/manual/reference/operator/aggregation/max/). If you already saw the answer then you should have mentioned it in your question. You did not. – Neil Lunn Jun 20 '17 at 08:19
  • @NeilLunn thanks for your quick and informative reply. I will definitely mention in my next question If I had seen a similar question! :) – Shadima Jun 20 '17 at 08:42

0 Answers0