2014-08-29 32 views
0

我已经代表一个蒙戈集合类名为“存档”Spring Data- MongoRepository:我们可以使用_id作为二进制类型吗?

@Document(collection = "archive") 

public class Message{ 
    @Id 
    private byte[] messageId; 

    private String from; 
    private String to; 
    // more stuff 

} 

接口MessagesRepository扩展MongoRepository:

public interface MessagesRepository extends MongoRepository<Message, String>{ 

} 

通过API调用,我得到一个findMessage要求,这给了我一个messageIdString。然后我需要将它编码为byte [],然后调用messagesRepository.findOne()方法。 (请记住ID是byte[])。

它失败。它返回null。我猜想因为存储在Mongo中的byte []将与findOne()方法中的byte[]不同,因为即使值相同的不同字符串也会产生不同的byte[]阵列。

我该如何做这项工作?或者真的可以在二进制文件中使用_id?

+0

什么是ID字段的逻辑数据类型? – chrylis 2014-08-29 22:14:43

+0

@chrylis:OP表示它是byte []; @“Sasanka Panguluri”:你没有为_id使用二进制/字节来帮助自己;为什么不使用消息String或其hashCode呢? – Seismoid 2014-08-29 22:20:31

+0

@Seismoid有一个原因,我问**的逻辑**类型。很明显,立即声明的类型是'byte []'。使用'hashCode'是荒谬的;数据库ID必须是唯一的。 – chrylis 2014-08-29 22:22:23

回答

0

那么,它仍然有效。我能够有byte[]作为_id。我能够成功插入和检索的东西。 find()的问题在MongoRepository中。它需要进行调整才能使其工作。

请参见:http://pastebin.com/xHVQJYfN

编辑:我得到了最新的驱动程序和它的工作,立竿见影。

0

你不是通过使用byte[]作为一个id做自己的忙。

你说你得到一个messageId作为String - 为什么不使用它?否则你可能只是最终在字符串和字节类型之间进行转换(除非使用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"); 
} 

这就是为什么我问为什么它必须是字节;它使一切变得更加困难,而不是更容易。

+0

没错,谢谢。但是我还能做些什么来节省我的Mongo空间? – 2014-08-30 14:55:06

相关问题