get max value in mongoose

后端 未结 5 1678
心在旅途
心在旅途 2020-12-20 15:23

how to get maximum value in mongoose query . in SQL it is easy

SELECT MAX(LAST_MOD) FROM table1
Where field1=1

i want Equivalent of above

相关标签:
5条回答
  • 2020-12-20 15:28

    I upvoted the Russian colleague before I provided the following answer and I will explain why as my answer.

    My understanding is that the goal is to take the list of whatever the Model out of the collection, sort them in a specific order and take just the first entry out of there.

    So first we start off with a minimum query like so:

    const minQuery = Model.find({}).sort().limit(1);
    

    Notice I am using find() as opposed to findOne(), I believe its more performant that way.

    Then we sort, in this case I will take the colleagues example of Goods as a model to give me an idea of what we are sorting:

    const minQuery = Goods.find({}).sort({ price: 1 }).limit(1);
    

    Here is the key part, I only want to get back the very first good because that first good will be the one with the minimum price and thats where the limit method comes in that you have been seeing:

    const minQuery = Goods.find({}).sort({ price: 1 }).limit(1);
    

    Why did I add this .limit(1); just like my colleague above did?

    Because we always want to minimize the number of records that get returned from our MongoDB database and so using a limit(1) ensures this happens. Again the objective is to be more performant and not be bombarding our Nodejs server with thousands of records.

    So limit(1) ensures that Mongo only communicates back a single record as opposed to a giant number of records. But I am not just providing another variation of a similar answer that I upvoted above.

    I am going to add on another step. Because I only care about the price of this good as it comes back from the query, I don’t care about the goods name or whatever.

    I am going to add on a .then() statement and take the list of goods that get returned and say take the first good out of there and give me its price like so:

    const minQuery = Goods.find({}).sort({ price: 1 }).limit(1).then(goods => goods[0].price);
    

    Keep in mind the terminology I use. I will take the list of goods. Because I used limit(1) that does not mean that the query will resolve with just one model by itself. Because I used the find({}) helper, that means give me back a list of goods or array of goods. So even though I limited that list to 1 it doesn’t mean we are only going to get this called with just a single model by itself.

    Its still going to be an array of models but there is just going to be one in there.

    So we have to recognize this is still going to be an array because we used find({}) but there is going to be a single good in that array.

    The reason I am doing this is to massage the API response. By adding on .then() means if I add on any subsequent .then() in the future anywhere else by taking this min query and adding on a .then() it will be called with just the minimum price.

    So if we add on a .then(minPrice => ) to the query, it will be called with just the minPrice, it will be called with just the minimum price that we care about.

    So the .then() is just to give ourselves a termination to this query.

    So now to do it the other way around, we do:

    const maxQuery = Goods.find({}).sort({ price: -1 }).limit(1).then(goods => goods[0].price);
    
    0 讨论(0)
  • 2020-12-20 15:29

    You can use this, for returning one record in collection (for example Goods):

    Goods
       .find({})
       .select({"price"})
       .sort({"price" : -1})
       .limit(1)
       .exec(function(err, doc){
          let max_price = doc[0].price;
    });
    
    0 讨论(0)
  • 2020-12-20 15:36

    I couldn't get the other answers to work. Perhaps I'm using a newer version of Mongoose (mongoose@3.0.1).

    This worked for me:

    Table1.findOne()
        .where({field1: 1})
        .sort('-LAST_MOD')
        .exec(function(err, doc)
        {
            var max = doc.LAST_MOD;
            // ...
        }
    );
    
    0 讨论(0)
  • 2020-12-20 15:38

    MongoDB supports max/min, but they don't recommend using it in real applications :

    min and max exist primarily to support the mongos (sharding) process.

    http://www.mongodb.org/display/DOCS/min+and+max+Query+Specifiers

    You could pretty much have the same result with :

    Model.findOne({ field1 : 1 }).sort(last_mod, 1).run( function(err, doc) {
         var max = doc.last_mod;
    });
    
    0 讨论(0)
  • 2020-12-20 15:38

    It works perfectly fine.

    Model.findOne().sort({
            "field": -1
        });
    
    0 讨论(0)
提交回复
热议问题