Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

306
Views
Mongodb replacing dot (.) in key name while inserting document

MongoDb doesn't support keys with dot. I have a Ruby nested hash that has many keys with dot (.) character. Is there a configuration that can be used to specify a character replacement for . like an underscore _ while inserting such data to MongoDb

I'm using MongoDB with Ruby & mongo gem.

example hash is like below

{
  "key.1" => {
             "second.key" => {
                             "third.key" => "val"
                           }
             }
}
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

If it isn't possible to use keys with . in Mongodb, you'll have to modify the input data :

hash = {
  'key.1' => {
    'second.key' => {
      'third.key' => 'val.1',
      'fourth.key' => ['val.1', 'val.2']
    }
  }
}

Transforming string keys

This recursive method transforms the keys of a nested Hash :

def nested_gsub(object, pattern = '.', replace = '_')
  if object.is_a? Hash
    object.map do |k, v|
      [k.to_s.gsub(pattern, replace), nested_gsub(v, pattern, replace)]
    end.to_h
  else
    object
  end
end

nested_gsub(hash) returns :

{
    "key_1" => {
        "second_key" => {
             "third_key" => "val.1",
            "fourth_key" => [
                "val.1",
                "val.2"
            ]
        }
    }
}

Transforming keys and values

It's possible to add more cases to the previous method :

def nested_gsub(object, pattern = '.', replace = '_')
  case object
  when Hash
    object.map do |k, v|
      [k.to_s.gsub(pattern, replace), nested_gsub(v, pattern, replace)]
    end.to_h
  when Array
    object.map { |v| nested_gsub(v, pattern, replace) }
  when String
    object.gsub(pattern, replace)
  else
    object
  end
end

nested_gsub will now iterate on string values and arrays :

{
    "key_1" => {
        "second_key" => {
             "third_key" => "val_1",
            "fourth_key" => [
                "val_1",
                "val_2"
            ]
        }
    }
}
over 4 years ago · Santiago Trujillo Report

0

In mongoDB, there is no configuration to support dot in the key. You need to preprocess the JSON before inserting to MongoDB collection.

One approach is that you can replace the dot with its unicode equivalent U+FF0E before insertion.

Hope this helps.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!