1

I have an entity with the following fields:

var name: String?,
var metadata: org.bson.Document?,
var things: Map<String, MyObject> = mapOf(),
var other_things: Map<String, MyObject> = mapOf(),
@Indexed(unique = true) var hash: String?

The keys of things and other_things are the hashes of their respective MyObject objects. The hashes do not contain dots in them. When I save the entity - the maps are correctly serialized and saved in mongodb.

An example map - taken directly from my mongo console (I have masked the MyObject fields...):

{
  "0xa5643bf27e2786816613d3eeb0b62650200b5a98766dfcfd4428f296fb56d043": {
    "field": true,
    "field1": [
      {
        "field1field": "foo",
        "field1field1": "",
        "field1field2": false
      }
    ],
    "field2": "sam",
    "field3": []
  },
  "0xfce353f601a3db60cb33e4b6ef4f91e4465eaf93c292b64fcde1bf4ba6819b6a": {
    "field": true,
    "field1": [
      {
        "field1field": "bash",
        "field1field1": "",
        "field1field2": true
      }
    ],
    "field2": "bar",
    "field3": []
  },
  "0xcdcd77c0992ec5bbfc459984220f8c45084cc24d9b6efed1fae540db8de801d2": {
    "field": true,
    "field1": [
      {
        "field1field": "mash",
        "field1field1": "",
        "field1field2": false
      }
    ],
    "field2": "baz",
    "field3": []
  }
}

However, when I retrieve the entity and print the keys of either map - there is a . prepended to each key i.e. the output of println(entity.things.keys) is

[.0xa5643bf27e2786816613d3eeb0b62650200b5a98766dfcfd4428f296fb56d043, .0xfce353f601a3db60cb33e4b6ef4f91e4465eaf93c292b64fcde1bf4ba6819b6a, .0xcdcd77c0992ec5bbfc459984220f8c45084cc24d9b6efed1fae540db8de801d2]

I do not know what is going on here can someone clarify?

N.B. the maps were previously Map<String, String> and I did not have this issue

EDIT I changed things and other_things to be a List<MySuperObject> which has 2 fields of type String and MyObject which slightly changes the structure in mongodb (it is saved as an array) - the phantom . is gone - but this does not answer the original question...

I. Kirilov
  • 281
  • 3
  • 12
  • Given that hashes are by definition not unique, keying maps on them is generally a Bad Idea. – Boris the Spider Dec 30 '18 at 09:47
  • Agreed - but that is the spec I am working with. Either way, the fact that the key is a hash is irrelevant. It seems that there is an issue with mongodb java driver when converting `Map` – I. Kirilov Dec 30 '18 at 15:29
  • My guess - just a guess - the dot is added so that the string isn't interpreted as a numeric literal. `0x...` is hexadecimal. – Boris the Spider Dec 30 '18 at 15:53

0 Answers0