My Question- I have mongodb installed in ubuntu server and it has multiple databases and I want to connect to mongodb installed on server and get all the databases (name of databases, collections of every databases) and perform search operations on all the databases. Note - I did not know the names of database
2 Answers
It won't work out-of-the-box. If you include the spring-boot mongodb starter project, it will look for the property 'spring.data.mongodb.uri' to connect to a single database, and if it does not find it, it will try to connect to 'localhost:27017'. This single database will then be used for all Spring Data Repositories automatically.
You can add extra MongoClient beans for different databases, but you'll need some work to connect different Spring Data Repositories to those different beans.
And if you want to work with a dynamic set of databases, i.e. when you don't know which databases or how many, you can't work with fixed MongoClients as spring beans anyway. You'll need some sort of factory that creates multiple MongoClient beans based on the number of databases, and multiple SearchRepository beans connected to each of those MongoClient beans.
In depends on the complexity of your project, but for this usecase I would not use Spring Data at all, but stick to the MongoDB Java client API:
Map<String, MongoClient> clientsPerDatabase = new HashMap<>();
// Setup
MongoClient mongoClient = new MongoClient(/* default database */);
MongoCursor<String> dbsCursor = mongoClient.listDatabaseNames().iterator();
while(dbsCursor.hasNext()) {
String databaseName = dbsCursor.next();
if (!"admin".equals(databaseName)) {
clientsPerDatabase.put(databaseName, new MongoClient(...+databaseName);
}
}
// Query
for(MongoClient client: clientsPerDatabase.values()){
...
}

- 16,398
- 2
- 37
- 61
-
Yeah i got your point but the main issue i did not know how should i proceed – Jan 13 '21 at 09:13
-
I added some code on how to use the MongoClient directly without messing with dynamic spring beans. – GeertPt Jan 13 '21 at 09:54
-
one more thing is can i pass dynamically database uri? – Jan 13 '21 at 10:22
-
Define 'dynamically'? Is it in an environment variable, in a properties file, or do you read it from a table? The database URI is what you pass to the MongoClient, so you can read it from anywhere you want. Even with Spring Boot, you can create a custom @Bean mongoClient() method in the main Application class, and it will no longer require the 'spring.data.mongodb.uri' property. – GeertPt Jan 13 '21 at 10:28
-
MongoCollection
mongoCollection = database.getCollection(collectionName); Iterable – Jan 14 '21 at 09:47fields = mongoCollection.find(); fields.forEach(field -> { System.out.println(field.toJson()); }); -
is it possible to get all fileds which contains string "chaitanya" – Jan 14 '21 at 09:48
-
If you know which field contains the string, you can use $regex. See https://stackoverflow.com/questions/10610131/checking-if-a-field-contains-a-string – GeertPt Jan 14 '21 at 14:01
-
the main issue is i did not know about any fields i have just database uri and i want to perform a search operation in all the databases and want the fields collection and database which contains that "string" – Jan 16 '21 at 13:33
I'd configure the Spring Boot application with admin access to the database and then use native queries to retrieve info on all existing databases, schemas, etc

- 63,078
- 28
- 122
- 148
-
suppose i have to connect two different database of two different server Is this scenario is possible? If yes then please tell me how i write configurations – Jan 13 '21 at 08:19