Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

{
  $text: {
    $search: "search query"
  }
}
like image 679
Modermo Avatar asked Dec 09 '25 04:12

Modermo


1 Answers

You just have to use the $match stage in the aggregation pipeline and wrap it in an array, as MongoDB's aggregate() method expects an array of pipeline stages—not a single object.

However, when you pass a single object or query, some MongoDB shells or terminals may implicitly convert it into an array. This is why it might appear to work in some environments but not others.

You can try this working aggregation query:

db.collection.aggregate([
  {
    $match: {
      $text: {
        $search: "search query"
      }
    }
  }
])

If you're using MongoDB Compass, you can test this by pasting the above aggregation query into the Aggregation Pipeline Editor and clicking “Run” to check if it works.

Also, if your goal is just to perform a text search without using aggregation, you can use the simpler find() method:

db.collection.find({
  $text: {
    $search: "search query"
  }
})

This is more straightforward when you're only searching and don't need additional stages like $project, $group, etc.

like image 187
Snehal Shelar Avatar answered Dec 11 '25 22:12

Snehal Shelar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!