Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9126271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:00:01+00:00 2026-06-17T07:00:01+00:00

I have a Node.js application that saves data to MongoDB. Given one document, I

  • 0

I have a Node.js application that saves data to MongoDB.
Given one document, I want to find the most similar document in the database.

My idea is to implement some sort of nearest neighbour algorithm that takes all the records as a training sequence and returns the most similar document (including some sort of percentage on how similar these two documents are.)

E.g. having these records in my database…

{ name: "Bill",   age: 10,  pc: "Mac",      ip: "68.23.13.8" }
{ name: "Alice",  age: 22,  pc: "Windows",  ip: "193.186.11.3" }
{ name: "Bob",    age: 12,  pc: "Windows",  ip: "56.89.22.1" }

…I want to find the closest document to this one

{ name: "Tom", age: 10, pc: "Mac", ip: "68.23.13.10" }
// algorithm returns "Bill", .76 

Are there any Node modules/implementations that take any kind of objects/parameters and return their nearest neighbour?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T07:00:03+00:00Added an answer on June 17, 2026 at 7:00 am

    Here is some example code. It assumes that you can run the search on every request. If you want to modify it, make sure that all similarity functions return a number between 0 and 1.

    function tokenize(string) {
      var tokens = [];
      for (var i = 0; i < string.length-1; i++) {
        tokens.push(string.substr(i,2));
      }
    
      return tokens.sort();
    }
    
    function intersect(a, b)
    {
      var ai=0, bi=0;
      var result = new Array();
    
      while( ai < a.length && bi < b.length )
      {
         if      (a[ai] < b[bi] ){ ai++; }
         else if (a[ai] > b[bi] ){ bi++; }
         else /* they're equal */
         {
           result.push(a[ai]);
           ai++;
           bi++;
         }
      }
    
      return result;
    }
    
    function sum(items) {
      var sum = 0;
      for (var i = 0; i < items.length; i++) {
        sum += items[i];
      }
    
      return sum;
    }
    
    function wordSimilarity(a, b) {
      var left   = tokenize(a);
      var right  = tokenize(b);
      var middle = intersect(left, right);
    
      return (2*middle.length) / (left.length + right.length);
    }
    
    function ipSimilarity(a, b) {
      var left  = a.split('.');
      var right = b.split('.');
    
      var diffs = [];
      for (var i = 0; i < 4; i++) {
        var diff1 = 255-left[i];
        var diff2 = 255-right[i];
        var diff  = Math.abs(diff2-diff1);
    
        diffs[i] = diff;
      }
    
      var distance = sum(diffs)/(255*4);
    
      return 1 - distance;
    }
    
    function ageSimilarity(a, b) {
      var maxAge   = 100;
      var diff1    = maxAge-a;
      var diff2    = maxAge-b;
      var diff     = Math.abs(diff2-diff1);
      var distance = diff / maxAge;
    
      return 1-distance;
    }
    
    function recordSimilarity(a, b) {
      var fields = [
        {name:'name', measure:wordSimilarity},
        {name:'age',  measure:ageSimilarity},
        {name:'pc',   measure:wordSimilarity},
        {name:'ip',   measure:ipSimilarity}
      ];
    
      var sum = 0;
      for (var i = 0; i < fields.length; i++) {
        var field   = fields[i];
        var name    = field.name;
        var measure = field.measure;
        var sim     = measure(a[name], b[name]);
    
        sum += sim;
      }
    
      return sum / fields.length;
    }
    
    function findMostSimilar(items, query) {
      var maxSim = 0;
      var result = null;
    
      for (var i = 0; i < items.length; i++) {
        var item = items[i];
        var sim  = recordSimilarity(item, query);
    
        if (sim > maxSim) {
          maxSim = sim;
          result = item;
        }
      }
    
      return result
    }
    
    var items = [
      { name: "Bill",   age: 10,  pc: "Mac",      ip: "68.23.13.8" },
      { name: "Alice",  age: 22,  pc: "Windows",  ip: "193.186.11.3" },
      { name: "Bob",    age: 12,  pc: "Windows",  ip: "56.89.22.1" }
    ];
    
    var query  = { name: "Tom", age: 10, pc: "Mac", ip: "68.23.13.10" };
    var result = findMostSimilar(items, query);
    
    console.log(result);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a c# application that saves user's data to an xml document. I
I have a Java application that I want to save the data in XML
I have a node.js application that pulls some data and sticks it into an
So I have an air application that I want to save data from into
I have a node application that is not a web application - it completes
I have a node.js (v0.6.12) application that starts by evaluating a Javascript file, startup.js.
I use node.js and socket.io. I have an application that runs on the IP
I have a memory leak on one of the part of my node.js application.
I have one node.js application published in appfog, but when I try to access
I have a WPF application that has just one button. When the button is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.