将MongoDB String属性转换为可以查询的Object

时间:2014-08-25 17:50:34

标签: mongodb

我在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存储为String我试图通过mongo shell将其转换为对象:

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

然而,似乎只是把它们分成了一张地图:

 "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" : "}" },

仍然没有重新认识我的嵌套查询,例如:

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

因此给出了我的原始数据结构。需要在转换方面做些什么才能深入研究这个eventdata属性。

1 个答案:

答案 0 :(得分:2)

forEach的示例中,使用JSON.parse(x.eventdata)代替new Object(x.eventdata)

当然你不能用.find()直接查询它 - 它只是从MongoDB的角度来看的字符串。如果你想查询它,你应该把它存储为BSON ...你可以使用的所有内容都记录在这里:http://docs.mongodb.org/manual/reference/operator/query/在从JSON转换到本地BSON(forEach示例)后,它将是可能的查询它。