So, I have the next schema, it's an index of car parts that is used to automatically find the part types according to the keywords.
nameEnglish: {
type: String
},
keywords: [{
type: String
}]
I have two documents on the database, one is:
[
{ _id: 1, nameEnglish: 'abcdef', keywords: ['a', 'b', 'c', 'd', 'e', 'f'] },
{ _id: 2, nameEnglish: 'cde', keywords: ['c', 'd', 'e'] }
]
What I am want to do now is query this collection with the MOST number of matched keywords.
myArr = ['b', 'c', 'f'];
db.collection.find({ keywords: { $in: myArr } });
I want this to always return the first document, since it has 3 matched keywords, and the second has only one. How can I achieve this?