如何读取MongoDB中嵌套Key对应的所有值

时间:2014-09-09 22:22:45

标签: python mongodb

我是MongoDB的新手。可能早些时候会问这个问题,但是我没有按照需要通过尝试给出类似问题的答案来获得解决方案

我想导入对应于" _data.review_Message"的值。从mongoDB文档(带有JSON结构)到Python Panda数据框,如下所示:

{
    "_id" : ObjectId("53e24b225c0f4439356814ee"),
    "_source" : "example.com",
    "_title" : "jspringertx",
    "_url" : "http://www.example.com/ShowUserReviews-g56014-d140093-r134096684-",
    "_data" : {
    "PO Box 508, Huntsville, TX 77342 " : {
            "reviewer_MemberSince" : "Travelling with us since 2011",
            "reviewer_NumberOfContributions" : "119",
            "reviewer_HelpfulVotes" : "23",
            "reviewThingsToDo_PhoneNumber" : "409-295-5644",
            "review_Title" : "Huntsville State Park",
            "reviewer_Name" : "jspringertx",
            "reviewer_NumberOfCitiesVisited" : "81",
            "review_Message" : "Be prepared to have a picnic lunch or hike thru the ..",
            "review_DatePosted" : "Reviewed July 11, 2012",
            "reviewer_NumberOfReviewedCities" : "10",
            "reviewer_NumberOfAttractionReviews" : "21",
            "reviewThingsToDo_Address" : "PO Box 508, Huntsville, TX 77342 ",
            "reviewer_NumberOfReviews" : "54",
            "review_RatingLevel" : "3 of 5 stars",
            "reviewer_CityOfResidence" : "Houston, Texas\n65+ year old man"
            }
     },
    "_attemp_time" : 1407339298.555776
}

我尝试过使用Mongo Query:

sandbox.example_Reviews_Texas.find({}, { "_id":0, "_data.review_Message" : 1 })

其中db名称是sandbox,集合名称是example_Reviews_Texas。我得到空值。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

在引用的结构中,review_Message的路径为:

"_data.PO Box 508, Huntsville, TX 77342 .review_Message" // note the space after 77342.

如果查询如下,您将得到结果:

sandbox.example_Reviews_Texas.find({}, { "_id":0, "_data.PO Box 508, Huntsville, TX 77342 .review_Message" : 1 })

注意:以上内容将帮助您只检索至少一个文档。