spring data - Create index in correct collection -
i annotate document @index(unique = true) so:
public class adocumentwithuniqueindex { private static final long serialversionuid = 1l; @indexed(unique = true) private string iamunique; public string getiamunique() { return iamunique; } public void setiamunique(string iamunique) { this.iamunique = iamunique; } }
when saving object, specify custom collection:
mongooperations mongodb = ... mongodb.save(document, "mycollection");
as result get:
- a new document in "mycollection"
- an index in collection "adocumentwithuniqueindex"
how can create index in "mycollection" instead without having explicitly specify in annotation?
background:
- the default collection name ambiguous in our use case. cannot guarantee, there wouldn't 2 documents same name in different packages. added package name collection.
- mapping document collection dealt in infrastructure component.
- the implementation details collection name etc. shouldn't leak individual documents.
- i understand bit of "abstraction on top of abstraction" smell required since had support mongodb , windows azure blob storage. not anymore though...
this seemed standard approach hide persistence details in infrastructure component. comments on approach appreciated well.
it's kind of unusual define collection object stored , expect index annotations work. there's few options have here:
use
@document
onadocumentwithuniqueindex
, configure collection name manually. cause objects of class persisted collection of course.manually create indexes via
mongooperations.indexops()
collections you'd use. more consistent approach of manually determining collection name during persistence operations.
Comments
Post a Comment