MinKey (Smallest Value)
Last updated: 2026-10-09
MinKey (Smallest Value)
MinKey is a special BSON type smaller than every other value, opposite to MaxKey. It serves shard-key design (forcing data into the first chunk), sort sentinels, and aggregation placeholders. A singleton constructed with `MinKey()`, it is rarely used in business code.
| Category | BSON Types |
|---|---|
| MongoDB Version | 1.0+ |
| Example Language | mongosh (Shell) |
📝 Syntax
MinKey()
{ $group: { _id: MinKey(), total: { $sum: "$amount" } } }
Returns:Returns the MinKey singleton
▶ Example
Run the commands below in your local mongosh (requires a running mongod):
// MinKey: placeholder key in aggregation
use shop
db.sales.aggregate([
{ $group: { _id: MinKey(), total: { $sum: '$amount' } } }
])
Output:
{ _id: MinKey(), total: 12900 }
📌 Notes
- MinKey sorts below every value, including null and numbers
- Pairs with MaxKey for shard boundaries
- Rarely used directly in application code
🏷️ Related Tags
💡 Related Cases
📚 Related Tutorials
❓ FAQ
QWhat is MinKey used for?
AShard boundaries, sort sentinels, and aggregation placeholder keys.
QIs MinKey smaller than null?
AYes — MinKey sorts first and MaxKey last in BSON's comparison order.
QCan MinKey be used in queries?
AYes — e.g. { $gte: MinKey() } matches everything.