Mapping a document with partly-defined schema

前端 未结 1 571
面向向阳花
面向向阳花 2020-12-18 16:58

I\'m writing a demo app using Spring & MongoDB as a database.

My main domain class looks like:

@Document
public class Person {

    @Id
    priva         


        
相关标签:
1条回答
  • 2020-12-18 17:42

    I'm not really sure of the expected result of casting the result of

    JSON.parse(value)
    

    to DBObject, which is an interface, not a class. Try casting the result to an implementation of DBObject BasicDBObject (or BasicDBList), or a Map<String, Object> as mentioned in the comments (it is also an interface, but it does work).

    If you're working with Spring Data Rest, you will probably not need to deserialize "manually", Spring will do it for you. Check this answer for a basic example of what to do. Having data with no schema specified may not be the best idea around (mongodb saves you from doing it at the database level, but you should do it at the application level), but I use similar tricks in production, and you can somehow make it work.

    0 讨论(0)
提交回复
热议问题