java mongodb keyf_踩到的 Mongodb key的坑

今天服务器重启了,发现用户数据不能存档。查找后发现是由于用户数据里存在了这种类型的 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 ()

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值