Apr 4th, 2011, 11:09 AM
Best Place to Call ensureIndex
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?
Apr 5th, 2011, 09:08 AM
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.
Apr 5th, 2011, 10:30 AM
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