Given a reference array of strings: ["Comedy", "Horror", "Romance"],
I would like to query a Movie model with this schema:
const MovieSchema = new Schema({
_id: {
type: Types.ObjectId,
required: true
},
title: {
type: String,
required: true
},
categories: [{ type: String }],
});
Such that I will get results where I will get Movies with categories in common with the reference array, sorted by the amount of elements it has in common with the reference array. For example:
[
{
_id: "57",
title: "Sample Movie A",
categories: ["Comedy", "Horror", "Romance", "Family"]
},
{
_id: "92",
title: "Sample Movie B",
categories: ["Comedy", "Romance", "Family", "Coming of Age"]
}
]
Note that Movie A is the first in the results because it has 3 items in common with the reference array while Movie B only has 2 items in common.
How can I achieve this query using Mongoose 5.11.16?