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.
- is duplicated by
-
JAVA-482 (DB/BSON)Object "equals" method is broken
-
- Closed
-
-
JAVA-1090 For embedded $regex, BasicBSONObject hashCode() output is not consistent with equals()
-
- Closed
-
-
JAVA-1112 BasicBSONObject.equals is broken for binary data.
-
- Closed
-
- is related to
-
JAVA-482 (DB/BSON)Object "equals" method is broken
-
- Closed
-