I have experience in MySQL and new to MongoDB. What I need to achieve is below. MongoDb collection is like: enter image description here

What I want is a query similar to MySQL as:

Select title,binary,hummingDistance(this.binary,"") as humDistance from Table order by humDistance desc limit 1

Here hummingDistance() is a MongoDB function I have created returns a value.

I am using PHP and also MongoDB Compass for testing. Thanks in advance.

  • Can you edit your question to provide more information on the function you've created and your specific version of MongoDB server? Is "humming distance" actually hamming distance? MongoDB (as at 4.2) does not allow for user-defined sort functions outside of the Aggregation Pipeline. Your query may be possible if you can write the calculation using agg expressions, but your example requires a collection scan and in-memory sort so could be challenging to scale for a large collection. This may be faster (and simpler) to implement in client code. – Stennie Jul 7 at 22:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.