spring data mongodb mapping dynamic field

拜拜、爱过 提交于 2019-12-06 13:57:31

问题


I've this model in my java class

@Document
public class Template {

    private String type;

    private String code;

    @Version
    Long version;
}

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?

SOLUTION

    @Document
    public class Template {

        private String type;

        private String code;

        private Map<String, Object> template;

        @Version
        Long version;
    }

回答1:


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;
    }
}

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

Here are the test case:

@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);
    }
}

And the result:

{
        "_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"
        ]
}


来源:https://stackoverflow.com/questions/37034497/spring-data-mongodb-mapping-dynamic-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!