'How to find all collections in a Mongo DB having keys matching some pattern?
E.g., we can use the following query in PostgreSQL to find all the tables with columns matching some pattern:
select table_name from information_schema.columns where column_name ilike '%your_column_name%'
as shown here.
How can we do something similar in MongoDB?
Solution 1:[1]
Not so straightforward, but anyway you can get it in this way
db.collection.aggregate([
{
$addFields: {
obj_as_arr: {
$objectToArray: "$$ROOT"
}
}
},
{
"$addFields": {
field_names: "$obj_as_arr.k",
}
},
{
"$match": {
field_names: {
$regex: "something"
}
}
},
{
"$project": {
obj_as_arr: 0,
field_names: 0
}
},
])
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | wardialer |
