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

Using an Array to create a DBObject breaks .equals and .hashcode

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Minor - P4 Minor - P4
    • 2.12.0, 3.0.0
    • Affects Version/s: 2.6.5
    • Component/s: API
    • Labels:
      None
    • Environment:
      All Java
    • Fully Compatible

      Using a Java Array to store information in a DBObject breaks .equals and .hashcode:

      scala> val one = MongoDBObject("anarray" -> Array(MongoDBObject("one" -> "oneval"),MongoDBObject("two"->"twoval")))
      one: com.mongodb.casbah.commons.Imports.DBObject = { "anarray" : [ { "one" : "oneval"} , { "two" : "twoval"}]}
      
      scala> val two = MongoDBObject("anarray" -> Array(MongoDBObject("one" -> "oneval"),MongoDBObject("two"->"twoval")))
      two: com.mongodb.casbah.commons.Imports.DBObject = { "anarray" : [ { "one" : "oneval"} , { "two" : "twoval"}]}
      
      scala> one == two
      res16: Boolean = false
      

      Using a List to store the equivalent object does not:

      scala> val three = MongoDBObject("anarray" -> List(MongoDBObject("one" -> "oneval"),MongoDBObject("two"->"twoval")))
      three: com.mongodb.casbah.commons.Imports.DBObject = { "anarray" : [ { "one" : "oneval"} , { "two" : "twoval"}]}
      
      scala> val four = MongoDBObject("anarray" -> List(MongoDBObject("one" -> "oneval"),MongoDBObject("two"->"twoval")))
      four: com.mongodb.casbah.commons.Imports.DBObject = { "anarray" : [ { "one" : "oneval"} , { "two" : "twoval"}]}
      
      scala> three == four
      res17: Boolean = true
      

      Of course, the two objects are absolutely equal whether they are constructed with an Array or a List:

      scala> mongoCollection += one
      res18: com.mongodb.WriteResult = N/A
      
      scala> mongoCollection += three
      res19: com.mongodb.WriteResult = N/A
      
      scala> mongoCollection.find().foreach(println)
      { "_id" : { "$oid" : "4e3723614206a091d3e32a4c"} , "anarray" : [ { "one" : "oneval"} , { "two" : "twoval"}]}
      { "_id" : { "$oid" : "4e37236e4206a091d3e32a4d"} , "anarray" : [ { "one" : "oneval"} , { "two" : "twoval"}]}
      
      scala> val recs = mongoCollection.find().toArray
      recs: Array[com.mongodb.casbah.Imports.DBObject] = Array({ "_id" : { "$oid" : "4e3723614206a091d3e32a4c"} , "anarray" : [ { "one" : "oneval"} , { "two" : "twoval"}]}, { "_id" : { "$oid" : "4e37236e4206a091d3e32a4d"} , "anarray" : [ { "one" : "oneval"} , { "two" : "twoval"}]})
      
      scala> recs(0).get("anarray") == recs(1).get("anarray")
      res24: Boolean = true
      

      The fix should just be to store any collection that will end up as a BSON array internally as a java List.

            Assignee:
            jeff.yemin@mongodb.com Jeffrey Yemin
            Reporter:
            bhudgeons Brandon Hudgeons
            Votes:
            1 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: