0 Daumen
83 Aufrufe

Ich habe eine JSON-Datei, die ich in MangoDB hostelen. Ich möchte einige ihrer Elemente erhalten. Hier ist die Datei:

{
  "_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
      }
    }
 ]
}

Ich hätte gerne diese

{
  "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
  }
}

Bis heute habe ich versucht, die Immobilien zu bekommen, aber ich habe ein Problem:


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 = db.collection.find(
    {"_id": "5de9483c65222ef002ad613"},
    { "features": 1, "type": 0, "election": 0, "_id": 0 }
)

for doc in cursor:
    print(doc)

Die cmd gibt mich zuruck:

(base) C:\Users\antoi\Documents\Programming\Richmond\Mongo>python query_mongdb.py
Traceback (most recent call last):
  File "query_mongdb.py", line 20, in <module>
    for doc in cursor:
  File "C:\ProgramData\Anaconda3\lib\site-packages\pymongo\cursor.py", line 1156, in next
    if len(self.__data) or self._refresh():
  File "C:\ProgramData\Anaconda3\lib\site-packages\pymongo\cursor.py", line 1073, in _refresh
    self.__send_message(q)
  File "C:\ProgramData\Anaconda3\lib\site-packages\pymongo\cursor.py", line 955, in __send_message
    address=self.__address)
  File "C:\ProgramData\Anaconda3\lib\site-packages\pymongo\mongo_client.py", line 1347, in _run_operation_with_response
    exhaust=exhaust)
  File "C:\ProgramData\Anaconda3\lib\site-packages\pymongo\mongo_client.py", line 1465, in _retryable_read
    return func(session, server, sock_info, slave_ok)
  File "C:\ProgramData\Anaconda3\lib\site-packages\pymongo\mongo_client.py", line 1341, in _cmd
    unpack_res)
  File "C:\ProgramData\Anaconda3\lib\site-packages\pymongo\server.py", line 136, in run_operation_with_response
    _check_command_response(first)
  File "C:\ProgramData\Anaconda3\lib\site-packages\pymongo\helpers.py", line 159, in _check_command_response
    raise OperationFailure(msg % errmsg, code, response)
pymongo.errors.OperationFailure: Projection cannot have a mix of inclusion and exclusion.
von

Ein anderes Problem?

Stell deine Frage

Willkommen bei der Stacklounge! Stell deine Frage einfach und kostenlos

x
Made by a lovely community