Spring Data MongoDB Repository with dynamic collection name on every request - mongodb

We are fecthing the data from Mongo db usinf spring data reactive repository.
The collection name is dynamic which comes in the request.
So we need to set it on every request and fetch the data from that collection.
#Document(collection = "<fetched from request>")
public class SomeModelBean {
...
For every request collection name is different retrieved from the request.
Is there a way to achieve this.

Related

Change document name at runtime in Spring Boot Mongo DB

I am fetching data from api and storing it in database ,I have to delete the whole document and refresh data from api and store it again ,on live server doing this will cause delay for users to get back the data again. Is there any possibility to change the
#Document(collection = "events")
#JsonIgnoreProperties(ignoreUnknown=true)
collection name at runtime ?? .I have gone through following link -Changing Table name dynamiclly in JPA/Hibernate for JPA but that doesnt solve my query any help would be really appreciated.
Instead of specifying the collection name in the Document annotation I would suggest using MongoTemplate#insert(Object document, String collectionName) similarly to this example from the Spring Data MongoDB Documentation.
This way you could specific collection name at runtime using a property or environment variable.

Modified Count when updating a entity with MongoRepository

I am using Spring Data MongoDB Repository to connect to my mongo database.
I want to update a document in a collection by passing the criteria and specific fields to update.
I do that by setting the entity object directly with the fields to update along with _id field which would be used as criteria.
This is the code I am using,
Employee updatedEmployee=employeeRepository.save(employeeToUpdate);
When I used the save method, I see I do not get any return status , whether the update was successful or not.
I do not want to make another query to mongodb to fetch the document and compare it with my updated document to validate the changes?
Is there a way with repository or should I use MongoTemplate for this specific usecase alone.

How to implement multi-tenancy in mongo db using morphia

Multi-tenancy on the database level: One way to separate data for multiple clients is to have individual databases per tenant.
Suppose you are working on webservice or web application.
A client will send tenant id as header parameter.
Write one filter and get the tenant details from the Cache by tenant id.
Set the schema name in the filter
for example:
MDC.put(Constants.MONGO_TENANT_DB, "uat");
MongoMultiTenancyInterceptor will get called before any DB operation/orm call.
String tenantDBName = MDC.get(Constants.MONGO_TENANT_DB);
LOG.info("Switching to database: "+tenantDBName);
if(StringUtils.isNotBlank(tenantDBName)){
MultitenantDatastoreFactory.setDatabaseNameForCurrentThread(tenantDBName);
}
Setting the Database name for current Thread.
MultitenantDatastoreFactory : Create datastore instance per tenant schema and store into HashMap.
MultitenantDatastoreFactory.getDS(): Returns the datastore from the Hashmap by schema name
UserRepository: It will get Datastore object from the factory method and perform any DB operation.
I have uploaded my project in GITHUB, you can download the code from GITHUB
https://github.com/vikashnitk50/spring-morphia-db-poc

Spring Data partial upsert not persisiting type information

I am using Spring Data with MongoDB to store very dynamic config data in a toolkit. These Config objects consist of a few organizational fields, along with a data field of type Object. On some instances of Config, the data object refers to a more deeply nested subdocument (such as "data.foo.bar" within the database. – this field name is set by getDataField() below). These Config objects are manipulated as they're sent to the database, so the storage code looks something like this:
MongoTemplate template; // This is autowired into the class.
Query query; // This is the same query which (successfully) finds the object.
Config myConfig; // The config to create or update in Mongo
Update update = new Update()
.set(getDataField(), myConfig.getData())
.set(UPDATE_TIME_FIELD, new Date())
.setOnInsert(CREATE_TIME_FIELD, new Date())
.setOnInsert(NAME_FIELD, myConfig.getName());
template.upsert(query, update, Config.class);
Spring recursively converts the data object into a DBObject correctly, but neither the data document nor any of its subdocuments have "_class" fields in the database. Consequentially, they do not deserialize correctly.
These issues seem quite similar to those previously reported in DATAMONGO-392 , DATAMONGO-407, and DATAMONGO-724. Those, however, have all been fixed. (I am using spring-data-mongodb 1.4.2.RELEASE)
Am I doing something incorrectly? Is there a possibility that this is a Spring issue?
Came across a similar issue. One solution is to write your own Converter for Config.class.

spring-data-mongodb. How can i dynamically create a database in mongo using spring-data-mongodb library?

spring-data-mongodb. How can i dynamically create a database in mongo using spring-data-mongodb library?
I am trying to use Spring-Mongodb-Data module for CRUD operations against Mongo database and going through examples and articles my assumption is that databasename should be pre-defined in spring context xml when defining MongoTemplate bean.
In my case I have an multi-tenant application that will accept requests over http and my application should create the mongodatabase on-the-fly and use the name provided in the input http request to create the database and then load the data into collection in the newly created database.
I am trying to figure out if there is a way to dynamically populate the databasename in MongoTemplate or MongoRepository without having to provide it in spring context.xml?
Please help me.
Thanks
-RK
Have you tried the following instead of going through the pre-defined spring context configuration.
MongoTemplate getMongoTemplate(Mongo mongo, String database) {
return new MongoTemplate(mongo, database);
}