2

I want to know if there is a way to get maximum value in a column of a MongoDB collection using Mongoengine. is it possible?

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
ehsan shirzadi
  • 4,709
  • 16
  • 69
  • 112

2 Answers2

7

If the column with your ordinal value is named "max_column":

MyDocument.objects().order_by("-max_column").limit(-1).first()
Tristan Brown
  • 561
  • 5
  • 10
Ross
  • 17,861
  • 2
  • 55
  • 73
  • What does limit(-1) does? In the documentation it only discusses a positive value or a 0... – Mr.WorshipMe May 12 '17 at 11:15
  • Doesn't first() implicitly put a limit(1) on the request? – Mr.WorshipMe May 12 '17 at 11:22
  • @Mr.WorshipMe See [numberToReturn](https://docs.mongodb.com/manual/reference/mongodb-wire-protocol/#op-query) in the mongo wire protocol docs: _If numberToReturn is 0, the db will use the default return size. If the number is negative, then the database will return that number and close the cursor. No further results for that query can be fetched. If numberToReturn is 1 the server will treat it as -1 (closing the cursor automatically)._ – Gordo May 26 '17 at 09:10
  • @tinix Thanks, I've missed that part about negative numbers. As first() already returns a single object and closes the cursor - isn't the use of limit(-1) redundant here? – Mr.WorshipMe May 26 '17 at 12:40
  • 2
    @Mr.WorshipMe first() only unwraps the result set to get you one object. If you called a query that returns 500 objects, and call first() on it, you still get 500 objects returned from the database (via a cursor), but the ORM just throws away all but one the first() item that you pop off the result set. The -1 simply closes the cursor in the database after one result, so you don't fetch all the other objects or leave the cursor open. You just fetch one, and only one. It's an optimization. – Gordo May 26 '17 at 16:36
  • More from the mongo docs: Limits the number of documents in the first OP_REPLY message to the query. However, the database will still establish a cursor and return the cursorID to the client if there are more results than numberToReturn. If the client driver offers ‘limit’ functionality (like the SQL LIMIT keyword), then it is up to the client driver to ensure that no more than the specified number of document are returned to the calling application. – Gordo May 26 '17 at 16:36
  • @Gordo - but the minus sign (on the count, NOT the key!) is redundant. correct, because _"If numberToReturn is 1 the server will treat it as -1 "_? – holdenweb Jul 13 '21 at 19:56
4

Hidden from the documentation (make note to request a patch) is the .get_collection() accessor which returns the raw pymongo object for a collection. Here you can use operations like .aggregate()

ClassModel._get_collection().aggregate([
    { "$group": {
        "_id": Null,
        "max_column": { "$max": "$column" }
    }}
])

Where of course ClassModel is the actual class you are using and "column" is the name of the column you want the max value from, obtained with the $max operator. The $ prefix on the field is how the aggregation framework identifies field variables in the pipeline.

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317