To get the unique attributes from by scanning all the records in Collection

We are looking for a query which will get in getting the list of unique attributes in a collection by scanning all the documents .We are not going with MongoDB Compass Schema Option as we are having the schema being analyzed for 1000 records .
We have tried something to pull manually by using the below query .
This works in local but not working as expected in the Sharded collection.

When we tried to run by each stage there is some issue with the $group stage .

Any suggestion s would be greatly appreciated

db.A.aggregate([  { "$project": {
    "data": { "$objectToArray": "$$ROOT" }
  }},
  { "$project": { "data": "$data.k" }},
  { "$unwind": "$data" },
  { "$group": {
    "_id": null,
    "keys": { "$addToSet": "$data" }
  }}
])

Hello :wave: @Geetha_M,

Welcome to the MongoDB Community forums :sparkles:

Could you please clarify whether you are looking for the query for all the documents or only 1000 of them? I understand that Compass analyzes only 1000 documents, but I would appreciate it if you could confirm your approach.

Could you share what is not working, and if possible, share the error message you are receiving?

Could you please explain the issue you are encountering with the $group stage?

Regards,
Kushagra

We need all the records to be scanned .Compass wont help because it uses only 1000 documents.

There is neither an error message nor any results.

As mentioned in the above line there is no error message but the cursor just comes out of the execution.

Hi @Geetha_M,

Could you please provide additional clarification? Are you able to see a returned cursor?

Did you attempt to iterate on the cursor to execute it, and is the cursor empty? Please provide the steps that you did so we can reproduce what you’re seeing.

Regards,
Kushagra