我在MongoDB中托管了一个JSON文件,我想获取它的一些元素。这是文件
{
"_id": "5de9483c65222ef002ad6135",
"election": "local-min-2015",
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
5039258.279933322,
5142586.1579948
],
[
5039615.847231327,
5142411.105566747
]
]
]
},
"properties": {
"1": {
"Partido Popular": "50.64"
},
"2": {
"Partido Socialista Obrero Espanol": "3.22"
},
"3": {
"Unidos Podemos": "1.29"
},
"4": {
"Ciudadanos": "26.18"
},
"5": {
"Vox": "18.45"
},
"id": 96918,
"parent_id": 96916,
"common_id": "11.01",
"common_name": "11.01",
"has_children": true,
"shape_type_id": 5,
"shape_type_name": "Majoritarian District",
"value": "No Data",
"color": null,
"title_location": "Majoritarian District: 11.01",
"Total Voter Turnout (%)": 55.08
}
},
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
5039258.279933322,
5142586.1579948
],
[
5039615.847231327,
5142411.105566747
]
]
]
},
"properties": {
"1": {
"Partido Popular": "45.64"
},
"2": {
"Partido Socialista Obrero Espanol": "7.22"
},
"3": {
"Unidos Podemos": "2.29"
},
"4": {
"Ciudadanos": "26.18"
},
"5": {
"Vox": "18.45"
},
"id": 96919,
"parent_id": 96916,
"common_id": "11.01",
"common_name": "11.01",
"has_children": true,
"shape_type_id": 5,
"shape_type_name": "Majoritarian District",
"value": "No Data",
"color": null,
"title_location": "Majoritarian District: 11.01",
"Total Voter Turnout (%)": 55.08
}
}
]
}
我想得到:
{
"0": {
"parties": [
{
"Partido Popular": 50.64
},
{
"Partido Socialista Obrero Espanol": 3.22
},
{
"Unidos Podemos": 1.29
},
{
"Ciudadanos": 26.18
},
{
"Vox": 18.45
}
],
"id": 96918
},
"1": {
"parties": [
{
"Partido Popular": 45.64
},
{
"Partido Socialista Obrero Espanol": 7.22
},
{
"Unidos Podemos": 2.29
},
{
"Ciudadanos": 26.18
},
{
"Vox": 18.45
}
],
"id": 96919
}
}
这是我写的代码。到目前为止,我可以访问properties
字段:
import pymongo
from bson.son import SON
import json
import os
client = pymongo.MongoClient("mongodb://localhost:27017/")
db = client["Richmond"]
collection = db['Collection']
# select the documents where the field features contains the field 0
cursor = collection.find(
{ "election": "local-min-2015" },
{ "features.properties": 1}#, "features.id":1 }#, "type": 0, "election": 0, "_id": 0 } # get the very first item of the features array
)
for doc in cursor:
print(doc)
命令返回给我:
(base) C:\Users\antoi\Documents\Programming\Richmond\Mongo>python query_mongdb.py
{
"_id": "5de9483c65222ef002ad6135",
"features": [
{
"properties": {
"1": {
"Partido Popular": "50.64"
},
"2": {
"Partido Socialista Obrero Espanol": "3.22"
},
"3": {
"Unidos Podemos": "1.29"
},
"4": {
"Ciudadanos": "26.18"
},
"5": {
"Vox": "18.45"
},
"id": 96917,
"parent_id": 96918,
"common_id": "999",
"common_name": "11.01",
"has_children": true,
"shape_type_id": 5,
"shape_type_name": "Majoritarian District",
"value": "No Data",
"color": null,
"title_location": "Majoritarian District",
"Total Voter Turnout (%)": 55.08
}
},
{
"properties": {
"1": {
"Partido Popular": "45.64"
},
"2": {
"Partido Socialista Obrero Espanol": "7.22"
},
"3": {
"Unidos Podemos": "2.29"
},
"4": {
"Ciudadanos": "26.18"
},
"5": {
"Vox": "18.45"
},
"id": 96919,
"parent_id": 96916,
"common_id": "999",
"common_name": "11.01",
"has_children": true,
"shape_type_id": 5,
"shape_type_name": "Majoritarian District",
"value": "No Data",
"color": null,
"title_location": "Majoritarian District",
"Total Voter Turnout (%)": 55.08
}
}
]
}
目前没有回答
相关问题 更多 >
编程相关推荐