0

As inputs, I get a search query (should operator) and a filter (about whether or not a post is liked, must/mustNot operators). I start with the following as a foundation to my aggregation pipeline:

{ $search: { compound: {} } }

Then, if a query is given, I add this:

$search.compound.should.push({
    text: {
        query: q,
        path: ["data.displayName"],
        score: {
            boost: {
                value: 5,
            },
        },
    },
    // Plus others
});

Now, every post has a likes array. I would like to only select the posts that include a particular string in their likes, if possible under compound.must/mustNot.

I'd like to avoid loading the data on the computer and filtering the documents locally (I pay for the entire database, I use the entire database!). If array manipulation is impossible in Aggregation Pipelines, what could an alternative be? I'm still pretty confused by the docs, and where operators should be used.

deb
  • 6,671
  • 2
  • 11
  • 27

1 Answers1

0

You are so close. In the way you have structured the query, what you need is compound.filter. You can have must clauses as well. It will exclude documents that don't contain the string. See here

$search.compound.must.push({
    text: {
        query: q,
        path: ["data.likes"],
    },
    // Plus others
});
Nice-Guy
  • 1,457
  • 11
  • 20
  • 1
    Thank you for your help, but I may have not been clear enough: I think I've understood how to add layers to my search, but I don't know how to check if an array contains a value, in the same way, that `text` checks for text in a string. – deb Apr 05 '21 at 20:23
  • check out my edit. Thanks for the clarification @a2br – Nice-Guy Apr 06 '21 at 18:05
  • The code indicates if a path exists, not if the array contains a value... Or am I missing something here? – deb Apr 06 '21 at 19:20
  • the first query was if the array contained a string value. But I thought it was a matter of semantics because some of your comment was not exactly using the terminology in the expected sense. So, I thought maybe you meant by array, the single value array that is your path. – Nice-Guy Apr 06 '21 at 22:32
  • I think I need further clarification. – Nice-Guy Apr 06 '21 at 22:33
  • The `likes` field is an array of strings. I need to know, in the aggregation pipeline, if that array includes a particular string. – deb Apr 07 '21 at 16:32