Usage of mongodb in spring boot project

preface

As we all know, MySQL database is easy to use, but the amount of data is more than ten million. It is very painful to want to add fields. This does not exist in mongodb. You can add fields as you want, so you have the idea of using mongodb in spring boot. There is a demo about using mongodb in spring projects on GitHub, and a link will be given later

rely on

to configure

Only add a @ ID to the entity class

Database configuration

The repository inherits mongorepository. The tracking code can find that mongorepository inherits pagingandsorting repository, so some simple methods such as paging can also be used directly, which is very convenient

How to use spring data is how to use it here

relation

Mongodb cannot be associated in spring boot, so @ manytoone annotations associated with MySQL cannot be used

After my test, if the user object is introduced into the blog entity class, the user object will also be saved in the blog when saving the blog, as shown below

If you modify the user's username, only the username in the user table will be modified, and the username in the user object in the blog table will not change. Therefore, it is recommended that the association only give an ID. when querying, use the ID to check the user, and then package it into a piece

summary

The above is the whole content of this article. I hope the content of this article has a certain reference value for your study or work. If you have any questions, you can leave a message. Thank you for your support for programming tips.

reference resources

https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-data-mongodb

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>