Python Kullanarak MongoDB Veri Tabanı Oluşturma

Merhabalar bu yazımda sizlere Python kullanarak MongoDB veri tabanı oluşturmayı göstereceğim. Bildiğimiz gibi MongoDB NoSQL bir veri tabanı ve kullanması da gayet kolay. Bu yüzden çokça tercih ediliyor. Python projelerinizde nasıl MongoDB kullanabileceğinize gelin birlikte göz atalım.

Python Kullanarak MongoDB Veri Tabanı Oluşturma

Öncelikle Python’da MongoDB ile bağlantı kurabilmemiz için Pymongo isimli modülü kullanacağız. Bu modülü kurmak için komut satırına aşağıdaki kodu yazabilirsiniz.

<!-- HTML generated using hilite.me --><div style="background: #f8f8f8; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><pre style="margin: 0; line-height: 125%"><span style="color: #000000">pip</span> <span style="color: #000000">install</span> <span style="color: #000000">pymongo</span>
</pre></div>

Eğer modül başarı ile kurulduysa projemize dahil edebiliriz. Bunun için metin editörümüze geliyoruz ve aşağıdaki kodu yazıyoruz.

<!-- HTML generated using hilite.me --><div style="background: #f8f8f8; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><pre style="margin: 0; line-height: 125%"><span style="color: #204a87; font-weight: bold">from</span> <span style="color: #000000">pymongo</span> <span style="color: #204a87; font-weight: bold">import</span> <span style="color: #000000">MongoClient</span>
</pre></div>

Bu kodla Pymongo içerisinde yer alan MongoClient isimli metodu projemize dahil ettik. Bu metodu kullanarak veri tabanımızı oluşturmamız gerekiyor.

<!-- HTML generated using hilite.me --><div style="background: #f8f8f8; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><pre style="margin: 0; line-height: 125%"><span style="color: #000000">client</span> <span style="color: #ce5c00; font-weight: bold">=</span> <span style="color: #000000">MongoClient</span><span style="color: #000000; font-weight: bold">(</span><span style="color: #4e9a06">'localhost'</span><span style="color: #000000; font-weight: bold">,</span> <span style="color: #0000cf; font-weight: bold">27017</span><span style="color: #000000; font-weight: bold">)</span>
</pre></div>

Veri tabanımızın adını giriyoruz ve ekrana veri tabanının oluşturulma işleminin başarıyla bittiğini anlatan bir yazı yazdırıyoruz.

<!-- HTML generated using hilite.me --><div style="background: #f8f8f8; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><pre style="margin: 0; line-height: 125%"><span style="color: #000000">db</span> <span style="color: #ce5c00; font-weight: bold">=</span> <span style="color: #000000">client</span><span style="color: #000000; font-weight: bold">[</span><span style="color: #4e9a06">'kod10]</span>
<span style="color: #204a87; font-weight: bold">print</span><span style="color: #000000; font-weight: bold">(</span><span style="color: #4e9a06">"Veri tabanı başarıyla oluşturuldu"</span><span style="color: #000000; font-weight: bold">)</span>
</pre></div>

Eğer veri tabanının oluşup oluşmadığını kontrol etmek istiyorsanız aşağıda yer alan kodları yazabilirsiniz.

<!-- HTML generated using hilite.me --><div style="background: #f8f8f8; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><pre style="margin: 0; line-height: 125%"><span style="color: #000000">list_of_db</span> <span style="color: #ce5c00; font-weight: bold">=</span> <span style="color: #000000">client</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">list_database_names</span><span style="color: #000000; font-weight: bold">()</span>
  
<span style="color: #204a87; font-weight: bold">if</span> <span style="color: #4e9a06">"kod10"</span> <span style="color: #204a87; font-weight: bold">in</span> <span style="color: #000000">list_of_db</span><span style="color: #000000; font-weight: bold">:</span>
    <span style="color: #204a87; font-weight: bold">print</span><span style="color: #000000; font-weight: bold">(</span><span style="color: #4e9a06">"Veri tabanı oluşturulmuş!"</span><span style="color: #000000; font-weight: bold">)</span>
</pre></div>

Kodların tamamı :

<!-- HTML generated using hilite.me --><div style="background: #f8f8f8; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><pre style="margin: 0; line-height: 125%"><span style="color: #204a87; font-weight: bold">from</span> <span style="color: #000000">pymongo</span> <span style="color: #204a87; font-weight: bold">import</span> <span style="color: #000000">MongoClient</span>

<span style="color: #000000">client</span> <span style="color: #ce5c00; font-weight: bold">=</span> <span style="color: #000000">MongoClient</span><span style="color: #000000; font-weight: bold">(</span><span style="color: #4e9a06">'localhost'</span><span style="color: #000000; font-weight: bold">,</span> <span style="color: #0000cf; font-weight: bold">27017</span><span style="color: #000000; font-weight: bold">)</span>

<span style="color: #000000">db</span> <span style="color: #ce5c00; font-weight: bold">=</span> <span style="color: #000000">client</span><span style="color: #000000; font-weight: bold">[</span><span style="color: #4e9a06">'kod10]</span>
<span style="color: #204a87; font-weight: bold">print</span><span style="color: #000000; font-weight: bold">(</span><span style="color: #4e9a06">"Veri tabanı başarıyla oluşturuldu"</span><span style="color: #000000; font-weight: bold">)</span>

<span style="color: #000000">list_of_db</span> <span style="color: #ce5c00; font-weight: bold">=</span> <span style="color: #000000">client</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">list_database_names</span><span style="color: #000000; font-weight: bold">()</span>
  
<span style="color: #204a87; font-weight: bold">if</span> <span style="color: #4e9a06">"kod10"</span> <span style="color: #204a87; font-weight: bold">in</span> <span style="color: #000000">list_of_db</span><span style="color: #000000; font-weight: bold">:</span>
    <span style="color: #204a87; font-weight: bold">print</span><span style="color: #000000; font-weight: bold">(</span><span style="color: #4e9a06">"Veri tabanı oluşturulmuş!"</span><span style="color: #000000; font-weight: bold">)</span>
</pre></div>

Böylelikle Python kullanarak MongoDB veri tabanı oluşturma yazımızın sonuna geldik. Yorum yapmayı ve yazımızı paylaşmayı unutmayın. İyi günler..

Yorum yapın