春季数据mongodb映射动态字段 [英] spring data mongodb mapping dynamic field

查看:194
本文介绍了春季数据mongodb映射动态字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Java类中有这个模型

I've this model in my java class

@Document
public class Template {

    private String type;

    private String code;

    @Version
    Long version;
}

我需要添加一个名为 template 的新字段,并将该字段映射为动态字段,换句话说,我将像这样对文档进行建模

I need to add a new field, named template, and map this field as dynamic in other words I would model a document like this

{
  _id: 'id' 
  type:'myType',
  code:'myCode'
  template:{
    someFiled:[
      {
        subField1:'value1',
        subField2:'value2'
      },
      {
        sub1Field1:'1value1',
        sub1Field2:'1value2'
      }
      .......................
    ],
    otherField:[
      {
        otherField1:'value1',
        otherField2:'value2'
      }
    ],
    .........
  },
  version:1000L
}

有什么方法可以将字段注释为动态字段?

There is any way to annotated a field as dynamic?

解决方案

    @Document
    public class Template {

        private String type;

        private String code;

        private Map<String, Object> template;

        @Version
        Long version;
    }

推荐答案

我找到了一个完美的解决方案.以我的项目为例:

I found out a perfect solution. take my project for example:

@Data
@Document(collection = "logs")
public class Log {
    @Id
    private String id;
    private Object data;

    // data field can be a string
    public void setData(String str) {
        data = str;
    }
    // data field can be a {}
    public void setData(JsonObject jsonObject) {
        data = new BasicDBObject(jsonObject.getMap());
    }
    // data can be a []
    public void setData(JsonArray jsonArray) {
        BasicDBList list = new BasicDBList();
        list.addAll(jsonArray.getList());
        data = list;
    }
}

data字段声明为Object的类型,为其实现3种设置器.

declare the data field as type of Object, implement 3 kind of setter for it.

这是测试用例:

@RunWith(SpringRunner.class)
@SpringBootTest
public class LogRepositoryTest {

    @Autowired
    private LogRepository logRepository;

    @Test
    public void test() {
        Log strLog = new Log();
        strLog.setData("string here");
        logRepository.save(strLog);
        Log objLog = new Log();
        objLog.setData(new JsonObject().put("key", "value").put("obj", new JsonObject()));
        logRepository.save(objLog);
        Log aryLog = new Log();
        aryLog.setData(new JsonArray().add("a").add("b").add("c"));
        logRepository.save(aryLog);
    }
}

结果:

{
        "_id" : ObjectId("5a09fa46a15b065268a0a157"),
        "_class" : "ltd.linkcon.spider.domain.Log",
        "data" : "string here"
}
{
        "_id" : ObjectId("5a09fa46a15b065268a0a158"),
        "_class" : "ltd.linkcon.spider.domain.Log",
        "data" : {
                "key" : "value",
                "obj" : [ ]
        }
}
{
        "_id" : ObjectId("5a09fa46a15b065268a0a159"),
        "_class" : "ltd.linkcon.spider.domain.Log",
        "data" : [
                "a",
                "b",
                "c"
        ]
}

这篇关于春季数据mongodb映射动态字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆