The mongodb data used in the separate development of the front and back office made me feel very strange during the development process.
Spring-data-mongo converts mongodb’s query data into entity classes, which in turn are converted into responsebody
JSON string, this is really enough.
Is it not good to send Mongodb’s results directly to the front desk?
Why do you have to make two unnecessary conversions?
Is there anyone who shares my thoughts?
As for data expression, MongoDB uses JSON, but due to some inherent defects of JSON (such as lack of types), the database cannot use it at the bottom level, so what MongoDB stores is actually BSON, which is a binary format that is beneficial to storage and transmission but unreadable to human beings. The result obtained from MongoDB is actually BSON. Neither BSON nor JSON is a type that Java supports natively, so they all have corresponding encapsulation classes in Java. BSON corresponds toBsonDocument, JSON type is not reflected in the driver, because the driver gives is
DocumentType, this will be discussed later.
From the overall process, the driver gets a BSON document after sending out the request, which can be understood as a pile of binary data, which cannot be directly used, so it is changed into through deserialization.
BsonDocument, this type has some interesting API, such as
toJson. In theory, it can be directly converted into JSON and returned back from here, but in actual use, we only need JSON strings as the result in very few cases, and in more cases, we need to take out the values in the document for use. For this purpose, all query methods return
DocumentWhich is equivalent to a dictionary or hash table, usually this is the basic type we use, and it is also supported
The following just mentioned Spring Data, which is located at the upper layer than the driver. In fact, it calls the driver and then realizes more upper layer functions on this basis. For example, mapping to POJO is one of them, and the rest are various CRUD methods. As mentioned earlier, JSON is probably the most useful for you from the front-end perspective. Most of the time, what we need is not to turn an object into JSON, but to query and modify its values conveniently according to business logic. This is also the meaning of Spring Data.
If your only purpose is to become JSON, then Spring Data is not needed at all. It is better to use drivers directly. It is indeed more efficient, and the drivers themselves are actually closer to Mongo. Spring Data has made some of its own encapsulation in order to form common semantics between different databases, which is sometimes not very easy to understand.
Having said so much, I don’t know if I have deepened your understanding of the positioning of Mongo driver and Spring Data. I hope it will be helpful.