這可能是一個簡單的問題,但我不是Spring的專家。 我有一個類表示mongoDB中名爲「messages」的集合中的文檔的消息。Spring MongoRepository:在插入到MongoDB之前轉換爲二進制文件
@Document(collection = "archive")
public class Message{
@Id
private String messageId;
private String from;
private String to;
// more stuff
}
接口MessagesRepository
延伸MongoRepository<Message,String>
:
public interface MessagesRepository extends MongoRepository<Message, String>{
}
我有MongoRepository.findOne(ID)
方法調用,其中ID是一個字符串,在不同的模塊中我的代碼。
這一切正常。該ID稱爲messageId
是String
。但我想以binary encoded
格式將它插入到Mongo中。我的第一個粗略的估計是做
@Id
private byte[] messageId;
在Message類
但它既不覺得我的權利,也沒有工作,因爲我發現我是爲不同的字符串獲得不同byte[]
即使使用相同的值的對象。
我見過MongoMappingConverter,但不知道它是否有效。有人可以投射一些光線嗎?
謝謝!