Python Mongodb Query

This article explains about , Pymongo queries

Hi all. Today We will see Mongo db  queries with Pymongo.

If you don't have mongo db in you system refer here

Let's create virtualenv for this experiment by

Step 0: Create Virtualenv and install Packages:

virtualenv envmongo

source envmongo/bin/activate

pip install pymongo

Step 2: Simple python script to connect to mongodb using pymongo

save this as mongoscript.py


from pymongo import MongoClient

# connection statement to mongodb

db = MongoClient(host="localhost", port=27017).mydb

# Insert Something into db

db.names.insert({"name": "balaji"})
db.names.insert({"name": "hari"})
db.names.insert({"name": "naveen"})

# Find command in pymongo

dbobj = db.names.find_one({"name": "balaji"})

print "document find by balaji", dbobj

# update document in pymongo

db.names.update({"name": "balaji"}, {"$set": {"country": "India"}}, upsert=False, multi=False)

print "update document by adding age to balaji", db.names.find_one({"name": "balaji"})

# delete in pymongo
print "Delete Document name by hari", db.names.remove({"name": "hari"})

# drop collection in pymongo
print "Drop entire collection names", db.drop_collection('names')

Step 3: Run Script By

$ python mongoscript.py

Output: 
document find by balaji {u'_id': ObjectId('58b3f0d99a5aab4b16e34418'), u'name': u'balaji'}
update document by adding age to balaji {u'country': u'India', u'_id': ObjectId('58b3f0d99a5aab4b16e34418'), u'name': u'balaji'}
Delete Document name by hari {u'ok': 1, u'n': 1}
Drop entire collection `names` {u'ns': u'mydb.names', u'ok': 1.0, u'nIndexesWas': 1}

Step 4: Line by Line Explanation in Script

from pymongo import MongoClient

we are importing MongoClient from pymongo package in order to make connection with mongo db

 db = MongoClient(host="localhost", port=27017).mydb

In this line, we are making connection to mongo db.

 host-> localhost ,  port = 27017 -> Mongo db's default port, mydb -> My database name,

mongo db automatically creates db by name

Pymongo Insert Query:

db.names.insert({"name": "balaji"})
db.names.insert({"name": "hari"})
db.names.insert({"name": "naveen"})

In these lines, we are inserting documents into names collection under mydb database. Here names is mycollection name.

Find Query in Pymongo:

  dbobj = db.names.find_one({"name": "balaji"})

We are finding one document named by  balaji. suppose we want all docs named balaji then query would be   db.names.find({"names": "balaji"})

Update query in Pymongo:

   db.names.update({"name": "balaji"}, {"$set": {"country": "India"}}, upsert=False, multi=False)

Here we are adding one field to document named balaji . Basically $set is used to update field values in mongo db documents.

Remove query by a filed value in Pymongo:

    db.names.remove({"name": "hari"})

Here we are deleting docs which are named  hari

Drop Table query in Pymongo: 

  db.drop_collection('names')

Here we are deleting entire collection named  names.

That's it. Thanks for Reading . Happy Coding !

Leave a comment

(Note: Comments are moderated)