简体   繁体   English

Spring Data- MongoRepository:我们可以将二进制类型用作_id吗?

[英]Spring Data- MongoRepository : Can we have binary type as the _id?

I have a class that represents a Mongo collection called "archive" 我有一个代表Mongo集合的类,称为“档案”

@Document(collection = "archive")

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

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

}

The interface MessagesRepository extends MongoRepository: MessagesRepository接口扩展了MongoRepository:

public interface MessagesRepository extends MongoRepository<Message, String>{

}

Through an API call, I get a findMessage request, which gives me a messageId in String . 通过API调用,我得到了findMessage请求,该请求在String提供了messageId I will then need to encode it into byte[] and then invoke messagesRepository.findOne() method. 然后,我需要将其编码为byte [],然后调用messagesRepository.findOne()方法。 ( Remember the ID is byte[] ). (请记住ID是byte[] )。

It fails. 它失败。 It returns null . 返回null I guess because the byte [] stored in Mongo would be different from the byte[] within the findOne() method since different strings even with same values produce different byte[] arrays. 我想,因为byte []存储在蒙戈将来自不同byte[]的内findOne()的方法,因为不同的字符串,即使相同的值产生不同的byte[]数组。

How can I make this work? 我该如何进行这项工作? Or is it really possible to work with _id in binary? 还是真的有可能以二进制形式使用_id?

you are not doing yourself a favor by using byte[] as an id. 使用byte[]作为ID并不能帮自己一个忙。

you say you get a messageId as a String - why not use that? 你说你得到一个messageId作为一个String -为什么不使用它呢? otherwise you might just end up casting between string and bytes types (unless the use of byte[] is not by your own choice, but an constraint given to you). 否则,您可能最终只能在字符串和字节类型之间进行转换(除非使用byte[]不是您自己选择的,而是给您的约束)。 you noticed yourself that the bytes generated even from equal strings don't match. 您注意到自己,即使从相等的字符串生成的字节也不匹配。

short example: 简短的例子:

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");
}

that's why i ask why it has to be bytes; 这就是为什么我问为什么它必须是字节; it's making everything HARDER, not easier. 这使一切变得更加艰难,而不是那么容易。

Well, it still worked. 好吧,它仍然有效。 I was able to have a byte[] as the _id . 我有一个byte[]作为_id I was able to successfully insert and retrieve stuff. 我能够成功插入和检索东西。 The problem with find() is in MongoRepository. find()的问题在MongoRepository中。 It needs to be adjusted to make it work. 需要对其进行调整以使其起作用。

See : http://pastebin.com/xHVQJYfN 请参阅: http//pastebin.com/xHVQJYfN

EDIT : I got the latest driver and it worked straight away. 编辑:我得到了最新的驱动程序,并且可以立即工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM