我有一个以
格式存储在文档中的json字符串{
"alert":"{\"UploadTimeStamp\":xxx,\"PDID\":\"xxx\",\"Alerts\":[{\"Timezone\":330,\"AlertType\":\"OverSpeed\",\"Data\":{\"Latitude\":\"10.9\",\"Longitude\":\"43.44\",\"MtrsPerSec\":\"125.5\"},\"TimeStamp\":1499346345164}]}"
}
如何获取UploadTimeStamp,Latitude,Longitude等特定字段?
我尝试使用db.collection.find({},{alert:1,_id:0})
它显示如上所述的警报字典,但当我尝试访问alert.UploadTimeStamp
时,我得到null结果。这是我使用的cmd
db.collection.find({_id: ObjectId("xxx")},{"alert.UploadTimeStamp":1})
答案 0 :(得分:0)
你可以使用。(点) e.g。
db.dc1.find({},{"UploadTimeStamp":1})
OR
db.collection.find({},{"Alerts.Timezone":1})
OR
db.dc1.find({},{"Alerts.Data.Latitude":1, "Alerts.Data.Longitude":1})
你的JSON如下: -
db.dc1.insert({
"UploadTimeStamp": "2018-03-28T10:53:14.687-0700",
"PDID": "1212",
"Alerts": [
{
"Timezone": 330,
"AlertType": "OverSpeed",
"Data": {
"Latitude": "10.9",
"Longitude": "43.44",
"MtrsPerSec": "125.5"
},
"TimeStamp": 1499346345164
}
]
})