Mongodb and sorting sub array

前端 未结 2 413

Not sure if this can be done, so thought I would ask.

I have the following mongodb/s

{
 \"store\":\"abc\",
 \"offers\":[{
    \"spend\":\"100.00\",
          


        
2条回答
  •  野的像风
    2021-01-16 11:02

    Amazingly, yes, mongodb can do this:

    // Sort ascending, by minimum percentage value in the docs' offers array.
    db.collection.find({}).sort({ 'offers.percentage': 1 });
    
    // Sort descending, by maximum percentage value in the docs' offers array.
    db.collection.find({}).sort({ 'offers.percentage': -1 });
    

提交回复
热议问题