2014-08-29 76 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

相關問題