MongoDB Compass $match and $text results in "Expected "[" or AggregationStage but "{" found."
Asked Answered
C

4

17

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

{
  $text: {
    $search: "search query"
  }
}
Craal answered 29/10, 2018 at 3:16 Comment(3)
This is a very frustrating error that I hope they address soon.Boom
did you get answer?Mayman
please provide your search query.Photopia
G
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

Grubstake answered 4/3, 2020 at 13:19 Comment(0)
T
0

Try This query for Search,

db.collectionname.aggregate([
       {
        $match:{
             "Field_name":
                {
                   $regex:'Search_value',
                   $options: "si" 
                 }
 }}]);
Tremaine answered 17/9, 2020 at 14:3 Comment(0)
T
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"
        }
      }
    }
])
Temporary answered 6/4, 2022 at 5:5 Comment(0)
H
-1

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

Example

/**
 * query - The query in MQL.
 */
{
  count_field: {"$gt": 1}
}
Hortensehortensia answered 7/1, 2019 at 15:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.