Uploaded image for project: 'Java Driver'
  1. Java Driver
  2. JAVA-946

DBObject.equals can fail when binary data exists

    XMLWordPrintableJSON

Details

    • Icon: Bug Bug
    • Resolution: Won't Fix
    • Icon: Minor - P4 Minor - P4
    • None
    • 2.11.2
    • BSON
    • None

    Description

      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

      Attachments

        Activity

          People

            Unassigned Unassigned
            daniel.gottlieb@mongodb.com Daniel Gottlieb (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: