Finding the highest value from submatrices in documents

Let's say I have the following collection:

{ _id: 1, Array: [ { K: "A", V: 8 }, { K: "B", V: 5 }, { K: "C", V: 13 } ] } { _id: 2, Array: [ { K: "D", V: 12 }, { K: "E", V: 14 }, { K: "F", V: 2 } ] } 

I would like to run a query that returns a sub-document with the highest "V", so in this case I would get:

 { _id: 1, Array: [ { K: "E", V: 14 } ] } 

or simply:

 { K: "E", V: 14 } 

The important part is that I want the memory usage on the Mongo server to be O (1) (no matter how many documents I process, the memory usage is constant), and I only want to get this one attached document using the value Me necessary (I don’t want to upload more attached documents than necessary).

My preferred approach is to use a simple search query, but I'm not sure if this is possible. I suspect that this can also be done using the aggregation structure (or reduce the map?), But I don’t see how to do it. I do not want the result to be stored in a temporary collection, but directly returned to my client (for example, a regular request).

+6
source share
3 answers

The following set of greetings returns what you need.

 db.letters.aggregate([ {$project:{"Array.K":1, "Array.V":1}}, {$unwind:"$Array"}, {$sort:{"Array.V":-1}}, {$limit:1} ]); 

Return:

 {"_id":2, "Array":{"K":"E","V":14}} 

Enjoy! :)

+5
source

As @JohnnyHK said:

 db.col.aggregate([ {$unwind: '$Array'}, {$group: {_id: '$_id', Array: {K: {$max: '$K'}, V: {$max: '$V'}}}} ]) 

Something like that.

+2
source

In simple words , if you have a mongo response request something like below - and you want to get only the highest value from Array-> "Wish_CreatedDate"

 { "_id": "57ee5a708e117c754915a2a2", "TotalWishs": 3, "Events": [ "57f805c866bf62f12edb8024" ], "wish": [ "Cosmic Eldorado Mountain Bikes, 26-inch (Grey/White)", "Asics Men Gel-Nimbus 18 Black, Snow and Fiery Red Running Shoes - 10 UK/India (45 EU) (11 US)", "Suunto Digital Black Dial Unisex Watch - SS018734000" ], "Wish_CreatedDate": [ "2017-03-05T00:00:00.000Z", "2017-02-13T00:00:00.000Z" ], "UserDetails": [ { "createdAt": "2016-09-30T12:28:32.773Z", "jeenesFriends": [ "57edf8a96ad8f6ff453a384a", "57ee516c8e117c754915a26b", "58a1644b6c91d2af783770b0", "57ef4631b97d81824cf54795" ], "userImage": "user_profile/Male.png", "email": " roopak@small-screen.com ", "fullName": "Roopak Kapoor" } ], }, 

*** Then you added

Latest_Wish_CreatedDate: {$ max: "$ Wish_CreatedDate"},

somthing as below -

 { $project : { _id: 1, TotalWishs : 1 , wish:1 , Events:1, Wish_CreatedDate:1, Latest_Wish_CreatedDate: { $max: "$Wish_CreatedDate"}, } } 

And the final response to the request will be lower

 { "_id": "57ee5a708e117c754915a2a2", "TotalWishs": 3, "Events": [ "57f805c866bf62f12edb8024" ], "wish": [ "Cosmic Eldorado Mountain Bikes, 26-inch (Grey/White)", "Asics Men Gel-Nimbus 18 Black, Snow and Fiery Red Running Shoes - 10 UK/India (45 EU) (11 US)", "Suunto Digital Black Dial Unisex Watch - SS018734000" ], "Wish_CreatedDate": [ "2017-03-05T00:00:00.000Z", "2017-02-13T00:00:00.000Z" ], "UserDetails": [ { "createdAt": "2016-09-30T12:28:32.773Z", "jeenesFriends": [ "57edf8a96ad8f6ff453a384a", "57ee516c8e117c754915a26b", "58a1644b6c91d2af783770b0", "57ef4631b97d81824cf54795" ], "userImage": "user_profile/Male.png", "email": " roopak@small-screen.com ", "fullName": "Roopak Kapoor" } ], "Latest_Wish_CreatedDate": "2017-03-05T00:00:00.000Z" }, 
0
source

All Articles