Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Mongodb customizes ID with String

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report