need get all key from object in mongodb need get all key from object in mongodb mongoose mongoose

need get all key from object in mongodb


  • $objectToArray convert spec object to array in key-value format
  • $unwind deconstruct spec array
  • $group by null and construct the unique array of spec object's key using $addToSet
db.collection.aggregate([  { $project: { spec: { $objectToArray: "$spec" } } },  { $unwind: "$spec" },  {    $group: {      _id: null,      spec: { $addToSet: "$spec.k" }    }  }])

Playground