DBObject.equals can fail when binary data exists

XMLWordPrintableJSON

    • Type: Bug
    • Resolution: Won't Fix
    • Priority: Minor - P4
    • None
    • Affects Version/s: 2.11.2
    • Component/s: BSON
    • None
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      My understanding of the cause:

      While the Binary type implements an `equals` method that compares byte arrays, the decoder conveniently replaces the binary type with the native java byte array. In `BasicBSONObject.equals`, byte arrays are compared with `a.equals(b)` which I believe is a pointer equality check and not a data equality check. The following code/output sample illustrates a case where I feel the equality check on `DBObjects` should return true.

          public static void main(String[] jargs) throws Exception {
              Mongo mongo = new MongoClient();
              DBCollection coll = mongo.getDB("test").getCollection("binary");
              coll.drop();
      
              DBObject item = new BasicDBObject();
              item.put("_id", 1);
              item.put("data", new byte[10]);
      
              // Insert a single item
              coll.insert(item);
      
              // Retrieve the same item twice
              DBObject first = coll.findOne(new BasicDBObject("_id", 1));
              DBObject second = coll.findOne(new BasicDBObject("_id", 1));
      
              System.out.println("First: " + first + " data: " + Arrays.toString((byte[])first.get("data")));
              System.out.println("Second: " + second + " data: " + Arrays.toString((byte[])second.get("data")));
              // Not true :(
              System.out.println("Equals? " + (first.equals(second)));
          }
      
      First: { "_id" : 1 , "data" : <Binary Data>} data: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
      Second: { "_id" : 1 , "data" : <Binary Data>} data: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
      Equals? false
      

            Assignee:
            Unassigned
            Reporter:
            Daniel Gottlieb (Inactive)
            None
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: