踩到的 Mongodb key的坑

今天服務(wù)器重啟了,發(fā)現(xiàn)用戶數(shù)據(jù)不能存檔。查找后發(fā)現(xiàn)是由于用戶數(shù)據(jù)里存在了這種類型的 map:{"a.b.c": 1 } ,在存檔進(jìn)mongodb時(shí),由于服務(wù)器進(jìn)行了一些處理,導(dǎo)致存檔失敗。
正常情況下這種類型的key 是可以存儲進(jìn)數(shù)據(jù)庫的,但是會發(fā)生事與愿違的事情:

> db.test.findOne()
{ "_id" : ObjectId("528090797f6408479a607d61"), "hi" : "world" }
>
>
> db.test.insert({ "a.b.c": 1 }) 
2015-12-31T10:28:53.098+0800 E QUERY    Error: can't have . in field names [a.b.c]
    at Error (<anonymous>)
    at DBCollection._validateForStorage (src/mongo/shell/collection.js:157:19)
    at insert (src/mongo/shell/bulk_api.js:646:20)
    at DBCollection.insert (src/mongo/shell/collection.js:243:18)
    at (shell):1:9 at src/mongo/shell/collection.js:157
>
>
> db.test.update({"hi":"world" },{$set:{ "a.b.c" : 1 }})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.test.findOne()
{
        "_id" : ObjectId("528090797f6408479a607d61"),
        "hi" : "world",
        "a" : {
                "b" : {
                        "c" : 1
                }
        }
}
>
>

可以看到這里insert根本就出錯(cuò),而update被解析錯(cuò)了。
特意查了下MongoDB的文檔,發(fā)現(xiàn)在MongoDB的key中不能使用的字符包括:

Windows下:/ . " $ * < > : | ?
Linux下:  / . " $

MongoDB文檔

For MongoDB deployments running on Windows, MongoDB will not permit database names that include any of the following characters:
/. "$*<>:|?
Also, database names cannot contain the null character.
Restrictions on Database Names for Unix and Linux Systems
For MongoDB deployments running on Unix and Linux systems, MongoDB will not permit database names that include any of the following characters:
/. "$
Also, database names cannot contain the null character.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 11,170評論 0 23
  • PLEASE READ THE FOLLOWING APPLE DEVELOPER PROGRAM LICENSE...
    念念不忘的閱讀 13,665評論 5 6
  • NAME dnsmasq - A lightweight DHCP and caching DNS server....
    ximitc閱讀 2,995評論 0 0
  • 回宿舍發(fā)現(xiàn)側(cè)門被鎖了,改走正門,在小道上前面,有一個(gè)阿姨推著自行車,車上綁著些泡沫,泡沫從車上掉了下來,阿姨停住車...
    陳耿坤閱讀 229評論 2 1
  • 一、覺察日記 【事實(shí)】兩天的學(xué)習(xí)過程,不斷的總結(jié)反思 【感覺】收獲滿滿,很豐盛 【意圖】不斷的學(xué)習(xí)、成長、提升,達(dá)...
    以詩為名閱讀 207評論 0 0

友情鏈接更多精彩內(nèi)容