Hello everyone,
I’m using $regexFind with regex groups to match and return a specific value buried in a text string, this is a couple of example data objects:
{
"key": 1,
"my_val": "me",
"Attrib1": "sdf dfg me 1 dfg",
"Attrib2": "sdf dfg we 2 dfg",
"Attrib3": "sdf dfg pe 3 dfg"
},
{
"key": 2,
"my_val": "we",
"Attrib1": "sdf dfg me 4 dfg",
"Attrib2": "sdf dfg we 5 dfg",
"Attrib3": "sdf dfg we 6 dfg"
}
]
The objective is to find all the values/integers after " me " in the ‘Attrib’ strings. I have done this but it requires two steps to pull the single captured value:
{
"$addFields": {
"parts": {
"$regexFind": {
"input": "$Attrib1",
"regex": {
$concat: [
"(",
"$my_val",
") ",
"([0-9]+) "
]
}
}
}
}
},
{
"$addFields": {
"part1": {
"$arrayElemAt": [
"$parts.captures",
1
]
}
}
}
])
This works, I get a result for the first object as expected in the added “part1” field but I want to check all the ‘attrib’ strings so it would be helpful to be able to return ONLY the required captured group as either a string or a single element array.
I have tried various syntax to access elements of the returned object (from $regexFind) but none work. Is there a way to achieve the equivalent of this (which I know does NOT work and I shall use add $addFields but to make a point… and hence the question
):
{ "$regexFind": {
"input": "$Attrib1",
"regex": {
$concat: [
"(",
"$my_val",
") ",
"([0-9]+) "
]
}
}
}.captures[1]
Hopefully this makes sense and I have used the back-ticks correctly, any ideals welcome and please don’t dismiss the possibility that I’m missing something obvious/basic/simple! Thank you.