How to add `.` to sub-field using the cli-commands

help me with this problem:

I using pymongo to update my mongoDB database using this code:

updates = {"$inc":{}}
user_ip = "127.0.0.1"

updates["$inc"][f"ips.{user_ip}"] = 1

creates something like this in the mongoDB document:

  "ips": {
    "127": {
      "0": {
        "0": {
          "1": 1
        }
      }
    }
  }

but I want it to be like:

"ips": {
  "127.0.0.1": 1
}

I cannot replace the . of the ip-address to any other characters in the DB because of some compatibility issues.

but if I do this it works fine:

url_data["ips"][user_ip] = url_data["ips"].get(user_ip, 0) + 1
updates["$set"]["ips"] = url_data["ips"]

url_data is the full document and querying it and updating it whole is not efficient for me

Hi @harry_potter3,

After reviewing your question, I would say updating this nested field is anti-pattern. Typically, top-level fields with dot notation can be handled; however, for this case, it both a nested field and using dot notation, so it may not be possible.

Here’s more information on the limitations of using dot or dollar notation in the field name.

Do you mind sharing the compatibility issue?