Announcement Announcement Module
Collapse
No announcement yet.
Force property order in MongoDB mapping Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Force property order in MongoDB mapping

    I have a Location object that I need to force the order of the properties when they get persisted to mongodb, but I'm not having much luck figuring out how to do it. The Location class looks something like:
    Code:
    Location {
       float lat;
       float lon;
       County county;
    }
    when it get's persisted into mongo, county is always before lat and long. This is a problem because I'm trying to put a geoindex on it and the first two properties must be lat/long.

    I have tried:
    Code:
    @XmlRootElement(name="location")
    @XmlType(propOrder={"latitude", "longitude", "county"})
    and that works when the location object is serialized to xml to my client, but not to the DB. It seems that spring data or some mongo mapper is always doing things in alphabetical order.

    Does anyone know how to force the order properties get persisted into mongo?
    Thanks!

  • #2
    Can you open a JIRA ticket here: https://jira.springsource.org/browse/DATADOC

    Thanks

    Comment


    • #3
      Wait, are you saying this should actually work? I was going to try out a custom Converter instead. It would definitely be nice if the annotation worked. I'll go ahead and add a JIRA for it.

      Comment


      • #4
        Ok, so I tried the converter but I ran into a problem with it being able to convert the nested County object. But I ran into a larger issue. Even if the converters work, the MongoTemplate constructors only allow for either setting the Converters OR setting a UserCredential object, not BOTH. Cloudfoundry requires username/password to be used when connecting to MongoDB, so I have to be able to set the UserCredentials object. So either there either needs to be setters for the UserCredentials and the Converters or a new constructor that takes both. I'll add this to the JIRA too. Right now, I'm unable to persist the object in a way that will let me use a geospatial index. Any idea when this would get fixed even in a nightly build?

        Comment


        • #5
          Well, I don't expect the annotations to work, but there should be a way to influence the order that the fields are saved in the db. Either the order they are defined in the Java class or using some annotation, maybe not an XML one. We could use the @Order annotation that already exists in Spring.

          Code:
          	@Order(1)
          	float lat;
          	@Order(2)
          	float lon;
          	County county;

          Comment


          • #6
            Just for completeness, here is the JIRA.
            https://jira.springsource.org/browse/DATADOC-172

            @trisberg are you guys going to fix the issue with being able to set Converters and UserCredentials in the MongoTemplate or should I write a separate JIRA for it?

            Comment


            • #7
              Hi,

              Regarding the constructor, we can add that. (DATADOC-174)

              However, there is an alternative constructor you can use now, namely

              MongoTemplate(MongoDbFactory mongoDbFactory, MongoConverter mongoConverter)

              FWIW, we were curious to see if people would actually request it since I was hoping that it would be a more common use case to register additional Spring Conversion Service based converters with the MappingContext vs. using an alternative.

              Mark

              Comment


              • #8
                Do you have an example of that? I voted up that JIRA too, but i'd be interested in the alternative.

                Comment


                • #9
                  You could use this:

                  Code:
                  new MongoTemplate(
                  	new SimpleMongoDbFactory(mongo, "database", new UserCredentials("username", "password")),  
                  	mongoConverter);

                  Comment

                  Working...
                  X