在開發SpringBoot程式的過程中,涉及到對mongodb的操作,遇到一些問題,記錄一下。
1、mongodb的操作:insert、update 等,不需要建立collection,直接操作即可。若無,會自動建立
2、cannot write to 'test.algorithm.po.system.conf’
操作過程中的問題,在spring boot代碼中一直報錯如下:
com.mongodb.MongoCommandException: Command failed with error 2 (BadValue): 'cannot write to 'test.algorithm.po.system.conf'' on server 10.XXX.XX.XX:XXX. The full response is { "ok" : 0.0, "errmsg" : "cannot write to 'test.algorithm.po.system.conf'", "code" : 2, "codeName" : "BadValue" }
在操作collection的時候一直報錯:{ “ok” : 0.0, “errmsg” : “cannot write to ‘test.algorithm.po.system.conf’”, “code” : 2, “codeName” : “BadValue” },排查了一遍spring boot代碼,無果。
在主機上登入mongodb,手動建立該collection,還是不行,同樣的報錯。
但手動建立了一個名為test的collection,成功了。
于是思考,應該是名字問題,經過資料查詢
集合設計限制如下: 1、UTF-8 字元 2、不能是空字元串(“”) 3、不能包含 \0 字元(空字元),這個字元辨別集合名的結束 4、不能以 “system.” 開頭,這是為系統保留的字首 5、不在集合中包含字元 “$” 6、使用 “.” 來分隔不同命名空間的子集合,如一個部落格可能包含兩個子集合,blog.posts和blog.authors,而blog本身可以不存在 |
我的命名沒有符合不能以 “system.” 開頭,這是為系統保留的字首。
因為dbname.system.*存放了資料資訊的中繼資料
在MongoDB資料庫中名字空間 .system.* 是包含多種系統資訊的特殊集合(Collection),如下:
集合命名空間 | 描述 |
---|---|
dbname.system.namespaces | 列出所有名字空間。 |
dbname.system.indexes | 列出所有索引。 |
dbname.system.profile | 包含資料庫概要(profile)資訊。 |
dbname.system.users | 列出所有可通路資料庫的使用者。 |
dbname.local.sources | 包含複制對端(slave)的伺服器資訊和狀态。 |
對于修改系統集合中的對象有如下限制。
在{{system.indexes}}插入資料,可以建立索引。但除此之外該表資訊是不可變的(特殊的drop index指令将自動更新相關資訊)。
{{system.users}}是可修改的。 {{system.profile}}是可删除的。
息)。
改完名字後,成功!!!