時(shí)間:2024-02-24 17:17作者:下載吧人氣:24
前言
MongoDB是面向集合存儲(chǔ)的文檔型數(shù)據(jù)庫(kù),其涉及到的基本概念與關(guān)系型數(shù)據(jù)庫(kù)比有所不同。本文主要介紹關(guān)于mongo數(shù)據(jù)集合屬性存在點(diǎn)號(hào)(.)的相關(guān)內(nèi)容,下面話不多說(shuō)了,來(lái)一起看看詳細(xì)的介紹吧
基本知識(shí)點(diǎn):
1.似乎mongo3.6之前不允許插入帶點(diǎn)(.)或美元符號(hào)($)的鍵,但是當(dāng)我使用mongoimport工具導(dǎo)入包含點(diǎn)的JSON文件時(shí),它工作正常。
2.在使用spring-data-mongodb處理mongodb的增刪改查時(shí)會(huì)通過(guò)一個(gè)MappingMongoConverter(Document和Modle轉(zhuǎn)換類(lèi))轉(zhuǎn)換數(shù)據(jù)
3.具體對(duì)點(diǎn)號(hào)的轉(zhuǎn)換在DBObjectAccessor(spring-data-mongodb-1.10.13)或者DocumentAccessor(spring-data-mongodb-2.0.9),如下:
//插入時(shí)轉(zhuǎn)換
public void put(MongoPersistentProperty prop, Object value) {
Assert.notNull(prop, “MongoPersistentProperty must not be null!”);
String fieldName = prop.getFieldName();
if (!fieldName.contains(“.”)) {
dbObject.put(fieldName, value);
return;
}
Iterator<String> parts = Arrays.asList(fieldName.split(“\.”)).iterator();
DBObject dbObject = this.dbObject;
while (parts.hasNext()) {
String part = parts.next();
if (parts.hasNext()) {
dbObject = getOrCreateNestedDbObject(part, dbObject);
} else {
dbObject.put(part, value);
}
}
}
//查詢時(shí)轉(zhuǎn)換
public Object get(MongoPersistentProperty property) {
String fieldName = property.getFieldName();
if (!fieldName.contains(“.”)) {
return this.dbObject.get(fieldName);
}
Iterator<String> parts = Arrays.asList(fieldName.split(“\.”)).iterator();
Map<String, Object> source = this.dbObject;
Object result = null;
while (source != null && parts.hasNext()) {
result = source.get(parts.next());
if (parts.hasNext()) {
source = getAsMap(result);
}
}
return result;
}
//判斷值是否為空
public boolean hasValue(MongoPersistentProperty property) {
Assert.notNull(property, “Property must not be null!”);
String fieldName = property.getFieldName();
if (!fieldName.contains(“.”)) {
return this.dbObject.containsField(fieldName);
}
String[] parts = fieldName.split(“\.”);
Map<String, Object> source = this.dbObject;
Object result = null;
for (int i = 1; i < parts.length; i++) {
result = source.get(parts[i – 1]);
source = getAsMap(result);
if (source == null) {
return false;
}
}
return source.containsKey(parts[parts.length – 1]);
}
網(wǎng)友評(píng)論