If looking for the minimum value for a field in MongoDB, use this command:

db..find({"": {$ne: null}}).sort({"":1}).limit(1)

### Explanation

This command **finds** all documents in the collection that have the field you care about, **sorts** them in ascending order, and returns the first one i.e. the minumum.

The purpose of having the `find` method with an argument of `{"": {$ne: null}}` is that this filters out all the documents that don't contain the field you're interested in. If you don't include the **find** argument (that is, do `db..find()...`) and you have a document in the collection that doesn't contain the field (yay [schema less databases](https://jaxenter.com/mongodb-schemaless-database-112878.html)), then that will mess up your query because of [the way Mongo handles null fields](http://docs.mongodb.org/manual/faq/developers/#what-is-the-compare-order-for-bson-types). Hence, we filter out all documents not containing the field, using the [$ne operator](http://docs.mongodb.org/manual/reference/operator/query/ne/#op._S_ne).

The **sort** method takes in [either 1 or -1 to define ascending or descending respectively](http://docs.mongodb.org/manual/reference/method/cursor.sort/#ascending-descending-sort). For the **minimum** case, we want to use `1`.

The **limit** method specifies the number of documents to return. In our case, we only want the first one in the sorted set, so we supply `1`.

Similarly, to get the **maximum** value for a field, use

db..find({"": {$ne: null}}).sort({"":-1}).limit(1)

None of the commands I found on the interwebz had this filtering on the null fields, and that was messing up my query.