Facet without list result

Hello,

I’m using this view

db.createView("filestatistic", "file", [{
"$facet": {
    "byte_size": [{
        "$group": {
            "_id": null,
            "max": {"$max": "$byte_size"},
            "min": {"$min": "$byte_size"},
            "avg": {"$avg": "$byte_size"},
        }
    }],
    "width": [{
        "$group": {
            "_id": null,
            "max": {"$max": "$image.width"},
            "min": {"$min": "$image.width"},
            "avg": {"$avg": "$image.width"},
        }
    }],
    "height": [{
        "$group": {
            "_id": null,
            "max": {"$max": "$image.height"},
            "min": {"$min": "$image.height"},
            "avg": {"$avg": "$image.height"},
        }
    }],
    "mediatypes": [{
        "$group": {
            "_id": "$media_type"
        }
    }]
}

}])

The result JSON (which is used for a service) is

{
    "byte_size": [
        {
            "_id": null,
            "max": 171763,
            "min": 127114,
            "avg": 152524.0
        }
    ],
    "width": [
        {
            "_id": null,
            "max": 794,
            "min": 794,
            "avg": 794.0
        }
    ],
    "height": [
        {
            "_id": null,
            "max": 1123,
            "min": 1123,
            "avg": 1123.0
        }
    ],
    "mediatypes": [
        {
            "_id": "image/jpeg"
        }
    ]
}

Is it possible to change the query, so that I get e.g.

"height: {
     "max":
     "min":
     "avg":
}

So I would like to remove the list and I don’t know how.
Thanks a lot

Try the following:

[
  {
    // Making height array element 0 as an object and
    // removing other top level fields.
    "$set" : 
    {
      "height" : { "$arrayElemAt" : [ "$height" , 0 ] } 
    }
  } ,
  {
    // Removing top level _id and _id inside height object.
    $unset:
    [
      "_id" ,
      "height._id"
    ]
  }
]

Tanks for this solution, but in my case I would like to have got a JSON result like

{
    "height: {
        "max":
        "min":
        "avg":
    },
    "byte_size: {
        "max":
        "min":
        "avg":
    },
   "width": {
        "max":
        "min":
        "avg":
   },
   "mediatypes": {
        "_id":
   }
}

Sorry for my fault, I don’t describe the expected result quite well

I have shown what you have to do to get what you want with the field height. You simply apply the same stage to your other fields.

But I don’t understand where I have to put my $group statement in your solution, because I need the $group to calculating the values and the result of this group calculation should be a single element.

You wrote

So you leave the $group stages where they are inside your $facet and you simply add the stages I posted to your view pipeline.

Did I understand it correctly, that your solution is a second pipeline step after my $facet, because pipeline is a list of objects?

Yes that is what I meant.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.