Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongodb - Find document with closest integer value

Let's assume I have a collection with documents with a ratio attribute that is a floating point number.

{'ratio':1.437}

How do I write a query to find the single document with the closest value to a given integer without loading them all into memory using a driver and finding one with the smallest value of abs(x-ratio)?

like image 356
DeaconDesperado Avatar asked Nov 07 '12 18:11

DeaconDesperado


People also ask

How do I use $in in MongoDB?

Use the $in Operator to Match Values This query selects all documents in the inventory collection where the value of the quantity field is either 5 or 15. Although you can write this query using the $or operator, use the $in operator rather than the $or operator when performing equality checks on the same field.

What does find () do in MongoDB?

Find() Method. In MongoDB, find() method is used to select documents in a collection and return a cursor to the selected documents.

Does MongoDB support range query search?

Explanation: MongoDB supports search by field, range queries, regular expression searches.

How do I find a specific key in MongoDB?

To find if a key/field exists in your document use the $exists operator. Show activity on this post. This does indeed work, but {'$exists': 'true'} will always be more obvious to other code readers than {'$exists': 1} .


1 Answers

Interesting problem. I don't know if you can do it in a single query, but you can do it in two:

var x = 1; // given integer
closestBelow = db.test.find({ratio: {$lte: x}}).sort({ratio: -1}).limit(1);
closestAbove = db.test.find({ratio: {$gt: x}}).sort({ratio: 1}).limit(1);

Then you just check which of the two docs has the ratio closest to the target integer.

MongoDB 3.2 Update

The 3.2 release adds support for the $abs absolute value aggregation operator which now allows this to be done in a single aggregate query:

var x = 1;
db.test.aggregate([
    // Project a diff field that's the absolute difference along with the original doc.
    {$project: {diff: {$abs: {$subtract: [x, '$ratio']}}, doc: '$$ROOT'}},
    // Order the docs by diff
    {$sort: {diff: 1}},
    // Take the first one
    {$limit: 1}
])
like image 90
JohnnyHK Avatar answered Sep 20 '22 02:09

JohnnyHK