当前位置: 移动技术网 > IT编程>数据库>MongoDB > mongo数据集合属性中存在点号(.)

mongo数据集合属性中存在点号(.)

2018年10月09日  | 移动技术网IT编程  | 我要评论

基本知识点:

1.似乎mongo3.6之前不允许插入带点(.)或美元符号($)的键,但是当我使用mongoimport工具导入包含点的json文件时,它工作正常。

2.在使用spring-data-mongodb处理mongodb的增删改查时会通过一个mappingmongoconverter(document和modle转换类)转换数据

3.具体对点号的转换在dbobjectaccessor(spring-data-mongodb-1.10.13)或者documentaccessor(spring-data-mongodb-2.0.9),如下:

//插入时转换
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);
        }
    }
}

//查询时转换
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]);
}

 

4.点号在mongodb中有子集合的含义

例如查询a.b属性:查询的是集合中a对应子集合中的属性b的值,并不是查询集合中a.b的属性  

问题描述:文档在数据库中的样子:

{
    "_id": objectid("5bae00765500af6307755111"),
    "name": "java",
    "age": 26,
    "a.b": "nnnn"
}

 

因此在model中使用@field("a.b")查询不出集合中的"a.b"的值
@field("a.b")
@jsonfield(serialzefeatures = serializerfeature.disablecircularreferencedetect)
private integer ab;  

5.解决方法:

查阅多方资料有以下几点体会:点号在mongodb中可以插入应该开始于3.6版本,官方文档虽然说可以支持点号,但是第三方驱动、spring-data-mongodb并没有支持,但是因为一开始项目已经使用了spring-data-mongodb难以替换,所以就想到覆盖转换方法。

怎么覆盖spring-data-mongodb包中的文件?

新建一个和dbobjectaccessor转换文件一样的目录,重新建dbobjectaccessor类复制代码自定义修改,编译之后或优先使用新建的类。

//查询时转换
public object get(mongopersistentproperty property) {
    string fieldname = property.getfieldname();
    return this.dbobject.get(fieldname);
}

//判断值是否为空
public boolean hasvalue(mongopersistentproperty property) {
    assert.notnull(property, "property must not be null!");
    string fieldname = property.getfieldname();
    return this.dbobject.containsfield(fieldname);
}  

 

 注意:尽量不要修改put方法,应为低版本的mongodb本不支持点号,插入会报错 

 当然最好不要发生属性中有点号的情况。

 

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网