MongoDB डेटाबेस पर क्वेरी करना

Python में MongoDB परिचय

Filip Schouwenaars

Machine Learning Researcher

Databases और collections

  • डेटाबेस
    • संबंधित कलेक्शंस
    • film
  • कलेक्शन
    • लचीली स्कीमा वाले डॉक्यूमेंट्स
    • movies

visual

Python में MongoDB परिचय

Python में Databases और collections

from pymongo import MongoClient 
client = MongoClient()

# उपलब्ध डेटाबेस सूचीबद्ध करें client.list_database_names()
['admin', 'config', 'film', 'local']
# film DB पर collections सूचीबद्ध करें
client.film.list_collection_names()
['movies']
Python में MongoDB परिचय

सभी डॉक्यूमेंट्स प्राप्त करना

# क्लाइंट सेट करें
from pymongo import MongoClient 
client = MongoClient()

# सभी डॉक्यूमेंट्स लौटाएँ client.film.movies.find()
# डॉट्स से बचें mov = client.film.movies
mov.find()
Python में MongoDB परिचय

Cursor से Python लिस्ट तक

# क्लाइंट सेट करें
from pymongo import MongoClient 
client = MongoClient()
mov = client.film.movies

# सभी डॉक्यूमेंट्स प्राप्त करें
mov.find()
<pymongo.synchronous.cursor.Cursor at 0x7f...760>
list(mov.find())
[{'_id': '68...ff', 'title': 'superbad', ...
  • Cursor = क्वेरी परिणामों का पॉइंटर
  • परिणाम एक-एक करके लाने देता है
  • यह नियंत्रण उपयोगी है
Python में MongoDB परिचय

एक नज़र डालें

# क्लाइंट सेट करें
from pymongo import MongoClient 
client = MongoClient()
mov = client.film.movies

# find(), cursor को list में बदलें
list(mov.find())
[
    {
        "_id": "6824bb...e53adbf274ff",
        "title": "superbad",
        "genre": ["comedy", "teen"],
        "release_year": 2007,
        "rating": 7.6
    },
    {
        "_id": "6824bb...e53adbf27500",
        "title": "interstellar",
        "genre": ["adventure", "drama", "sci-fi"],
        "release_year": 2014,
        "rating": 8.6,
        "won_oscar": True
    },
    ...
]
Python में MongoDB परिचय

क्वेरी फ़िल्टर्स

from pymongo import MongoClient 
client = MongoClient()
mov = client.film.movies

curs = mov.find({ "won_oscar": True }) list(curs)
[
    {
        '_id': '6824bbd05644e53adbf27500',
        'genre': ['adventure', 'drama', 'sci-fi'],
        'rating': 8.6,
        'release_year': 2014,
        'title': 'interstellar',
        'won_oscar': True
    },
     {
        '_id': '6824bbd05644e53adbf27501',
        'genre': ['action', 'sci-fi', 'thriller'],
        'rating': 8.8,
        'release_year': 2010,
        'title': 'inception',
        'won_oscar': True
    },
    ...
]
Python में MongoDB परिचय

.find_one() से विशेष रिकॉर्ड टार्गेट करें

from pymongo import MongoClient 

client = MongoClient()
mov = client.film.movies

mov.find_one({ "title": "parasite" })
{
    '_id': '6824bbd05644e53adbf27518',
    'genre': ['drama', 'thriller'],
    'rating': 8.5,
    'release_year': 2019,
    'title': 'parasite',
    'won_oscar': True
}
  • क्वेरी से मेल खाता पहला डॉक्यूमेंट लौटाता है
  • डॉक्यूमेंट लौटाता है, Cursor ऑब्जेक्ट नहीं
Python में MongoDB परिचय

अभ्यास करते हैं!

Python में MongoDB परिचय

Preparing Video For Download...