How to use dot in field name?

后端 未结 7 1155
一整个雨季
一整个雨季 2020-11-30 07:19

How to use dot in field name ?

I see error in example:

db.test2.insert({ \"a.a\" : \"b\" })

can\'t have . in field names [a.a]
相关标签:
7条回答
  • 2020-11-30 08:12

    Initially I used a simple recursion to replace all "." characters with its unicode equivalent but figured it out that even the dots in the values was getting replaced. So I thought that we should replace the dots only from keys and made the changes accordingly in case "if isinstance(input, dict)". I thought it should be a sufficient condition to do the magic but I forgot that dict value can also be a dict or a list and then I finally added that check that if value of a dict was not string then, go inside recursively and was finally able to come up with this solution which eventually did the trick.

    def remove_dots(data):
        if isinstance(data, dict):
                return {remove_dots(key): value if isinstance(value, str) else remove_dots(value) for key,value in data.iteritems()}
        elif isinstance(data, list):
                return [remove_dots(element) for element in data]
        elif isinstance(data, str):
                return data.replace('.','\u002e')
        else:                                                                                             
                return data
    
    0 讨论(0)
提交回复
热议问题