Disclaimer: Totally new in MongoDB.
I have a document structured something like this:
{
"hd": [
{
"rd": [
{
"rm": [
{
"code": "a1asd"
},
{
"code": "a24aer"
}
]
},
{
"rm": [
{
"code": "b16hwe7"
},
{
"code": "b2z41s"
}
]
}
]
}
]
}
And I want return the subdocument that only contains, for example, "a1". How will I approach this problem?
The solutions I tried so far looks something like this
db.foo.aggregate([
{
$match: {
"hd.rd.rm.code": /a1/
}
},
{
$project: {
"hd.rd": {
$filter: {
input: "$hd.rd",
as: "roomData",
cond: {
$eq: [
{
$substr: [ "$$roomData.rm.code", 0, 10]
},
"a1"
]
}
}
}
}
}
])
but the query above doesn't work. I found out $regexMatch doesn't work with $cond, so I'm quite loss at the moment.
My expected results will look something like this:
{
"hd": {
"rd": {
"rm": {
"code": "a1asd"
}
}
}
}
