An item in collection contains an array of strings. I would like to find and sort items with most matching elements in array.
Consider a collection:
[
{
"item_name":"Item_1",
"tags":["A","B","C","D","E"]
},
{
"item_name":"Item_2",
"tags":["A","B","D","E","G"]
},
{
"item_name":"Item_3",
"tags":["B","C","E","H"]
}
]
I want to sort the collection based on an array like ["B","D","G","F"] which will return
[
{
"item_name":"Item_2",
"tags":["A","B","D","E","G"]
},
{
"item_name":"Item_1",
"tags":["A","B","C","D","E"]
},
{
"item_name":"Item_3",
"tags":["B","C","E","H"]
}
]
The expected order will be Item_2,Item_1 and then Item_3 since,
- Item_2 matches 3 items ("B","D" and "G")
- then Item_1 with 2 matches ("B" and "D")
- finally Item_3 with 1 match ("B")
If not in mongodb, JavaScript methods will also be appreciated