本篇內(nèi)容主要講解“MongoDB用String自定義ID”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“Mongodb用String自定義ID”吧!
我們提供的服務有:成都網(wǎng)站建設、做網(wǎng)站、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認證、沂水ssl等。為成百上千家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務,是有科學管理、有技術的沂水網(wǎng)站制作公司
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;
@Component
public class BeforeConvertListener extends AbstractMongoEventListener<Object> {
@Override
public void onBeforeSave(BeforeSaveEvent<Object> event) {
Document d = event.getDocument();
if(d==null){ //不太可能
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());
}
}
}通過監(jiān)聽器 , 保存的時候,把 ObjectId 類型的id都轉成 Stirng, 如果是空的,就自己加一個String類型的id.
But 有一個問題, 查詢,或者刪除的時候, 如果String 字符串是一個合法的 ObjectId 的形式。 Spring Data Mongo 會自動轉成 ObjectId 去查詢或刪除。
這樣就找不到記錄了。
https://stackoverflow.com/questions/14329175/prevent-spring-data-for-mongo-to-convert-ids-to-objectid
這里介紹的一個方式是 拋異常, 還沒試過。 看著拋異常就不太想用。
public class CustomMongoConverter extends MappingMongoConverter {
public CustomMongoConverter(MongoDbFactory mongoDbFactory, MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext) {
super(mongoDbFactory, mappingContext);
conversionService.addConverter(new Converter<String, ObjectId>() {
@Override
public ObjectId convert(String source) {
throw new RuntimeException();
}
});
}
}還沒結束, 估計要放棄的節(jié)奏,就用 ObjectId 當主鍵。
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;
@Component
public class BeforeConvertListener extends AbstractMongoEventListener<Object> {
@Override
public void onBeforeSave(BeforeSaveEvent<Object> event) {
Document d = event.getDocument();
if(d==null){ //不太可能
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())*/);
}
}
}之前一個項目用了 ObjectId ,感覺沒啥用。 還容易出錯。 比如前臺傳的String, 查詢的時候忘記轉為 ObjectId了...
最后,反正是不用 ObjectId了, 只要 String 不符合 ObjectId spring 就不會自動轉換, 建議就用 UUID字符串 替代。這樣查詢刪除也沒問題。
或者加一個字符串,改變長度就行。
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看一下, 他這個轉換的判斷邏輯是啥,
判斷邏輯就是是不是合法的 ObjectId.

到此,相信大家對“Mongodb用String自定義ID”有了更深的了解,不妨來實際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關內(nèi)容可以進入相關頻道進行查詢,關注我們,繼續(xù)學習!
分享題目:Mongodb用String自定義ID
標題URL:http://chinadenli.net/article30/jggeso.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供用戶體驗、Google、自適應網(wǎng)站、ChatGPT、響應式網(wǎng)站、網(wǎng)站導航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)