Python MongoDB 不允许使用 '.' 关键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28664383/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 03:34:46  来源:igfitidea点击:

MongoDB not allowing using '.' in key

pythonmongodb

提问by eleanor

I'm trying to save a dictionary containing a special character '.' in key portion to the MongoDB. The error is presented below, which clearly states that the key must not contain a special character '.'.

我正在尝试保存包含特殊字符 '.' 的字典。在 MongoDB 的关键部分。错误如下所示,其中明确指出密钥不得包含特殊字符“.”。

>>> import pymongo
>>> client = pymongo.MongoClient('localhost')
>>> db = client['malware']                                                                                                                                                          
>>> test = db['test']
>>> d = {'.aaa' : '.bbb'}                                                                                                                                                           
>>> test.insert(d)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python2.7/site-packages/pymongo/collection.py", line 362, in insert
    self.database.connection)
bson.errors.InvalidDocument: key '.aaa' must not contain '.'

But my current information contains '.' in the key portion of the data, which I need to store to MongoDB. Currently I was just deleting the '.' from the string, another options would be to replace it with '_' or some other special character.

但我当前的信息包含'.' 在数据的关键部分,我需要将其存储到 MongoDB。目前我只是删除了“。” 从字符串中,另一个选项是用“_”或其他一些特殊字符替换它。

Nevertheless, all results in loss of information, because if I have a key '.aaa' and a key 'aaa' and if I convert '.' into '' then the keys are exactly the same and I lose some information. Why isn't Mongo allowing me to save the '.aaa' into the DB?

尽管如此,所有这些都会导致信息丢失,因为如果我有一个密钥 '.aaa' 和一个密钥 ' aaa' 并且我转换了 '.' 进入 '' 然后键完全相同,我丢失了一些信息。为什么 Mongo 不允许我将“.aaa”保存到数据库中?

Any ideas how to approach the problem?

任何想法如何解决这个问题?

采纳答案by Padraic Cunningham

You can set check_keysto False according to the source:

您可以check_keys根据来源设置为 False :

 test.insert(d,check_keys=False)


 def insert(self, doc_or_docs, manipulate=True,
           safe=None, check_keys=True, continue_on_error=False, **kwargs):

It does indeed work:

它确实有效:

In [28]: d = {'.aaa' : '.bbb'}

In [29]: test.insert(d,check_keys=False)
Out[29]: ObjectId('54ea604bf9664e211e8ed4e6')

The docstring states:

文档字符串指出:

  • check_keys(optional): If Truecheck if keys start with '$' or contain '.', raising :class:~pymongo.errors.InvalidNamein either case.
  • check_keys(可选):如果True检查键是否以 '$' 开头或包含 '.',则~pymongo.errors.InvalidName在任何一种情况下都提高 :class: 。

You seem to be able to use any character apart from just the two $or .so a leading underscore or any other character would be fine and probably a better option.

你似乎可以分开使用任何字符只从两个 $.因此导致下划线或任何其他字符会被罚款和可能是一个更好的选择。

There is info in the faq about escaping :

常见问题解答中有关于转义的信息

In some cases, you may wish to build a BSON object with a user-provided key. In these situations, keys will need to substitute the reserved $ and . characters. Any character is sufficient, but consider using the Unicode full width equivalents: U+FF04 (i.e. “$”) and U+FF0E (i.e. “.”).

在某些情况下,您可能希望使用用户提供的密钥构建 BSON 对象。在这些情况下,键需要替换保留的 $ 和 。人物。任何字符都可以,但请考虑使用 Unicode 全角等价物:U+FF04(即“$”)和 U+FF0E(即“.”)。

And the dot-notation faq explains why using .is not a good idea:

点符号 faq 解释了为什么使用.不是一个好主意:

MongoDB uses the dot notation to access the elements of an array and to access the fields of an embedded document. To access an element of an array by the zero-based index position, concatenate the array name with the dot (.) and zero-based index position, and enclose in quotes:

MongoDB 使用点符号来访问数组的元素和访问嵌入文档的字段。要按从零开始的索引位置访问数组元素,请将数组名称与点 (.) 和从零开始的索引位置连接起来,并用引号引起来:

回答by Padraic Cunningham

You can't use the '.aaa' as your key because Mongo uses the dot to refer to nested documents.

您不能使用 '.aaa' 作为键,因为 Mongo 使用点来引用嵌套文档

If you want to have the key look like a dot, you can use a unicode equivalent like \u002E:

如果您想让密钥看起来像一个点,您可以使用 unicode 等价物,例如\u002E

>>> d = {'\u002Eaaa' : '\u002Ebbb'}    

However, I'd suggest your approach of just choosing another character and accepting it as a "limitation" of the platform.

但是,我建议您只选择另一个角色并将其视为平台的“限制”。