0

I have the following data in mongoDB

{ "_id" : "ee477d7a-7a0c-4420-b476-c402012c74a9", 
"_class" : "com.TrackingData", 
"modified" : ISODate("2014-07-09T20:23:33.117Z"), 
"eventtype" : "Test",
"eventdata" : "{\"QueryDate\":\"01-APR-2014\",
  \"SearchQuery\":{\
  "keyword\":\"Java\",\
  "location\":\"Santa Clara, CA\",
  \"Facet\":\"skill~java~perl|workAuth~USC\",
  \"SearchAgentId\":\"4299\"
  },
  \"Viewed\":[
  {\"ViewedID\":\"8992ade400a\",
  \"Dockey\":\"3323aba3233\",
  \"PID\":\"32399a\",
  \"actionsTaken\":\"email|direct message|report seeker\",
  \"viewDate\":\"01-APR-2014\",
  \"MessageSent\":\"true\",
  \"Message\":[
  {\"MessageID\":\"123aca323\",
  \"Delivered\":\"True\",
  \"Opened\":\"True\",
  \"ClickThroughRate\":\"NotBad\",
  \"MessageDate\":\"02-APR-2014\",
  \"Response\":[{\"ResponseId\":\"a323a9da\",\"ResponseDate\":\"23-APR-2014\"}]}]}]}",
 "eventsource" : "API-Dev Test - JMachine", 
 "sourceip" : "myIp", 
 "entityid" : "Test", 
 "groupid" : "ice", 
 "datecreated" : ISODate("2014-07-09T20:23:33.112Z") }

evendata is stored as a String I have tried to convert this to an object through the mongo shell:

db.mydb.find( { 'eventdata' : { $type : 2 } } ).forEach( function (x) { x.eventdata = new Object(x.eventdata); db.mydb.save(x);});

However is simply seemed to split these into a map:

 "eventdata" : { "0" : "{", "1" : "\"", "2" : "V", "3" : "i", "4" : "e", "5" : "w", "6" : "e", "7" : "d", "8" : "\"", "9" : ":", "10" : "[", "11" : "{", "12" : "\"", "13" : "V", "14" : "i", "15" : "e", "16" : "w", "17" : "e", "18" : "d", "19" : "I", "20" : "D", "21" : "\"", "22" : ":", "23" : "\"", "24" : "8", "25" : "9", "26" : "9", "27" : "2", "28" : "a", "29" : "d", "30" : "e", "31" : "4", "32" : "0", "33" : "0", "34" : "a", "35" : "\"", "36" : ",", "37" : "\"", "38" : "D", "39" : "o", "40" : "c", "41" : "k", "42" : "e", "43" : "y", "44" : "\"", "45" : ":", "46" : "\"", "47" : "1", "48" : "7", "49" : "2", "50" : "9", "51" : "f", "52" : "7", "53" : "a", "54" : "f", "55" : "c", "56" : "d", "57" : "1", "58" : "f", "59" : "7", "60" : "6", "61" : "3", "62" : "9", "63" : "3", "64" : "a", "65" : "b", "66" : "6", "67" : "6", "68" : "d", "69" : "5", "70" : "c", "71" : "6", "72" : "4", "73" : "8", "74" : "a", "75" : "f", "76" : "3", "77" : "7", "78" : "b", "79" : "\"", "80" : ",", "81" : "\"", "82" : "P", "83" : "I", "84" : "D", "85" : "\"", "86" : ":", "87" : "\"", "88" : "\"", "89" : ",", "90" : "\"", "91" : "a", "92" : "c", "93" : "t", "94" : "i", "95" : "o", "96" : "n", "97" : "s", "98" : "T", "99" : "a", "100" : "k", "101" : "e", "102" : "n", "103" : "\"", "104" : ":", "105" : "\"", "106" : "\"", "107" : ",", "108" : "\"", "109" : "v", "110" : "i", "111" : "e", "112" : "w", "113" : "D", "114" : "a", "115" : "t", "116" : "e", "117" : "\"", "118" : ":", "119" : "\"", "120" : "0", "121" : "9", "122" : "-", "123" : "J", "124" : "U", "125" : "L", "126" : "-", "127" : "2", "128" : "0", "129" : "1", "130" : "4", "131" : " ", "132" : "2", "133" : "0", "134" : ":", "135" : "3", "136" : "1", "137" : ":", "138" : "2", "139" : "3", "140" : "\"", "141" : "}", "142" : "]", "143" : "}" },

Which still does not recongised my nested queries such as:

 db.mydb.find({'eventdata.SearchQuery.keyword' :'keywordValue' }).skip(0).limit(20)

So given my original data structure. What need to be done in terms of transformation to allow me to drill down into this eventdata attribute.

1 Answer 1

3

In your example with forEach use JSON.parse(x.eventdata) instead of new Object(x.eventdata).

Of course you cannot query it directly with .find() - it's just string from the MongoDB point of view. You should have store it as BSON if you wanted to query it... All you can use is documented here: http://docs.mongodb.org/manual/reference/operator/query/ After the transformation from JSON to native BSON (the forEach example) it will be possible to query it.

Sign up to request clarification or add additional context in comments.

2 Comments

Big ty Messa. Worked a treat :) Can I ask what the type is that is returned from JSON.parse(x.eventdata) and what type mongo stores this, as I want to make the correction on the persistence layer too
db.mydb.find( { 'eventdata' : { $type : "string" } } ) .forEach( function (x) { x.eventdata = JSON.parse(x.eventdata) ; db.mydb.save(x);})

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.