I'm trying to index a variety of JSON objects in Mongo. Once in a while, the JSON object has a key somewhere that contains a .
in it, which makes Mongo complain:
MongoError: The dotted field 'foo (e.g. bar)' in 'key.0.prop foo (e.g. bar)' is not valid for storage.
Leaving aside that this data shouldn't have this kind of key, I want to temporarily deal with the problem by adjusting the keys with dots in them by removing the text containing the dot. However, these keys can be anywhere in the object and this requires a simultaneous traversal and key modification. For example,
{
"foo": {
"foo (e.g. bar)": "baz"
},
"a": "b"
}
would become
{
"foo": {
"foo": "baz"
},
"a": "b"
}
based on some function transform(badKey)
that I would specify.
What's the easiest/most robust way to iterate through a Javascript object and modify keys based on some criterion? Note that some of the keys may be nested several levels deep and so a straightforward key-value iteration would not work here. Pointers to a library that provides this functionality would be awesome.