I have a mongodb collection with an array field, containing a list of strings. There may be repeats in those strings. for example:
doc1 = {a: ["p", "q", "r", "p", "r"]}
doc2 = {a: ["p", "q", "q"]}
doc3 = {a: ["p"]}
doc4 = {a: ["p", "r", "r"]}
I want to find all the documents that, given a string (say, "p"), finds all the documents that have the string at least two times in the array.
For example:
query("p") == [doc1]
query("q") == [doc2]
query("r") == [doc1, doc4]
Is there a way to do this directly in mongo? I know I can query for occurrence once, and then filter the results on my application, but I'd rather avoid that.