MaxKey (Largest Value)
Last updated: 2026-10-09
MaxKey (Largest Value)
MaxKey is a special BSON type larger than every other value, the counterpart of MinKey. It serves shard-key design (pushing data into the last chunk), sort sentinels, and aggregation placeholders. A singleton constructed with `MaxKey()`, it mostly appears in sharding metadata.
| Category | BSON Types |
|---|---|
| MongoDB Version | 1.0+ |
| Example Language | mongosh (Shell) |
📝 Syntax
MaxKey()
db.orders.find({ createdAt: { $lte: MaxKey() } })
Returns:Returns the MaxKey singleton
▶ Example
Run the commands below in your local mongosh (requires a running mongod):
// MaxKey: query without an upper bound
use shop
db.orders.find({ createdAt: { $lte: MaxKey() } })
Output:
// All orders returned (MaxKey exceeds every date)
📌 Notes
- MaxKey exceeds every value; $lte: MaxKey() means no upper bound
- Pairs with MinKey for shard boundaries
- Rarely used directly in application code
🏷️ Related Tags
💡 Related Cases
📚 Related Tutorials
❓ FAQ
QWhat is MaxKey used for?
AShard boundaries, sort sentinels, and aggregation placeholder keys.
QWhat is MaxKey larger than?
AEverything — strings, numbers, and dates included.
QWhen will I encounter MaxKey?
AIn sharded-cluster chunk boundaries, metadata, and some aggregation results.