Results 1 to 9 of 9

Thread: Force property order in MongoDB mapping

  1. #1
    Join Date
    Oct 2010
    Posts
    10

    Default 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. #2
    Join Date
    Aug 2004
    Posts
    1,107

    Default

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

    Thanks
    Thomas Risberg
    SpringSource by Pivotal
    http://www.springsource.org

  3. #3
    Join Date
    Oct 2010
    Posts
    10

    Default

    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.

  4. #4
    Join Date
    Oct 2010
    Posts
    10

    Default

    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?

  5. #5
    Join Date
    Aug 2004
    Posts
    1,107

    Default

    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;
    Thomas Risberg
    SpringSource by Pivotal
    http://www.springsource.org

  6. #6
    Join Date
    Oct 2010
    Posts
    10

    Default

    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?

  7. #7
    Join Date
    Aug 2004
    Location
    New York, NY
    Posts
    74

    Default

    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

  8. #8
    Join Date
    Oct 2010
    Posts
    10

    Default

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

  9. #9
    Join Date
    Aug 2004
    Posts
    1,107

    Default

    You could use this:

    Code:
    new MongoTemplate(
    	new SimpleMongoDbFactory(mongo, "database", new UserCredentials("username", "password")),  
    	mongoConverter);
    Thomas Risberg
    SpringSource by Pivotal
    http://www.springsource.org

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •