Mongodb customizes ID with String
This article mainly explains "Mongodb uses String to customize ID". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "Mongodb uses String to customize ID".
Import org.bson.Document;import org.bson.types.ObjectId;import org.springframework.data.mongodb.core.mapping.event.AbstractMongoEventListener;import org.springframework.data.mongodb.core.mapping.event.BeforeConvertEvent;import org.springframework.data.mongodb.core.mapping.event.BeforeSaveEvent;import org.springframework.stereotype.Component;@Componentpublic class BeforeConvertListener extends AbstractMongoEventListener {@ Override public void onBeforeSave (BeforeSaveEvent event) {Document d = event.getDocument () If (d==null) {/ / unlikely return;} Object _ id = d.get ("_ id"); if (_ id = = null) {event.getDocument (). Put ("_ id", new ObjectId (). ToString ());} else if (_ id instanceof ObjectId) {event.getDocument (). Put ("_ id", _ id.toString ()) }}}
Through the listener, when saving, convert all the id of type ObjectId to Stirng, and if it is empty, add an id of type String.
But has a problem querying, or deleting, if the String string is a legitimate ObjectId form. Spring Data Mongo will be automatically transferred to ObjectId to query or delete.
So we can't find the record.
Https://stackoverflow.com/questions/14329175/prevent-spring-data-for-mongo-to-convert-ids-to-objectid
One of the ways introduced here is to throw an exception, which has not been tried yet. I don't want to use it when I look at throwing an exception.
Public class CustomMongoConverter extends MappingMongoConverter {public CustomMongoConverter (MongoDbFactory mongoDbFactory, MappingContext, MongoPersistentProperty > mappingContext) {super (mongoDbFactory, mappingContext); conversionService.addConverter (new Converter () {@ Override public ObjectId convert (String source) {throw new RuntimeException ();}});}}
Not over, estimated to give up the rhythm, use ObjectId as the primary key.
Import cn.hutool.core.lang.UUID;import cn.hutool.core.util.StrUtil;import org.bson.Document; import org.bson.types.ObjectId; import org.springframework.data.mongodb.core.mapping.event.AbstractMongoEventListener; import org.springframework.data.mongodb.core.mapping.event.BeforeConvertEvent; import org.springframework.data.mongodb.core.mapping.event.BeforeSaveEvent; import org.springframework.stereotype.Component @ Componentpublic class BeforeConvertListener extends AbstractMongoEventListener {@ Override public void onBeforeSave (BeforeSaveEvent event) {Document d = event.getDocument (); if (d==null) {/ / unlikely return;} Object _ id = d.get ("_ id") If (_ id = = null) {event.getDocument (). Put ("_ id", UUID.fastUUID (). ToString () / * StrUtil.reverse (new ObjectId (). ToString ()) * /);} else if (_ id instanceof ObjectId) {event.getDocument (). Put ("_ id", UUID.fastUUID (). ToString () / * StrUtil.reverse (_ id.toString ()) * /);}
The previous project used ObjectId, which felt useless. And error-prone. For example, the String passed by the front desk forgot to change to ObjectId when querying.
Finally, there is no need for ObjectId anyway. As long as String does not conform to ObjectId spring, it will not be converted automatically. It is recommended to use UUID string instead. It is no problem to delete the query in this way.
Or add a string and change the length.
Object _ id = d.get ("_ id"); if (_ id = = null) {event.getDocument (). Put ("_ id", new ObjectId (). ToString () + "c");} else if (_ id instanceof ObjectId) {event.getDocument (). Put ("_ id", _ id.toString () + "c");}
Debug, take a look, what is the judgment logic of his transformation?
To judge whether the logic is legal or not is ObjectId.
At this point, I believe you have a deeper understanding of "Mongodb customized ID with String". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!