'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"
}
}
Solution 1:[1]
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
Solution 2:[2]
Try This query for Search,
db.collectionname.aggregate([
{
$match:{
"Field_name":
{
$regex:'Search_value',
$options: "si"
}
}}]);
Solution 3:[3]
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"
}
}
}
])
Solution 4:[4]
You are expected to provide a query object directly in the text box.
Example
/**
* query - The query in MQL.
*/
{
count_field: {"$gt": 1}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Gleb Ignatev |
| Solution 2 | Prakash Harvani |
| Solution 3 | radheya kale |
| Solution 4 | Siyu |
