Last active
December 29, 2015 17:49
-
-
Save andrius/7706363 to your computer and use it in GitHub Desktop.
Fixes 'not Ok for value' mongo DB error, when key contains '.' (DOT) character
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def mongodb_keys(from,to,data) | |
| if data.kind_of? Hash | |
| result = {} | |
| data.each_pair do |k,v| | |
| result[k.to_s.gsub(from, to)] = mongodb_keys(from, to, v) | |
| end | |
| return result | |
| elsif data.kind_of? Array | |
| result = [] | |
| data.each do |v| | |
| result.push mongodb_keys(from, to, v) | |
| end | |
| return result | |
| else | |
| return data | |
| end | |
| end | |
| s = 'JSON code here...' | |
| d = mongodb_keys('.', '_DOT_', JSON.parse(s)); nil | |
| c = mongodb_keys('_DOT_', '.', d); nil |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment