Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple MongoDB collection names for the same Java class

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.

like image 853
Zack Avatar asked Jul 28 '14 12:07

Zack


People also ask

Can a MongoDB database have multiple collections?

Yes, you can have multiple collections within a database in MongoDB.

What is @document annotation in spring boot?

@Document is an annotation provided by Spring data project. It is used to identify a domain object, which is persisted to MongoDB. So you can use it to map a Java class into a collection inside MongoDB. If you don't use Spring Data, you don't need this annotation.


1 Answers

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)

like image 110
John B Avatar answered Sep 19 '22 11:09

John B