Is there a way I setup a MongoDB Aggregation Pipeline to do a $regexMatch on every 'text' field no matter what level it's in. I'm currently utilizing Wildcard Text Indexes for handling the text index creation: https://docs.mongodb.com/manual/core/index-text/#wildcard-text-indexes
I want this $regexMatch to be done for every 'text' field. We only want to return documents that have the user text query inside of the 'text' field, and have the pipeline scalable to where we can do a text query search on almost all 'text' fields in the future without having to go back and change anything if we can avoid it. Here is what I have for my current schema ("collection_items" is the root key name in my collection):
"collection_items": [{
"title": "The Title",
"text": "We Want to do a text search on this field",
"text_ideas": {
"one_idea":{
"idea_title":
"text": "Some more text we want to look for user text query"
},
"second_idea":{
"idea_title"
"text": "Even more text we want to look for user text query"
}
}
}]
I've been trying to handle the case where I have the 'text' field in the object within the object (like the text field shown in 'one_idea' and 'second_idea') but have had no luck. Here is what I've tried:
{
"$match": {
"$text": {
"$search": "$word",
"$language": "en",
"$caseSensitive": False,
}
}
},
"$addFields": {
"collection_items": {
"$filter": {
"input": {"$objectToArray": "$$collection_items.item"},
"as": "item",
"cond": {
{
"$filter": {
"input": {"$objectToArray": "$$item.v"},
"as": "secondItem",
"cond": {
{
"$regexMatch": {
"input": "$$secondItem.v.text",
"regex": "$word",
"options": "i",
}
},
}
}
}
}
}
},
But i keep getting the error:
TypeError: unhashable type: 'dict'
In theory you're approach is "fine" if you work out the kinks, you need to ensure you're actually querying the fields you mean to query and at the moment this is not the case.
However have you considered building a wildcard text index instead? this will automatically index every text field in your document. (obv you can also just choose which fields you want indexed).
After that you can use the $text operator to search on that index.