Clicky

Java, Mongo - BasicBSONList can only work with numeric keys not:

March 10, 2016

Problem

<![CDATA[ DBObject offers = (DBObject) ((DBObject)mongoClient.find(query, fields).next().get(“offers”)).get(“offers”); assertEquals(“efgh”, ((DBObject)offers.get(“url”)); ]]>
BasicBSONList can only work with numeric keys, not: [url]

Solution:

Looking at the Intelij debug output the solution became obvious:

offers = {BasicDBList@5718}  size = 1
  0 = {BasicDBObject@7368}  size = 12
     0 = {LinkedHashMap$Entry@7371} “@type” -> “Offer”
     1 = {LinkedHashMap$Entry@7372} “availability” ->
     2 = {LinkedHashMap$Entry@7373} “name” -> “xxx”
     3 = {LinkedHashMap$Entry@7374} “description” -> “yyy”
     4 = {LinkedHashMap$Entry@7375} “itemCondition” ->
     5 = {LinkedHashMap$Entry@7376} “price” -> “378.56”
     6 = {LinkedHashMap$Entry@7377} “priceCurrency” -> “EUR”
     7 = {LinkedHashMap$Entry@7378} “priceSpecification” -> " size = 1”
     8 = {LinkedHashMap$Entry@7379} “offerID” -> “2”
     9 = {LinkedHashMap$Entry@7380} “seller” -> " size = 2”
     10 = {LinkedHashMap$Entry@7381} “sku” ->
     11 = {LinkedHashMap$Entry@7382} “url” -> “google.com”

<![CDATA[ BasicDBList offers = (BasicDBList) ((DBObject)mongoClient.find(query, fields).next().get(“offers”)).get(“offers”); assertEquals(“google.com”, ((DBObject)offers.get(0)).get(“url”)); ]]>

SyntaxHighlighter.highlight();