Home > front end >  Given a mongodb model's array I want to find records in which that array has the most matches u
Given a mongodb model's array I want to find records in which that array has the most matches u

Time:12-29

I have a mongoose schema as such:

const userSchema = new mongoose.Schema({
  keywords: [{ "type": String, "enum": ["yup", "nope"] }],
})

Here, I have one user with a set of keywords and I want to find the records in my database which have the most similar set of keywords to this particular user.

For example, If a user has ["yup" "nope"] as their keywords, I want to find all records of users who have "yup" or "nope" or both in their keywords array. This is just an example and in reality, the users will have a whole lot more keywords to choose from.

How can I do this using mongoose?

I was thinking about one-hot-encoding the values in the array and the records with the most matching 1s can be added to another table "Most similar values table or something" that maintains this list for every user with the user as the foreign key. But I haven't been able to come up with an efficient and/or working algorithm for this yet.

CodePudding user response:

In my opinion the best aproach is regular expresion. I write you one example function how to search and filter data in MongoDB using mongoose. For example lets search customers by lastName example for starts, ends, contains string "yup". Be aware searching with regex is case sensitive default. If you add "i" after regex it will be case insensitive.

async function getCustomers() {
 const customers = await Customer
  //case search lastName whitch starts with "yup" - case sensitive
 .find({lastName: /^yup/})
 //or case search lastName whitch ends with "yup" - case insensitive
 .find({lastName: /yup$/i })
 //or case search lastName whitch contains "yup" in any part 
 .find({lastName: /.*yup.*/ })
 .limit(20) //get top 20 results
 .sort({lastName: 1}) // sort by lastName
console.log(customers)}



 //searching in array
 const customerList = ['Smith', 'Jackson'];

  async function getCustomers(arr) {
   return await Customer
   .find({lastName: {$in: arr}})
   .limit(20) //get top 20 results
   .sort({lastName: 1}) // sort by lastName
   }
  
   getCustomers(customerList);

for more info chceck documentations: https://docs.mongodb.com/manual/reference/operator/query/regex/

  • Related