Results 1 to 3 of 3

Thread: Best Place to Call ensureIndex

  1. #1
    Join Date
    Mar 2011
    Posts
    7

    Smile Best Place to Call ensureIndex

    Hi There,

    Im wondering when ensureIndex should be called on the template.

    Im currently using an xml bean definition to create the monogTemplate. Is there a way to specify the index in the xml definition or is this done some other way?

    Should I use @Configuration instead and call ensureIndex after the new mongo template call?

    Thanks,
    Dave

  2. #2
    Join Date
    Apr 2010
    Posts
    9

    Default

    I've actually just started using spring-data with mongodb, so am not really an expert.

    As far as I can tell, annotating your entity with @Document and then annotating the field you want indexed with @Indexed and then using the <mongo:mapping-converter> xml config tells spring-data to automatically index the field. I'm not sure if this is the right way, but it seems to work for me.

    Btw, I'm using the latest snapshot and a weird behavior I'm seeing is that if you don't specify the collection name in the @Document or @Indexed annotation, spring-data will create two different collections if the name is camel cased. i.e. if your entity is ContactPerson, a collection with contactPerson will be created to store the data, while a contactperson collection will be created that contains the index. Workaround is to manually specify the collection name for consistency.

    Hope this helps.

  3. #3
    Join Date
    Apr 2010
    Posts
    9

    Default

    Additionally, be wary of this bug: https://jira.springsource.org/browse/DATADOC-79

    Not sure if its a spring-data bug or mongo-java-driver bug, but when the index name does not match to any field in the document, and both "unique" and "dropDups" is true, all the rows in the collection is dropped except for one. Probably because it treats all entries to be duplicated, since they do not have the index.

    I'm not sure what the expected behavior should be, but I hope losing data is not one of them.

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
  •