0

I have a question regarding filtering in MongoDB.I am looking at the class Filters

I am adding face size parameters as (key,value) to the DB. Later I want to find matches between new face and already faces that are in the DB.I want to search on the value and get the best match face. so example if my (key,value) is (face,100) and there is no exact match. give me back the closest results it could be (face,88) or (face,110). Many Thanks

  • The question itself is a little "fuzzy". Can you be more specific about your case? You are looking at a pretty standard set of database query operators, but there is nothing that says we cannot "brute force" calculations as well. – Neil Lunn Jun 26 '17 at 10:36
  • Neil I have docs with keys and values I want to run search with certain key and a value but if there is no exact match give me the results with the value which is the closest to what I have searched on. basically it called the best match. it could be higher number or lower number but I want to get the closest . for example I am searching for value "facesize":100 give all the faces that closest to 100, it could be 88 or 110 but I want to closest I have in the DB –  Jun 26 '17 at 11:06
  • Can you edit your question and give us a clear example of something you need to match as a real world case? Just off a comment then I say "brute force" the calculation and then sort on the "difference". But if you take the time to explain a real world case then there is likely a reasonable and more elegant solution. – Neil Lunn Jun 26 '17 at 11:10
  • I have edit the question.Many thanks –  Jun 26 '17 at 11:18
  • Possible duplicate of [mongodb - Find document with closest integer value](https://stackoverflow.com/questions/13275491/mongodb-find-document-with-closest-integer-value) – Neil Lunn Jun 26 '17 at 11:22

1 Answers1

0

the best match for a float value could be:

db.collection.find({floatVal : {$gte : 1.0}).sort(floatVal : 1).limit(1)

it will get a little harder if you want to look both ways but it should be possible with a $cond

p.streef
  • 3,652
  • 3
  • 26
  • 50