If I've understand you correctly, you can do something like this:
- Convert object
paths
into an array to can get the key value using $objectToArray
.
- Then filter by
regex
expression the key you want. In this case pet
is to match values where string has "pet" but you can use another regex if you want.
- After that, we will have only routes objects that matches the regex, then again convert to object using
$arrayToObject
db.collection.aggregate([
{"$match": {"swagger": "2.0"}},
{"$set": {"paths": {"$objectToArray": "$paths"}}},
{
"$set": {
"paths": {
"$filter": {
"input": "$paths",
"as": "p",
"cond": {"$regexMatch": {"input": "$$p.k","regex": "pet"}}
}
}
}
},
{"$set": {"paths": {"$arrayToObject": "$paths"}}}
])
Example here
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…