同一Java类的多个MongoDB集合名称 [英] Multiple MongoDB collection names for the same Java class
问题描述
我正在使用Spring的MongoRespository.我有一个类,但是有两个方法调用它,我想根据调用该方法的方法将该类存储在Mongo中.我如何通过在mongo中基于该类的两个不同的集合来区分它的用法?
现在我在dao中有两个存储库接口.
public interface PastOpportunityRepository extends MongoRepository<DMOpportunity, String>, CustomPastOpportunityRepository {}
和
public interface PredictiveOpportunityRepository extends MongoRepository<DMOpportunity, String>, CustomPredictiveOpportunityRepository {`
我想避免使用相同的代码创建两个名称不同的类.
我建议使用Springs MongoTemplate
,它具有带有集合名称的重载.
MongoTemplate.insert(Object objectToSave,String collectionName)
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.
I suggest using Springs MongoTemplate
that has overloads that take the collection name.
MongoTemplate.find(Query query, Class entityClass, String collectionName)
MongoTemplate.insert(Object objectToSave, String collectionName)
这篇关于同一Java类的多个MongoDB集合名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!