title: MongoDB[Java] : How do I query a stored UUID in byte[ ] format?
body: <p>I am trying to query a java.util.UUID stored in my MongoDB, using a byte[] that corresponds to the same string the stored UUID is based on. </p>
  
  <p>I've seen that the Mongo Driver automatically converts the UUID to bytes and stores it as a BinData(subtype, ). Here's the code I used to save a UUID:</p>
  
  <pre><code>        UUID originalUUID= UUID.fromString("7ee973c0-54b5-11e4-aaed-0002a5d5c51b");
          MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
          DB db = mongoClient.getDB("test");
          DBCollection coll = db.getCollection("test");
  
          BasicDBObject query = new BasicDBObject("_id", originalUUID);
          coll.save(query);
  </code></pre>
  
  <p>I know we can easily retrieve this document with a UUID object as the value in a BasicDBObject, but I would like to use a byte[] instead. After all, it's gotta be bytes in my Mongo right? 
  I know a few complications arise with Java messing up the byte-order before sending it off to Mongo, 
  but I haven't been able to get it to work. My code for retrieval is here:</p>
  
  <pre><code>        String stringOne = "7ee973c0-54b5-11e4-aaed-0002a5d5c51b"; 
          // It's the same UUID String
  
          BasicDBObject find = new BasicDBObject("_id", stringOne.getBytes());
  
          DBCursor cursor = null;
          cursor = coll.find(find);
          while(cursor.hasNext()){
              BasicDBObject dbObject = (BasicDBObject)cursor.next();
              byte[] received = (byte[])dbObject.get("_id");
              System.out.println(new String(received));
          }
  </code></pre>
  
  <p>I've also tried Little Endian encoding like this:</p>
  
  <pre><code>        byte[] bytes = new byte[16];
          ByteBuffer bb = ByteBuffer.wrap(bytes);
          bb.order(ByteOrder.LITTLE_ENDIAN);
  
          bb.putLong(originalUUID.getMostSignificantBits());
          bb.putLong(originalUUID.getLeastSignificantBits());
  
          BasicDBObject find = new BasicDBObject("_id", bb.array()); //Also tried flipping bb.
  </code></pre>
  
  <p>None of these things worked. Can someone provide some insight into this, with a solution?</p>
  
  <p>Thank you!</p>