title: node.js+mongoose - How to implement matching (like order matching)?
body: <p>I have a node.js+mongoose rest api. I have two schemas which needs to be matched as and when a new entry is added to either one or on a timely basis. The matching will compare the whole set of documents with a set of paramters. Let's say, for example, i have the below 2 schemas - </p>
  
  <pre><code>Males: {
  age: Number,
  location: String,
  language: String,
  matchedFemales: []
  }
  
  Females: {
  age: Number,
  location: String,
  language: String,
  matchedMales: []
  }
  </code></pre>
  
  <p>Now, i have to take a collection and scroll through all the documents and find matches. I have lots of parameters as matching criteria, but let us take for an example, language, location should be same and the age is almost equal (+ or - 1 year). Like below - </p>
  
  <pre><code>Males: [{
      id: 1001,
      age: 20,
      location: London,
      language: English,
      matchedFemales: [2001,2002]
      },
      {
      id: 1002,
      age: 30,
      location: London,
      language: English,
      matchedFemales: []
      },
      {
      id: 1003,
      age: 20,
      location: Madrid,
      language: Spanish,
      matchedFemales: [2003]
      }]
  
  
  Females: [{
      id: 2001,
      age: 20,
      location: London,
      language: English,
      matchedFemales: [1001]
      },
      {
      id: 2002,
      age: 19,
      location: London,
      language: English,
      matchedFemales: [1001]
      },
      {
      id: 2003,
      age: 20,
      location: Madrid,
      language: Spanish,
      matchedFemales: [1003]
      }]
  </code></pre>
  
  <p>How to perform this matching and how to store the matches? </p>
  
  <ol>
  <li><p>Should i iterate though each document in Male collection and find matches in the Female collection and update it? IF so, i plan to have a service do it and call this service every X minutes. This job will be time and resource consuming (as it has to go through each document and find matches) but will be run for a definite number of times per day. If its every 5 mins, then it will be run only 12 times in an hour. </p></li>
  <li><p>Instead of matching all documents in LHS against all documents in RHS, as and when a record is getting inserted, i can find matches just for that document and update it. This method will be less time &amp; resource consuming than the previous method, but it will be run more number of times i.e., for every insert/update this has to be done. </p></li>
  </ol>
  
  <p>Or is there any other elegant way to do this? </p>
  
  <p>P.S - If this question seems inappropriate, kindly direct me to the right source for reference or consultation. </p>