17

Doing a simple $match aggregation results in the Expected "[" or AggregationStage but "{" found. error.

{
  $text: {
    $search: "search query"
  }
}
Harsha Biyani
  • 7,049
  • 9
  • 37
  • 61
Modermo
  • 1,852
  • 2
  • 25
  • 46

4 Answers4

0

I've encountered the same error, but in my case I use $addFields with $switch statement and here $regexMatch breaks my pipeline in the Compass:

{
  attribute: {
    $switch: {
      branches: [
        {
          case: {
            $regexMatch: {
              input: "$attribute",
              regex: /expression/
            }
          },
          then: "..."
        }
      ],
      default: "$attribute"
    }
  }
}

But when I execute it in the CLI it works perfectly fine

0

Try This query for Search,

db.collectionname.aggregate([
       {
        $match:{
             "Field_name":
                {
                   $regex:'Search_value',
                   $options: "si" 
                 }
 }}]);
Prakash Harvani
  • 1,007
  • 7
  • 18
0

In order to use $text, you need to create a text index. Let's consider that you have a collection named countries and text field named capital. You can create a text index by the command db.countries.createIndex({capital:"text"}).

Now, you can aggregate and use $text in the following manner. Note the square brackets in which we enclose the operations.

db.countries.aggregate([
    {
      $match:{
        $text: {
          $search: "Mumbai"
        }
      }
    }
])
-1

You are expected to provide a query object directly in the text box.

Example

/**
 * query - The query in MQL.
 */
{
  count_field: {"$gt": 1}
}
Siyu
  • 11,187
  • 4
  • 43
  • 55