nodejs mongodb dynamic filtering

I would like to buit a dynamic filtering engine based on document fileds and values distribution (decision tree for the next field to filter by)

I use mongodb mongoose and nodejs.

i.e :

The DB contains documents describing car characteristics:

{color:"blue", engine:"1598", numOfDoors:3, ...}

I would like to return the best field to filter by the documents. The goal is to return maximum 5 documents that are most compatibele with the characteristics entered.

For example if there are 2 blue cars and 8 red cars and 2 cars with 2 doors, 2 cars with 3 doors, 2 cars with 4 doors, 2 cars with 5 doors, 2 cars with 6 doors.

I would like return numOfDoors as the next field to filter by as the probability for returning the required number of documents(5) is 100% while filtering by color is only 20%.

I would really appreciate it if you could answer the following questions:

  1. How to retrieve more fields to filter by a group of matched documents (documents structure is different from one another due to mongo flexible schema)
  2. Is there a builtin function or model that I can use for calculating the documents distribution by fields and their values?

Thank you