Show the output in an array of values

Viewed 23

Hello I have this query result

{
    sac: 1,
    sac_db: 0,
    kafka: 1,
    platform: 13700,
}

now I just want to show the values in an array, but I can't find how to do it:

[1,0,1,13700]
1 Answers

You can get this done using $map and $objectToArray, like so:

db.collection.aggregate([
  {
    $project: {
      _id: 0,
      results: {
        $map: {
          input: {
            $filter: {
              input: {
                "$objectToArray": "$$ROOT"
              },
              cond: {
                $ne: [
                  "$$this.k",
                  "_id"
                ]
              }
            }
          },
          in: "$$this.v"
        }
      }
    }
  }
])

Mongo Playground

Related