mongo 中文字元的查詢與插入

2021-09-08 16:31:05 字數 1630 閱讀 5085

一、中文字元的查詢

在windows的cmd命令中,查詢mongo資料時,中文會出現亂碼:

c:\>mongo 127.0.0.1:2222/test

mongodb shell version: 1.8.3

connecting to: 127.0.0.1:2222/test

neu:primary> show collections

system.indexes t1

yctshard

neu:primary> db.t1.find()

neu:primary> 

這是因為mongo中的字元以utf-8格式儲存,而cmd預設為gbk(936)格式。可以將你當前的cmd視窗改為utf8()

1. 設定當前cmd視窗為utf8格式:

neu:primary> ^z

byec:\>chcp 65001 

在新開啟的cmd視窗標題上點右鍵 - 「屬性」  - 「字型」,將字型改為非「點陣字型」,然後再重新登入並查詢,你就能看到正常的中文了:

c:\>mongo 127.0.0.1:2222/test

mongodb shell version: 1.8.3

connecting to: 127.0.0.1:2222/test

neu:primary> db.t1.find()

二、中文字元的插入

當在cmd中批量插入記錄時,若有中文,則同樣會提示錯誤:

c:\>mongo 127.0.0.1:2222/test

mongodb shell version: 1.8.3

connecting to: 127.0.0.1:2222/test

neu:primary> db.t3.find()

neu:primary> 

neu:primary> db.t3.insert(name:"田")

fri nov 02 15:45:26 malformed utf-8 character sequence at offset 19

此時,可以通過mongo的--shell引數執行該插入語句。 

neu:primary> ^z

byec:\>echo db.t3.insert() > yct.txt

c:\>cat yct.txt

db.t3.insert()

將yct.txt轉成utf8格式的文字後,通過mongo的shell引數執行該檔案即可:

c:\>mongo 127.0.0.1:2222/test --shell c:\yct.txt

mongodb shell version: 1.8.3

connecting to: 127.0.0.1:2222/test

type "help" for help

neu:primary> db.t3.find()

neu:primary>

這裡的中文顯示亂碼,可以通過剛才介紹的方法解決。

另:若有多條記錄,在文字中使用回車換行符分隔即可:

c:\>cat yct.txt

db.t3.insert() 

db.t3.insert() 

db.t3.insert() 

MySQL不能插入中文字元及中文字元亂碼問題

mysql的預設編碼是latin1,不支援中文,要支援中午需要把資料庫的預設編碼修改為gbk或者utf8。在安裝後mysql之後,它的配置檔案不是很給力,不知道你們的是不是,反正我的是!開始插入中文字元的時候出現如下錯誤 error 1366 hy000 incorrect string value...

MySQL不能插入中文字元及中文字元亂碼問題

mysql的預設編碼是latin1,不支援中文,要支援中午需要把資料庫的預設編碼修改為gbk或者utf8。在安裝後mysql之後,它的配置檔案不是很給力,不知道你們的是不是,反正我的是!開始插入中文字元的時候出現如下錯誤 error 1366 hy000 incorrect string value...

python查詢中文字元

filename seek.py import unicodedata import sys import os class seek 功能 查詢中文,並替換成指定字元或字串 使用方法 python指令碼用法 引數說明 d 檔案目錄 絕對或相對路徑 預設為指令碼所在目錄 t 檔案型別 檔名字尾,如....