Zum Inhalt

Hier ist die formatierte Wiedergabe des Inhalts:


Verwenden von Array-Feldern

In dieser Übung werden Kommentare zum Film hinzugefügt. Der vollständige Text eines Kommentars wird zusammen mit Benutzerdetails wie Name, E-Mail und Datum angegeben.

Python-Beispiel

import pymongo

myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["moviesDB"]
mycol = mydb["movies"]

movielist = {
   "id": 14253,
   "Title": "Beauty and the Beast",
   "year": 2016,
   "language": "English",
   "genre": "Romance",
   "director": "Christophe Gans",
   "runtime": 112,
   "imdb": {
       "rating": 6.4,
       "votes": "17762"
   },
   "tomatoes": {
       "viewer": {
           "rating": 3.9,
           "votes": 238
       },
       "critic": {
           "rating": 4.2,
           "votes": 8
       },
       "fresh": 96,
       "rotten": 7
   },
   "comments": [
       {
           "name": "Talisa Maegyr",
           "email": "oona_chaplin@gameofthron.es",
           "text": "Rem itaque ad sit rem voluptatibus. Ad fugiat...",
           "date": "1998-08-22T11:45:03.000+00:00"
       },
       {
           "name": "Melisandre",
           "email": "carice_van_houten@gameofthron.es",
           "text": "Perspiciatis non debitis magnam. Voluptate...",
           "date": "1974-06-22T07:31:47.000+00:00"
       }
   ]
}

movies = mycol.insert_one(movielist)
print(movies)

Beispielausgabe

<pymongo.results.InsertOneResult object at 0x0000028D4D043A80>

JSON-Darstellung der Daten

{
   "id": 14253,
   "Title": "Beauty and the Beast",
   "year": 2016,
   "language": "English",
   "genre": "Romance",
   "director": "Christophe Gans",
   "runtime": 112,
   "imdb": {
       "rating": 6.4,
       "votes": "17762"
   },
   "tomatoes": {
       "viewer": {
           "rating": 3.9,
           "votes": 238
       },
       "critic": {
           "rating": 4.2,
           "votes": 8
       },
       "fresh": 96,
       "rotten": 7
   },
   "comments": [
       {
           "name": "Talisa Maegyr",
           "email": "oona_chaplin@gameofthron.es",
           "text": "Rem itaque ad sit rem voluptatibus. Ad fugiat...",
           "date": "1998-08-22T11:45:03.000+00:00"
       },
       {
           "name": "Melisandre",
           "email": "carice_van_houten@gameofthron.es",
           "text": "Perspiciatis non debitis magnam. Voluptate...",
           "date": "1974-06-22T07:31:47.000+00:00"
       }
   ]
}

Autor: Joel Mazurek, 16.12.24