Is it possible to sort the entries in each document in a specific order in compass?

This is just to make it easy to work visually in compass. For example, all records are sorted in alphabetical order. I have not found anywhere whether it is possible to do so.

Sure, @OlegRC2_OlegRC2,

Could you please share your query and provide a sample example of the collection you’re working with? I believe sorting can be accomplished using the $sort operator.

The example is in the screenshot. For example I want all “Boolean” values to be on top of the document. Can I do that? I don’t have any problems with programmatic work, I just need to be visually comfortable working in Compass. For example, I found a document, it has 30 fields, and I want all fields of type “Boolean” to be at the top of the document.
Screenshot_10

This is not possible. If you want to display your data in a specific order you must write an application to view your data.

However, your schema can be improved by using hierarchy of objects rather than being a big flat list of fields. For example, all your fields that starts with ratio could be grouped together under an object ratio such as

ratio : {
    common : 1.54 ,
    epic : 1.25 ,
    ...
    uncommon : 1.43
}

and

allow : {
    play : true ,
    deposit : true ,
    ...
    withdraw : true
}
withdraw : {
   max : 400 ,
   min : 1 ,
   delay_hours : 24
}

Having your fields logically grouped is much better that having them visually grouped anyway. It also naturally leads to better and safer code since now can pass objects rather than full documents to functions.

1 Like

now I understand what you want.
Below is the query which does the same.

[i
  {
    "$project": {
      "allFields": {
        "$objectToArray": "$$ROOT"
      }
    }
  },
  {
    "$project": {
      "booleanFieldsTrue": {
        "$filter": {
          "input": "$allFields",
          "as": "field",
          "cond": {
            "$eq": ["$$field.v", true]
          }
        }
      },
      "booleanFieldsFalse": {
        "$filter": {
          "input": "$allFields",
          "as": "field",
          "cond": {
            "$eq": ["$$field.v", false]
          }
        }
      },
      "otherFields": {
        "$filter": {
          "input": "$allFields",
          "as": "field",
          "cond": {
            "$and": [
              { "$ne": ["$$field.v", true] },
              { "$ne": ["$$field.v", false] }
            ]
          }
        }
      }
    }
  },
  {
    "$addFields": {
      "sortedFields": { 
        "$concatArrays": [
          "$booleanFieldsTrue", 
          "$booleanFieldsFalse", 
          "$otherFields"
        ] 
      }
    }
  },
  {
    "$replaceRoot": { "newRoot": { "$arrayToObject": "$sortedFields" } }
  }
]

This pipeline works as follows:

  1. In the first $project stage, it converts each document into an array of key-value pairs where the keys are the field names and the values are the field values.
  2. In the second $project stage, it filters the boolean fields into one array and other fields into another array.
  3. It then combines these arrays so that the boolean fields appear first using the $concatArrays stage.
  4. Finally, it converts the combined array back into an object using $arrayToObject, effectively giving you documents where boolean fields are sorted to appear at the top.
1 Like

To match the requirement

the pipeline is missing a final $merge stage.