Mongo does not have a max() function, how do I work around this?

后端 未结 3 568
粉色の甜心
粉色の甜心 2021-01-02 20:04

I have a MongoDB collection and need to find the max() value of a certain field across all docs. This value is the timestamp and I need to find the latest doc by finding th

相关标签:
3条回答
  • 2021-01-02 20:32

    Try with db.collection.group

    For example, with this collection:

    > db.foo.find()
    { "_id" : ObjectId("..."), "a" : 1 }
    { "_id" : ObjectId("..."), "a" : 200 }
    { "_id" : ObjectId("..."), "a" : 230 }
    { "_id" : ObjectId("..."), "a" : -2230 }
    { "_id" : ObjectId("..."), "a" : 5230 }
    { "_id" : ObjectId("..."), "a" : 530 }
    { "_id" : ObjectId("..."), "a" : 1530 }
    

    You can use group using

    > db.foo.group({
        initial: { },
        reduce: function(doc, acc) {
            if(acc.hasOwnProperty('max')) {
                if(acc.max < doc.a)
                    acc.max = doc.a;
            } else {
                acc.max = doc.a
            }
          }
        })
    [ { "max" : 5230 } ]
    

    Since there is no key value in group all the objects are grouped in a single result

    0 讨论(0)
  • 2021-01-02 20:34

    For sure if it will be big collection and if you need always display max timestamp you may need create separate collection and store statistic data there instead of order big collection each time.

    statistic
    {
      _id = 1, 
      id_from_time_stamp_collection = 'xxx',
      max_timestamp: value
    }
    

    And whenever new doc come just update statistic collection with id = 1(with $gt condition in query, so if new timestamp will be greater than max_timestamp then max_timestamp will be updated, otherwise - no).

    Also probably you can store and update other statistic data within statistic collection.

    0 讨论(0)
  • 2021-01-02 20:50

    if you have an index on the timestsamp field, finding the highest value is efficientl something like

    db.things.find().sort({ts:-1}).limit(1)
    

    but if having an index is too much overhead storing the max in a separate collection might be good.

    0 讨论(0)
提交回复
热议问题