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)

 

wordpress固定连接出现404错误

在使用wordpress过程中,为了SEO优化,让URL更易于理解,通常是需要把URL连接语义化,如本文连接为:http://ciika.com/2016/11/wordpress-postname-404/,其中wordpress-postname-404基本上就能表达出本文的主题。

在wordpress后台,如图:

wordpress

如上图设置后,一般情况下回出现404,那么是需要修改apache的配置:

1.固定链接的目录结构需要 Apache服务器的mod_rewrite模块支持,所以在Apache配置文件httpd.conf中将 LoadModule rewrite_module modules/mod_rewrite.so设置为启用,参考路径为/etc/httpd/conf.modules.d/00-base.conf

2.修改Apache的配置文件,参考路径为:/etc/httpd/conf/httpd.conf,对于站点目录下(比喻我的网站根目录为/var/www/html)的AllowOverride None的参数设置为All。当然修改完配置后,一定要重启Apache服务。

<Directory "/var/www/html">
   
    Options Indexes FollowSymLinks

   
    AllowOverride All

   
    Require all granted
</Directory>

 

GO语言AES加密示例

GO在自己的源代码里面给出了AES的加密写法https://golang.org/src/crypto/cipher/,但是不够详细,其实大部分场景是我们会指定KEY和IV,但是在大部分的使用场景都是要显示的指定这两个值,以下代码是我们在线上 运行的,当然KEY我更改过.

AES

/

CBC

/

PKCS5Padding
package main
import (
    "bytes"
    "crypto/aes"
    "crypto/cipher"
    "fmt"
    "encoding/base64"
)

func main() {
    TestCBCAES()
}

var commonIV = []byte{0x1A, 0x3B, 0x36, 0x22, 0xD6, 0xE2, 0x2E, 0xD0, 0x22, 0xFB, 0xB8, 0x75, 0xDD, 0x38, 0x22, 0x11}
var secretKey = []byte{0x12, 0x4D, 0x4A, 0x3E, 0xC2, 0x08, 0x4A, 0x21, 0x41, 0xC1, 0xD5, 0xC5, 0xA8, 0x6A, 0xEE, 0xA1}
var planText = []byte(`ciika test go aes`)

func TestCBCAES() {
    fmt.Printf("Plain Text : %s\n", planText)

    // 加密
    result, err := AESEncrypt(planText, commonIV, secretKey)
    if err != nil {
        panic(err)
    }
    //nkmnjWoKu89yXXe+tTDoZxJIe7q/RGAD2JqXDHRgPoU=
    fmt.Println(base64.StdEncoding.EncodeToString(result))
    fmt.Printf("Encrypted Hex Data : %x\n", result)

    // 解密
    origData, err := AESDecrypt(result, commonIV, secretKey)
    if err != nil {
        panic(err)
    }
    fmt.Printf("Decrypted String : %s\n", origData)
}

// 加密函数
func AESEncrypt(origData, iv []byte, key []byte) ([]byte, error) {
    block, err := aes.NewCipher(key)
    if err != nil {
        return nil, err
    }
    blockSize := block.BlockSize()
    origData = PKCS5Padding(origData, blockSize)
    blockMode := cipher.NewCBCEncrypter(block, iv)
    crypted := make([]byte, len(origData))
    blockMode.CryptBlocks(crypted, origData)
    return crypted, nil
}

// 解密函数
func AESDecrypt(crypted, iv []byte, key []byte) ([]byte, error) {
    block, err := aes.NewCipher(key)
    if err != nil {
        return nil, err
    }
    blockMode := cipher.NewCBCDecrypter(block, iv)
    origData := make([]byte, len(crypted))
    // origData := crypted
    blockMode.CryptBlocks(origData, crypted)
    origData = PKCS5UnPadding(origData)
    return origData, nil
}

// padding
func PKCS5Padding(ciphertext []byte, blockSize int) []byte {
    padding := blockSize - len(ciphertext)%blockSize
    padtext := bytes.Repeat([]byte{byte(padding)}, padding)
    return append(ciphertext, padtext...)
}

// unpadding
func PKCS5UnPadding(origData []byte) []byte {
    length := len(origData)
    // remove the last byte
    unpadding := int(origData[length-1])
    return origData[:(length - unpadding)]
}

 

运行结果为:

Plain Text : ciika test go aes 2+1yVpDBD4G+3RWACNsRTD2YLlYDT5UzEE57LsriKpI= Encrypted Hex Data : dbed725690c10f81bedd158008db114c3d982e56034f9533104e7b2ecae22a92 Decrypted String : ciika test go aes

以上代码运行的结果,在相同的KEY和IV的情况下载JAVA,PYTHON,LUA里面运行得出的结果是一致的。

实现播放器开机自动播放视频的方法

由于家里搬家后没有电视,家里的小孩没有娱乐的工具,但是小孩又只有3岁,还不太会使用电脑,只会开机和关机,所以就想如果电脑能和电视一样就好了,即开机后自动调用暴风影音或者QQ影音自动播放动画片就好了。

于是有了以下的思路:

1.开机自动调用播放器,这步是可以实现,但是无法播放视频,在播放器里面查看了所有的设置,都无法实现这个功能,只是单纯的打开播放器,然后就不动了,在网上搜索了一下,好像以前的旧版本是支持这一的功能,但是后来这个功能取消了

2.新建一个开机任务,调用指定的批处理脚本,在脚本里面打开播放器,并制定要播放的文件。这条路可以,实现代码如下:

@echo off 
::动画片的路径
set work_path=c:\m\ 
::播放器的路径
set play="C:\Program Files (x86)\Tencent\QQPlayer>QQPlayer.exe"
::循环递归遍历动画片路径下的mp4文件
for /R %work_path%  %%s in (*.mp4) do ( 
::调用播放器播放制定视频
start  %play% %%s
::跳出循环
goto out

) 
:out
::www
::set num=%random%
::set /a  num=num%%%total%+1

当然次脚本非常粗糙,无法随机播放,或者接着上次播放的文件下一个就行播放,后续我会优化这个脚本。