Skip to content

Instantly share code, notes, and snippets.

@andrius
Last active December 29, 2015 17:49
Show Gist options
  • Select an option

  • Save andrius/7706363 to your computer and use it in GitHub Desktop.

Select an option

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
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