Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongooseJS - How to find the element with the maximum value?

I am using MongoDB , MongooseJS and Nodejs.

I have a Collection ( called Member ) with the following Fields -

Country_id , Member_id , Name, Score

I want to write a query which returns the Member with the max Score where Country id = 10

I couldnt find suitable documentation for this in MongooseJS.

I found this at StackOVerflow ( this is MongoDB code )

Model.findOne({ field1 : 1 }).sort(last_mod, 1).run( function(err, doc) {      var max = doc.last_mod; }); 

But how do I translate the same to MongooseJS ?

like image 441
geeky_monster Avatar asked Nov 03 '13 09:11

geeky_monster


People also ask

What is skip and limit in mongoose?

The limit method will be used to return the maximum number of results from the collection, while the skip method is used to skip the number of documents from the collection in MongoDB. If we have one collection name as a student, student collection contains a hundred documents in it.

What does find return if nothing is found MongoDB?

With NodeJS driver version 4.0 , the Collection#findOne returns an undefined when no match is found. In the earlier driver version 3.6 the same method returned a null . Also, when no document is found it is not an error. If you run the same query in the mongo shell the method returns a null , in case there is no match.

What is query in mongoose?

The Mongoose Query class provides a chaining interface for finding, updating, and deleting documents.

What is projection in mongoose?

In MongoDB, projection means selecting only the necessary data rather than selecting whole of the data of a document. If a document has 5 fields and you need to show only 3, then select only 3 fields from them.


2 Answers

Member   .findOne({ country_id: 10 })   .sort('-score')  // give me the max   .exec(function (err, member) {      // your callback code    }); 

Check the mongoose docs for querying, they are pretty good.

If you dont't want to write the same code again you could also add a static method to your Member model like this:

memberSchema.statics.findMax = function (callback) {    this.findOne({ country_id: 10 }) // 'this' now refers to the Member class     .sort('-score')     .exec(callback); } 

And call it later via Member.findMax(callback)

like image 79
tmaximini Avatar answered Oct 02 '22 10:10

tmaximini


You do not need Mongoose documentation to do this. Plain MongoDb will do the job.

Assume you have your Member collection:

{ "_id" : ObjectId("527619d6e964aa5d2bdca6e2"), "country_id" : 10, "name" : "tes2t", "score" : 15 } { "_id" : ObjectId("527619cfe964aa5d2bdca6e1"), "country_id" : 10, "name" : "test", "score" : 5 } { "_id" : ObjectId("527619e1e964aa5d2bdca6e3"), "country_id" : 10, "name" : "tes5t", "score" : -6 } { "_id" : ObjectId("527619e1e964aa5d2bdcd6f3"), "country_id" : 8, "name" : "tes5t", "score" : 24 } 

The following query will return you a cursor to the document, you are looking for:

 db.Member.find({country_id : 10}).sort({score : -1}).limit(1) 
like image 20
Salvador Dali Avatar answered Oct 02 '22 11:10

Salvador Dali