Default Mongodb settings for mongodb connection in spring-data-mongodb - mongodb

I'm using Spring Boot and am trying to figure out how I can see a list of all the default properties for the MongoDB connection.
I looked at AbstractMongoClientConfiguration but it's abstract and I can't see where the defaults come from. Looking at the class hierarchy for this class I can't see any default implementation in the Spring libs I have.
Where are the defaults for this connection? I don't see any properties files either, but might be missing them.

Had the same issue not too long ago. The official spring-data-mongodb documentation doesn't really mention any details about connection properties and their default values.
However, you can find more detailed information about the connection parameters in the MongoDB Java Driver Documentation and even more detailed in the Connection string docs including some default values.
Spring Data MongoDB uses default values generated by calling the builder method for MongoClientSettings.

Related

Spring Boot + JDBC (not JPA) + Postgres Getting Started?

Try as I might, I cannot seem to find a simple example of a SpringBoot application that uses Spring Data JDBC with a Postgres database, or how to generate Entity classes from a database, or vice versa if that's required, or even how to get a reference to a Data Source.
There are lots of examples using JPA.
There are a few examples spinning up an H2/HSQL on the fly.
There are a couple using Postgres with Spring but not Spring Boot, which means these examples have a number of extra steps.
I believe I know what dependencies are needed -- basically Postgres and a Spring Data JDBC starter, both available in start.spring.io - and as far as data source properties, the example in this this link seems like it might work ...
spring.datasource.url=jdbc:postgresql://localhost:5432/shopme
spring.datasource.username=postgres
spring.datasource.password=password
But I cannot find how to declare a Repository class, or how to instantiate or get a reference to said Repository. If the docs are meant to explain this, I am afraid their virtues are lost on me. From the examples they link to, it looks like perhaps I can create a repository like this ...
interface CategoryRepository extends CrudRepository<Category, Long>, WithInsert<Category> {}
... and then get a reference to an implementation of my repository like this ...
#Autowired CategoryRepository repository;
... and I guess that will use my Postgres info from application.properties somehow.
None of that addresses Table schema => POJO generation (or vice versa). But even if I'm right about the above, this is my persistence layer. I'm not comfortable copy/pasting from some sample code, getting a good result (for now), and declaring it done. I'd rather be working from real information.
If I'm starting with valid Postgres connection info and I know what I want my Entities to look like ...
How do I capture my Postgres connection info in properties? (I suspect my properties example above is correct but that's just copy/paste from some link)
How do I write tables and then generate Entity classes, or the reverse? I prefer the former but I'll settle for either at this point.
How do I get a reference to a DataSource to my Postgres database? (I might never need one but I'd like to know how in case I do)
How do I define a repository class? (I believe I extend CrudRepository<AggRoot, IdType> if I'm happy with CrudRepo, but I'm hazy on this)
How do I instantiate my repo class with my postgres info / DataSource?
How do I get a reference to this repo?
I'm sure a lot of this would be easier if I was stronger with basic Spring, but I am learning that as I go.
Thanks so much!
Bean
I have pieced together some working code from various source and copy/pastes. It does not feel like an answer, so much as it feels like code that happens to work, for now, and I'm open to any suggestions, corrections, or improvements.
How do I capture my Postgres connection info in properties?
This appears to be covered in the Spring Boot docs, not Spring Data etc. There's quite a gotcha around property names that's easy to overlook, which has to do with a Spring Data default connection pool change (Tomcat to Hikari), which requires a subtle property name change: x.y.url= changes to x.y.jdbc-url=. So my properties look like this:
app.datasource.jdbc-url=jdbc:postgresql://localhost:5432/mydb
app.datasource.username=admin
app.datasource.password=admin
How do I write tables and then generate Entity classes, or the reverse? I prefer the former but I'll settle for either at this point.
From what I can tell, in Spring Data JDBC you cannot do either. All I am going off of is something I read in a blog post from 2019 ... I'd love something definitive one way or the other.
How do I get a reference to a DataSource to my Postgres database? (I might never need one but I'd like to know how in case I do)
Using the subtly-documented DataSourceBuilder seems to be the way to go. Even more subtly documented is the need to annotate your DataSourceBuilder with the prefix you're using for your connection string application properties. Easiest is to declare the DataSourceBuilder method in your Application class. The good news is the declaration itself is very simple.
#Bean
#ConfigurationProperties(prefix = "app.datasource")
public DataSource dataSource ()
{
return DataSourceBuilder.create().build();
}
How do I define a repository class? (I believe I extend CrudRepository<AggRoot, IdType> if I'm happy with CrudRepo, but I'm hazy on this)
Indeed, CrudRepository is the way to go. A lot of the examples I found were misleadingly complex: they add annotations because they are doing non-default stuff, but if you just want CRUD, this is all you need:
#Repository // I'm unsure if this is needed - some examples had it, some didn't
public interface MyAggRootRepository extends CrudRepository<MyAggRoot, Long>
{
}
How do I instantiate my repo class with my postgres info / DataSource?
How do I get a reference to this repo?
With a properly coded DataSourceBuilder as above, all you need is to declare an #Autowired field for your repo and you're done.
#Autowired
MyAggRootRepository _repo
That appears to be everything. Once you know the steps there's not much to it:
a few lines in application.properties
a pretty trivial interface extending CrudRepository(T, PK)
a boilerplate DataSource-returning method using DataSourceBuilder (possibly with care taken to get the prefix right on the properties)
a simple #Autowired repository field
The lack of table or Entity class generation means a bit more work, but it's one less thing to know, and one less source of surprises I have to deal with so I don't mind the tradeoff.
If anyone can correct the above, or point to a definitive reference rather than hazy memory of blog posts, feel free.
I just uploaded a basic example of using Spring Data JPA here on my Github (Sorry, that's a lot of line on the application.properties, just ignore if unnecessary)
When you using spring-boot-starter-data-jpa dependency. It will setup anything related to database for you. You don't need to put any boilerplate code.
You can use annotation #Repository or not, it depends on your code structure and requirement. For me, I always use annotation.
If you're using eclipse, you can use a 'Generate Entities from Tables' wizard. Specify a connector or driver, fill out database creadential and you are ready to go.

Maps/Collections in MongoDB Java driver

In the C# mongodb driver, there are 3 possible representations for Dictionaries:
Document, ArrayOfArrays, ArrayOfDocuments.
https://mongodb.github.io/mongo-csharp-driver/2.8/reference/bson/mapping/#dictionary-serialization-options
As far as I understand, the Java driver supports (only or by default) the "Document" representation.
Is there a Convention or other built-in way to configure the driver to use ArrayOfArrays?
I was not able to see anything related in the MongoDB Java Driver documentation.
According to the Java driver team, the answer is that while there's isn't a simple flag:
You can do it with a custom codec to handle Map conversions to a nested key, value array for all Maps.
Alternatively, you could create a custom annotation could be used to set the codec for a single Class field in the POJO. That way you would not have to worry about all Maps being treated the same by the codec registry.
If you want to store all maps in the same way, the first option is obviously easier. You can refer to the driver code to see how the built-in annotations are built.

What is the correct way to get all databases from MongoDB through Java Driver

I want to use MongDB Java driver to connect to Mongo instance. I am able to create a MongoClient instance and now I want to get all the database names. I see that there is a method getDatabaseNames which meet my requirement but it is deprecated. There is another method getDatabase(databasename) but I have to know the database name before calling this method. Is there a way for me to get all database names?
The javadocs for deprecated method has the name of the replacement method.
http://api.mongodb.com/java/3.0/com/mongodb/Mongo.html#getDatabaseNames--
Replaced with MongoClient.listDatabaseNames()

How do I extend MyBatis to support a new database?

I need to port MyBATIS to a new type of database (a custom one)...I guess I need to write a SqlSessionFactory that is relevant to my database?
Can you please refer me to the documentation on how I can extend it?
Don't think you have to do anuthing fancy. Having a server pool or just using your own driver would be enough. Here is the document I've used for my needs http://mybatis.github.io/mybatis-3/

How to read the schema used by a JPA implementation

My EntityManager is using a persistence unit that uses a data source provided by our Websphere configuration. The DS configuration includes an environment specific DB to use.
The EM successfully uses this schema, but I can't figure out a way to log or display the schema being used. I was thing something like em.getCurrentSchema would be available..
Any help would be great, thanks.
No API to do this (in JPA). You could do it via JDBC and use of DatabaseMetaData.
JPA is to provide an object view of the data and ease persistence of those objects, not to just present datastore specifics to the user.