Get all fields in a collection

Hello, I want to extract all the fields present in a collection, including nested fields. How can I extract the fields using shell or compass?

In mongosh, is this what you want?

db.mycollection.aggregate([ { $group: { '_id': "$_id", 'otherFields': { $push: { fields: "$$ROOT" } } } }] )

See also https://www.mongodb.com/docs/manual/reference/operator/aggregation/group

Hello Jack,

I want to list out only field names present in a collection. values are not required.

Object.keys(db.YOURCOLLECTION.findOne()) gets the top level.
You’d need to write something recursive like that to get the sub fields.