MongoDB不允许使用“."在关键 [英] MongoDB not allowing using '.' in key

查看:84
本文介绍了MongoDB不允许使用“."在关键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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.

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

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?

有什么办法解决这个问题吗?

Any ideas how to approach the problem?

推荐答案

您可以根据它确实有效:

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

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

文档字符串状态:

  • check_keys(可选):如果为True,则检查键是否以"$"开头 或包含'.',无论哪种情况都会引发:class:~pymongo.errors.InvalidName.
  • check_keys (optional): If True check if keys start with '$' or contain '.', raising :class:~pymongo.errors.InvalidName in either case.

您似乎可以使用除两个$.以外的任何字符,因此前划线或其他任何字符都可以,并且可能是一个更好的选择.

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 :

在某些情况下,您可能希望使用用户提供的密钥来构建BSON对象.在这种情况下,密钥将需要替换保留的$和.人物.任何字符都足够,但是请考虑使用Unicode全角等效项:U + FF04(即"$")和U + FF0E(即.").

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. ".").

点号常见问题解释了为什么使用.并不是一个好主意:

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

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

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不允许使用“."在关键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆