Multiple MongoDB collection names for the same Java class

白昼怎懂夜的黑 提交于 2019-12-04 01:32:38

问题


I am using Spring's MongoRespository. I have one class but it is called by two methods and I want to store that class in Mongo based on which method called it. How would I differentiate how it was used by having two different collections based on that one class in mongo?

Right now I have my have two repository interfaces in my dao.

public interface PastOpportunityRepository extends MongoRepository<DMOpportunity, String>,       CustomPastOpportunityRepository {}

and

public interface PredictiveOpportunityRepository extends MongoRepository<DMOpportunity, String>, CustomPredictiveOpportunityRepository {`

I want to avoid making two differently named classes with the same code.


回答1:


I suggest using Springs MongoTemplate that has overloads that take the collection name.

MongoTemplate

MongoTemplate.find(Query query, Class entityClass, String collectionName)

MongoTemplate.insert(Object objectToSave, String collectionName)



来源:https://stackoverflow.com/questions/24995595/multiple-mongodb-collection-names-for-the-same-java-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!