Java Spring数据-MongoRepository:我们可以用二进制类型作为_id吗?
我有一个类,它表示一个名为“archive”的Mongo集合 接口消息Respository扩展了MongoRespository:Java Spring数据-MongoRepository:我们可以用二进制类型作为_id吗?,java,spring,mongodb,spring-data-mongodb,Java,Spring,Mongodb,Spring Data Mongodb,我有一个类,它表示一个名为“archive”的Mongo集合 接口消息Respository扩展了MongoRespository: public interface MessagesRepository extends MongoRepository<Message, String>{ } 公共接口消息存储扩展了MongoRepository{ } 通过一个API调用,我得到一个findMessage请求,它在String中给我一个messageId。然后我需要将其编码为字节
public interface MessagesRepository extends MongoRepository<Message, String>{
}
公共接口消息存储扩展了MongoRepository{
}
通过一个API调用,我得到一个findMessage
请求,它在String
中给我一个messageId
。然后我需要将其编码为字节[],然后调用messagesRepository.findOne()
方法。(请记住ID是字节[]
)
它失败了。它返回null
。我猜是因为Mongo中存储的byte[]
与findOne()
方法中的byte[]
不同,因为不同的字符串即使具有相同的值也会产生不同的byte[]
数组
我怎样才能做到这一点?或者真的有可能使用二进制的_id吗?您使用
字节[]
作为id并不是在帮自己的忙
你说你得到一个messageId作为字符串
-为什么不使用它呢?否则,您可能会在字符串类型和字节类型之间转换(除非byte[]
的使用不是由您自己选择的,而是由您指定的约束)。您注意到,即使是从相等的字符串生成的字节也不匹配
简短示例:
public static void main(String[] args) throws Exception {
String s1 = "hello";
String s2 = "hello";
System.out.println("our two strings: ");
System.out.println(s1);
System.out.println(s2);
// one of the first thing we learned when starting with
// java was that this is not equal
System.out.println();
System.out.println("compare with ==");
if(s1==s2) System.out.println("equal");
else System.out.println("not equal");
// but this is equal
System.out.println("compare with equals()");
if(s1.equals(s2)) System.out.println("equal");
else System.out.println("not equal");
// create the byte arrays and compare them
byte[] b1 = s1.getBytes();
byte[] b2 = s2.getBytes();
System.out.println();
System.out.println("byte array 1: " + b1.toString());
System.out.println("byte array 2: " + b2.toString());
// same as for strings
System.out.println("compare with ==");
if(b1==b2) System.out.println("equal");
else System.out.println("not equal");
// not equal, unlike the strings from which we
// created the byte arrays
System.out.println("compare with equals()");
if(b1.equals(b2)) System.out.println("equal");
else System.out.println("not equal");
// create string out of the bytes again and compare
String ss1 = new String(b1, "UTF-8");
String ss2 = new String(b2, "UTF-8");
System.out.println();
System.out.println("re-created string 1: " + ss1);
System.out.println("re-created string 2: " + ss2);
// this is equal again
System.out.println("compare re-created strings with equals()");
if(ss1.equals(ss2)) System.out.println("equal");
else System.out.println("not equal");
}
这就是为什么我问为什么它必须是字节;它让一切变得更难,而不是更容易。嗯,它仍然有效。我可以使用
字节[]
作为\u id
。我能够成功地插入和检索内容。find()
的问题在MongoRepository中。它需要调整以使其工作
见:
编辑:我得到了最新的驱动程序,它马上就工作了。ID字段的逻辑数据类型是什么?@chrylis:OP说它是byte[]@“Sasanka Panguluri”:使用二进制/字节作为_id,这并不是在帮自己的忙;为什么不使用消息字符串或其哈希代码呢?@Seismoid我问逻辑类型是有原因的。很明显,立即声明的类型是
byte[]
。使用hashCode是荒谬的;数据库ID必须是唯一的。@chrylis:是的,hasCode是一个错误的,抱歉。他说他得到了一个字符串messageId
,所以我猜他不知怎么地从这个字符串创建了他的字节数组;我只是想知道为什么字符串不够好…@chrylis原始类型是string。messageId实际上是一个UUID,如果以二进制编码格式存储在Mongo中,将节省我的空间。没错,谢谢。但我还能做些什么来节省Mongo中的空间呢?
public static void main(String[] args) throws Exception {
String s1 = "hello";
String s2 = "hello";
System.out.println("our two strings: ");
System.out.println(s1);
System.out.println(s2);
// one of the first thing we learned when starting with
// java was that this is not equal
System.out.println();
System.out.println("compare with ==");
if(s1==s2) System.out.println("equal");
else System.out.println("not equal");
// but this is equal
System.out.println("compare with equals()");
if(s1.equals(s2)) System.out.println("equal");
else System.out.println("not equal");
// create the byte arrays and compare them
byte[] b1 = s1.getBytes();
byte[] b2 = s2.getBytes();
System.out.println();
System.out.println("byte array 1: " + b1.toString());
System.out.println("byte array 2: " + b2.toString());
// same as for strings
System.out.println("compare with ==");
if(b1==b2) System.out.println("equal");
else System.out.println("not equal");
// not equal, unlike the strings from which we
// created the byte arrays
System.out.println("compare with equals()");
if(b1.equals(b2)) System.out.println("equal");
else System.out.println("not equal");
// create string out of the bytes again and compare
String ss1 = new String(b1, "UTF-8");
String ss2 = new String(b2, "UTF-8");
System.out.println();
System.out.println("re-created string 1: " + ss1);
System.out.println("re-created string 2: " + ss2);
// this is equal again
System.out.println("compare re-created strings with equals()");
if(ss1.equals(ss2)) System.out.println("equal");
else System.out.println("not equal");
}