Node.JS + Monk. "ORDER BY" in "find"

You need to make a selection in a certain state and sort the result by the above field. How to do it?

The driver for MongoDB is " Monk ".

+4
source share
1 answer

Assuming you already have access to the collection, you need the find () method:

collection.find(query, options, callback); 

You can use the query object to specify conditions and the parameter object to sort. For details on how to build two objects, see the mongodb driver documentation .

So, in your case, for example, something like this example might work. For the mentioned โ€œconditionโ€, I use the condition that the โ€œquantityโ€ field is greater than 0, and then sort by the largest quantity.

 collection.find( {quantity: {$gt: 0}}, {sort: {quantity: -1}}, function(err, docs) { // docs is the sorted array of your results, process them here }); 

Monk itself is fairly easily documented, but it basically calls mongoskin and then the mongodb native driver, linked above, so the documentation for them is a good place to view it.

+11
source

Source: https://habr.com/ru/post/1493408/


All Articles