Visit complete MongoDB roadmap

← Back to Topics List

Max Key

Max Key is a special data type in MongoDB that is used mainly for sorting and comparing values. It has the unique characteristic of being greater than all other BSON types during the sorting process. This makes Max Key quite useful when you need to create a document that should always appear after other documents in a sorted query or when you are setting a limit for a range of data, and you want to ensure that nothing exceeds that limit.

Here is a brief summary of Max Key:


  • Max Key is a constant that holds the value greater than any other BSON data type value.
  • It is used for comparing and sorting values in MongoDB collections.
  • Max Key is a part of the BSON data type, which is the primary data format used in MongoDB for storing, querying, and returning documents.
  • Max Key is not to be confused with a regular value in a document and is primarily used for internal purposes.


To use Max Key in your MongoDB implementation, you can insert it into your document using MongoDB syntax as follows:

  _id: ObjectId("some_id_value"),
  field1: "value1",
  myMaxKeyField: MaxKey()

In this example, myMaxKeyField is assigned the Max Key value.

When you want to sort or compare documents in a collection, Max Key will help ensure that a document will always come last in the results when compared with other BSON types.

Here is an example of how Max Key can be used in a range query:

db.my_collection.find({ age: { $lte: MaxKey() } });

This query will return all the documents in my_collection where the age field is less than or equal to Max Key, essentially retrieving everything, as no value can be greater than Max Key.

In summary, Max Key plays an essential role in MongoDB by providing a constant value that is always greater than other BSON types, thus ensuring proper sorting and comparing behavior in your implementation.

Community is the 6th most starred project on GitHub and is visited by hundreds of thousands of developers every month.

Roadmaps Best Practices Guides Videos Store YouTube by Kamran Ahmed

Community created roadmaps, articles, resources and journeys to help you choose your path and grow in your career.

© · FAQs · Terms · Privacy


The leading DevOps resource for Kubernetes, cloud-native computing, and the latest in at-scale development, deployment, and management.