Doing a simple $match aggregation results in the Expected "[" or AggregationStage but "{" found.
error.
{
$text: {
$search: "search query"
}
}
Doing a simple $match aggregation results in the Expected "[" or AggregationStage but "{" found.
error.
{
$text: {
$search: "search query"
}
}
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
Try This query for Search,
db.collectionname.aggregate([
{
$match:{
"Field_name":
{
$regex:'Search_value',
$options: "si"
}
}}]);
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"
}
}
}
])
You are expected to provide a query object directly in the text box.
Example
/**
* query - The query in MQL.
*/
{
count_field: {"$gt": 1}
}
© 2022 - 2024 — McMap. All rights reserved.