今天服務器重啟了,發現用戶數據不能存檔。查找后發現是由于用戶數據里存在了這種類型的 map:{"a.b.c": 1 }
,在存檔進mongodb時,由于服務器進行了一些處理,導致存檔失敗。
正常情況下這種類型的key 是可以存儲進數據庫的,但是會發生事與愿違的事情:
> 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根本就出錯,而update被解析錯了。
特意查了下MongoDB的文檔,發現在MongoDB的key中不能使用的字符包括:
Windows下:/ . " $ * < > : | ?
Linux下: / . " $
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.