今天服務(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下: / . " $
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.