mongodb表名(集合)长度限制和命名规则

mongodb在表名使用过程中会产生一些问题。

问题一:表名不能含有system.,这个系统会保留,出错信息如下

Traceback (most recent call last):
  File "/opt/backend-job/scripts/applock/scan.py", line 248, in insert_row
    conn.applock[k].bulk_write(v[ti*1000:(ti+1)*1000])
  File "/opt/backend-job/scripts/applock/2.6/lib/python2.6/site-packages/pymongo/collection.py", line 432, in bulk_write
    bulk_api_result = blk.execute(self.write_concern.document)
  File "/opt/backend-job/scripts/applock/2.6/lib/python2.6/site-packages/pymongo/bulk.py", line 468, in execute
    return self.execute_command(sock_info, generator, write_concern)
  File "/opt/backend-job/scripts/applock/2.6/lib/python2.6/site-packages/pymongo/bulk.py", line 300, in execute_command
    run.ops, True, self.collection.codec_options, bwc)
  File "/opt/backend-job/scripts/applock/2.6/lib/python2.6/site-packages/pymongo/message.py", line 573, in write_command
    reply = self.sock_info.write_command(request_id, msg)
  File "/opt/backend-job/scripts/applock/2.6/lib/python2.6/site-packages/pymongo/pool.py", line 284, in write_command
    helpers._check_command_response(result)
  File "/opt/backend-job/scripts/applock/2.6/lib/python2.6/site-packages/pymongo/helpers.py", line 196, in _check_command_response
    raise OperationFailure(msg % errmsg, code, response)
OperationFailure: cannot write to 'system.blue.theme'

其中system.blue.theme插入不进去,会直接报错。

在网上找的关于表名的命名规则为:

1.集合名不能为空字符串(" ")

2.不能包含\0或空字符,这个字符表示键的结尾

3.集合名不能以"system."开头,此前缀是系统本身保留的

4.集合名不能包含$字符(注:可包含 . 点号)

官方的说明为:

Collection names should begin with an underscore or a letter character, and cannot:

  • contain the $.
  • be an empty string (e.g. "").
  • contain the null character.
  • begin with the system. prefix. (Reserved for internal use.)

 

问题二:表名的长度有限制

我实际测试的长度为113个字符,官方的的说明链接为:https://docs.mongodb.com/manual/reference/limits/,The maximum length of the collection namespace, which includes the database name, the dot (.) separa。出错信息如下:

Traceback (most recent call last):
  File "/opt/backend-job/scripts/applock/scan.py", line 252, in insert_row
    conn.applock[k].create_index("locked")
  File "/opt/backend-job/scripts/applock/2.6/lib/python2.6/site-packages/pymongo/collection.py", line 1380, in create_index
    self.__create_index(keys, kwargs)
  File "/opt/backend-job/scripts/applock/2.6/lib/python2.6/site-packages/pymongo/collection.py", line 1290, in __create_index
    sock_info, cmd, read_preference=ReadPreference.PRIMARY)
  File "/opt/backend-job/scripts/applock/2.6/lib/python2.6/site-packages/pymongo/collection.py", line 205, in _command
    read_concern=read_concern)
  File "/opt/backend-job/scripts/applock/2.6/lib/python2.6/site-packages/pymongo/pool.py", line 213, in command
    read_concern)
  File "/opt/backend-job/scripts/applock/2.6/lib/python2.6/site-packages/pymongo/network.py", line 99, in command
    helpers._check_command_response(response_doc, None, allowable_errors)
  File "/opt/backend-job/scripts/applock/2.6/lib/python2.6/site-packages/pymongo/helpers.py", line 196, in _check_command_response
    raise OperationFailure(msg % errmsg, code, response)
OperationFailure: namespace name generated from index name "applock.com.goldwallpaper.goldpictures.money.goldlight.goldblack.pattern.luxury.metallic.background.images.art.free.hd.$locked_1" is too long (127 byte max)

 

标签: none

仅有一条评论

  1. 111111 111111

    1222222

添加新评论