How to find a document with an array of strings based on if it has items in common with a reference array of string?

Berry

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?

eol

You could use $setIntersection to get a count of matching elements, add the resulting array-size as a new field to each document and and then sort by this field.

You could then extend the query to filter matches with a count greater than 0 and remove the categoryCount from the output, e.g.

Movie.aggregate([
  {
    "$addFields": {
      "categoryCount": {
        $size: {
          $setIntersection: [
            [
              "Comedy",
              "Horror",
              "Romance"
            ],
            "$categories"
          ]
        }
      }
    }
  },
  {
    "$match": {
      categoryCount: {
        $gt: 0
      }
    }
  },
  {
    "$sort": {
      categoryCount: -1
    }
  },
  {
    "$project": {
      categoryCount: 0
    }
  }
])

Example on mongoplayground: https://mongoplayground.net/p/ZlUNfB82FRK

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to find a document with an array of strings based on if it has items in common with a reference array of string?

From Dev

How to find an array of strings in a string?

From Dev

How do I reference the string in a array of strings?

From Dev

Objective-C: How to find the most common string in an array?

From Dev

Objective-C: How to find the most common string in an array?

From Dev

How can I return the longest string in an array—even if the array contains items that are not strings?

From Dev

Grouping array items based on string value

From Dev

How to translate items in a String Array?

From Dev

find document where item of array equals to a string

From Dev

How to return a reference to an array of ten strings

From Dev

How to check if string is in array of strings

From Dev

How to split array of strings (string [])?

From Dev

How to turn a string into an array of strings?

From Dev

How to search a string in an array of strings

From Dev

python find string pattern in numpy array of strings

From Dev

Fastest way to find string in the array of strings

From Dev

Return Reference to An Array of Strings

From Dev

How to select items in JQ based on value in array

From Dev

How to get a id of string array from Strings.xml without direct id reference

From Dev

find a string in a sorted array of strings having some NULL strings as well

From Dev

find a string in a sorted array of strings having some NULL strings as well

From Dev

How to find duplicates of three variables that reference an array?

From Dev

How to find duplicates of three variables that reference an array?

From Java

How to create an array of strings based on a variable in C

From Dev

Find duplicate items in array

From Dev

How to find the string that matches in array?

From Dev

Find common items in list of lists of strings

From Dev

Find common items in list of lists of strings

From Dev

How to find common part among all items in the string list?

Related Related

  1. 1

    How to find a document with an array of strings based on if it has items in common with a reference array of string?

  2. 2

    How to find an array of strings in a string?

  3. 3

    How do I reference the string in a array of strings?

  4. 4

    Objective-C: How to find the most common string in an array?

  5. 5

    Objective-C: How to find the most common string in an array?

  6. 6

    How can I return the longest string in an array—even if the array contains items that are not strings?

  7. 7

    Grouping array items based on string value

  8. 8

    How to translate items in a String Array?

  9. 9

    find document where item of array equals to a string

  10. 10

    How to return a reference to an array of ten strings

  11. 11

    How to check if string is in array of strings

  12. 12

    How to split array of strings (string [])?

  13. 13

    How to turn a string into an array of strings?

  14. 14

    How to search a string in an array of strings

  15. 15

    python find string pattern in numpy array of strings

  16. 16

    Fastest way to find string in the array of strings

  17. 17

    Return Reference to An Array of Strings

  18. 18

    How to select items in JQ based on value in array

  19. 19

    How to get a id of string array from Strings.xml without direct id reference

  20. 20

    find a string in a sorted array of strings having some NULL strings as well

  21. 21

    find a string in a sorted array of strings having some NULL strings as well

  22. 22

    How to find duplicates of three variables that reference an array?

  23. 23

    How to find duplicates of three variables that reference an array?

  24. 24

    How to create an array of strings based on a variable in C

  25. 25

    Find duplicate items in array

  26. 26

    How to find the string that matches in array?

  27. 27

    Find common items in list of lists of strings

  28. 28

    Find common items in list of lists of strings

  29. 29

    How to find common part among all items in the string list?

HotTag

Archive