Tuesday, 12 May 2020

Python with Mongo db - Sample Programs

python -m pip install pymongo


import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")

print(myclient.list_database_names())
['admin', 'config', 'local']



dblist = myclient.list_database_names()
if "school" in dblist:
  print("The database exists.")
  
mydb = myclient["school"]

for p in dblist:
    print(p)
    
admin
config
local


 
 
#Display all the databases.
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
for p in myclient.list_database_names():
    print(p)
    
admin
config
local
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")

dblist = myclient.list_database_names()
if "school" in dblist:
  print("The database : {} exists.".format("school"))
    
The database : school exists.
#Display all the collections (tables) in a database:

import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["school"]
collist = mydb.list_collection_names()
for collectionName in collist:
    print(collectionName)
    
student
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["school"]
collist = mydb.list_collection_names()

if "student"  in collist:
        print("Collection student exists in school database")

Collection student exists in school database



#insert a document (Row) in a collection (table)
import pymongo

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

personDict = { "firstname":"Priya","lastname":"Balakumaran","city":"Kandanur" }

x = mycol.insert_one(personDict)
 
print(x.inserted_id) 


#insert multiple documents - Multi row insert
#autogenerated ids
import pymongo

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

mydocs = [
  { "firstname":"Ravi","lastname":"Rahul","city":"Pallathur"},
  { "firstname":"Siva","lastname":"Prasad","city":"Vadagudi"},
  { "firstname":"Arun","lastname":"Kumar","city":"Kanadukathan"},
  { "firstname":"Awesome","lastname":"Nator","city":"London"}
]

x = mycol.insert_many(mydocs)

#print list of the _id values of the inserted documents:
print(x.inserted_ids)


#insert multiple document with id specified

import pymongo

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

mydocs = [
  {"_id": 101,"firstname":"Kalai","lastname":"Selvi","city":"Aathalur"},
  {"_id": 102,"firstname":"Anna","lastname":"Malai","city":"Singapore"},
  {"_id": 103,"firstname":"Vanakkam","lastname":"da mappla","city":"Theni"},
  {"_id": 104 ,"firstname":"Anbu","lastname":"Sudha","city":"Bangalore"}
]

x = mycol.insert_many(mydocs)

#print list of the _id values of the inserted documents:
print(x.inserted_ids)


[101, 102, 103, 104]


#Search document in a collection

import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["school"]
mycol = mydb["student"]

x = mycol.find_one()
print(x)
{'_id': ObjectId('5eba5eff2a9545fa868c9aa8'), 'firstname': 'Raja', 'lastname': 'Raman', 'city': 'Kottaiyur'}



#find : Selct all : Select * from doc



import pymongo

myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["school"]
mycol = mydb["student"]
docs = mycol.find()
for d in docs:
    print (d)
{'_id': ObjectId('5eba5eff2a9545fa868c9aa8'), 'firstname': 'Raja', 'lastname': 'Raman', 'city': 'Kottaiyur'}
{'_id': ObjectId('5eba5f522a9545fa868c9aac'), 'firstname': 'Priya', 'lastname': 'Balakumaran', 'city': 'Kandanur'}
{'_id': ObjectId('5eba60012a9545fa868c9aae'), 'firstname': 'Ravi', 'lastname': 'Rahul', 'city': 'Pallathur'}
{'_id': ObjectId('5eba60012a9545fa868c9aaf'), 'firstname': 'Siva', 'lastname': 'Prasad', 'city': 'Vadagudi'}
{'_id': ObjectId('5eba60012a9545fa868c9ab0'), 'firstname': 'Arun', 'lastname': 'Kumar', 'city': 'Kanadukathan'}
{'_id': ObjectId('5eba60012a9545fa868c9ab1'), 'firstname': 'Awesome', 'lastname': 'Nator', 'city': 'London'}
{'_id': 101, 'firstname': 'Kalai', 'lastname': 'Selvi', 'city': 'Aathalur'}
{'_id': 102, 'firstname': 'Anna', 'lastname': 'Malai', 'city': 'Singapore'}
{'_id': 103, 'firstname': 'Vanakkam', 'lastname': 'da mappla', 'city': 'Theni'}
{'_id': 104, 'firstname': 'Anbu', 'lastname': 'Sudha', 'city': 'Bangalore'}



#display specific columns

import pymongo

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

for x in mycol.find({},{ "_id": 0, "firstname": 1, "lastname": 1, "city":1 }):
  print(x)
  
  
  {'firstname': 'Raja', 'lastname': 'Raman', 'city': 'Kottaiyur'}
{'firstname': 'Priya', 'lastname': 'Balakumaran', 'city': 'Kandanur'}
{'firstname': 'Ravi', 'lastname': 'Rahul', 'city': 'Pallathur'}
{'firstname': 'Siva', 'lastname': 'Prasad', 'city': 'Vadagudi'}
{'firstname': 'Arun', 'lastname': 'Kumar', 'city': 'Kanadukathan'}
{'firstname': 'Awesome', 'lastname': 'Nator', 'city': 'London'}
{'firstname': 'Kalai', 'lastname': 'Selvi', 'city': 'Aathalur'}
{'firstname': 'Anna', 'lastname': 'Malai', 'city': 'Singapore'}
{'firstname': 'Vanakkam', 'lastname': 'da mappla', 'city': 'Theni'}
{'firstname': 'Anbu', 'lastname': 'Sudha', 'city': 'Bangalore'}


#display only the last name:

import pymongo

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

for x in mycol.find({},{ "_id": 0,"city":0,"firstname":0  }):
  print(x)
  
  {'lastname': 'Raman'}
{'lastname': 'Balakumaran'}
{'lastname': 'Rahul'}
{'lastname': 'Prasad'}
{'lastname': 'Kumar'}
{'lastname': 'Nator'}
{'lastname': 'Selvi'}
{'lastname': 'Malai'}
{'lastname': 'da mappla'}
{'lastname': 'Sudha'}



#Except id and City:

import pymongo

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

for x in mycol.find({},{ "_id":0,"city":0 }):
  print(x)
  
  {'firstname': 'Raja', 'lastname': 'Raman'}
{'firstname': 'Priya', 'lastname': 'Balakumaran'}
{'firstname': 'Ravi', 'lastname': 'Rahul'}
{'firstname': 'Siva', 'lastname': 'Prasad'}
{'firstname': 'Arun', 'lastname': 'Kumar'}
{'firstname': 'Awesome', 'lastname': 'Nator'}
{'firstname': 'Kalai', 'lastname': 'Selvi'}
{'firstname': 'Anna', 'lastname': 'Malai'}
{'firstname': 'Vanakkam', 'lastname': 'da mappla'}
{'firstname': 'Anbu', 'lastname': 'Sudha'}


#Search specific city

import pymongo

myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["school"]
mycol = mydb["student"]
myquery = { "city": "London" }
mydoc = mycol.find(myquery)
for x in mydoc:
  print(x)
  
  {'_id': ObjectId('5eba60012a9545fa868c9ab1'), 'firstname': 'Awesome', 'lastname': 'Nator', 'city': 'London'}





#Search city >= "S"

import pymongo

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

myquery = { "city": { "$gt": "S" } }
mydoc = mycol.find(myquery)
for x in mydoc:
  print(x)
  
  
{'_id': ObjectId('5eba60012a9545fa868c9aaf'), 'firstname': 'Siva', 'lastname': 'Prasad', 'city': 'Vadagudi'}
{'_id': 102, 'firstname': 'Anna', 'lastname': 'Malai', 'city': 'Singapore'}
{'_id': 103, 'firstname': 'Vanakkam', 'lastname': 'da mappla', 'city': 'Theni'}


#Regular Explression. city starts with 'S'


import pymongo

myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["school"]
mycol = mydb["student"]
myquery = { "city": { "$regex": "S" } }
mydoc = mycol.find(myquery)
for x in mydoc:
  print(x)
  
  {'_id': 102, 'firstname': 'Anna', 'lastname': 'Malai', 'city': 'Singapore'}


#Order by City Ascending

import pymongo

myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["school"]
mycol = mydb["student"]
myquery = { "city": { "$regex": "S" } }
mydoc = mycol.find().sort("city")
for x in mydoc:
  print(x)
  
  
  {'_id': 101, 'firstname': 'Kalai', 'lastname': 'Selvi', 'city': 'Aathalur'}
{'_id': 104, 'firstname': 'Anbu', 'lastname': 'Sudha', 'city': 'Bangalore'}
{'_id': ObjectId('5eba60012a9545fa868c9ab0'), 'firstname': 'Arun', 'lastname': 'Kumar', 'city': 'Kanadukathan'}
{'_id': ObjectId('5eba5f522a9545fa868c9aac'), 'firstname': 'Priya', 'lastname': 'Balakumaran', 'city': 'Kandanur'}
{'_id': ObjectId('5eba5eff2a9545fa868c9aa8'), 'firstname': 'Raja', 'lastname': 'Raman', 'city': 'Kottaiyur'}
{'_id': ObjectId('5eba60012a9545fa868c9ab1'), 'firstname': 'Awesome', 'lastname': 'Nator', 'city': 'London'}
{'_id': ObjectId('5eba60012a9545fa868c9aae'), 'firstname': 'Ravi', 'lastname': 'Rahul', 'city': 'Pallathur'}
{'_id': 102, 'firstname': 'Anna', 'lastname': 'Malai', 'city': 'Singapore'}
{'_id': 103, 'firstname': 'Vanakkam', 'lastname': 'da mappla', 'city': 'Theni'}
{'_id': ObjectId('5eba60012a9545fa868c9aaf'), 'firstname': 'Siva', 'lastname': 'Prasad', 'city': 'Vadagudi'}




#Order by City descending

import pymongo

myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["school"]
mycol = mydb["student"]
myquery = { "city": { "$regex": "S" } }
mydoc = mycol.find().sort("city",-1)
for x in mydoc:
  print(x)



{'_id': ObjectId('5eba60012a9545fa868c9aaf'), 'firstname': 'Siva', 'lastname': 'Prasad', 'city': 'Vadagudi'}
{'_id': 103, 'firstname': 'Vanakkam', 'lastname': 'da mappla', 'city': 'Theni'}
{'_id': 102, 'firstname': 'Anna', 'lastname': 'Malai', 'city': 'Singapore'}
{'_id': ObjectId('5eba60012a9545fa868c9aae'), 'firstname': 'Ravi', 'lastname': 'Rahul', 'city': 'Pallathur'}
{'_id': ObjectId('5eba60012a9545fa868c9ab1'), 'firstname': 'Awesome', 'lastname': 'Nator', 'city': 'London'}
{'_id': ObjectId('5eba5eff2a9545fa868c9aa8'), 'firstname': 'Raja', 'lastname': 'Raman', 'city': 'Kottaiyur'}
{'_id': ObjectId('5eba5f522a9545fa868c9aac'), 'firstname': 'Priya', 'lastname': 'Balakumaran', 'city': 'Kandanur'}
{'_id': ObjectId('5eba60012a9545fa868c9ab0'), 'firstname': 'Arun', 'lastname': 'Kumar', 'city': 'Kanadukathan'}
{'_id': 104, 'firstname': 'Anbu', 'lastname': 'Sudha', 'city': 'Bangalore'}
{'_id': 101, 'firstname': 'Kalai', 'lastname': 'Selvi', 'city': 'Aathalur'}


#Delete single doc

import pymongo

myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["school"]
mycol = mydb["student"]
myquery = { "city": "London" }
mycol.delete_one(myquery)



#Delete multiple docs

import pymongo

myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["school"]
mycol = mydb["student"]
myquery = { "city": { "$gt": "S" } }  # city >= "S"
x = mycol.delete_many(myquery)
print(x.deleted_count, " documents deleted.")

3  documents deleted.



#Delete all docs / rows in a collection

import pymongo

myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["school"]
mycol = mydb["student"]
myquery = { "city": { "$gt": "S" } }
x = mycol.delete_many({})
print(x.deleted_count, " documents deleted.")

6  documents deleted.


#Drop a collection

import pymongo

myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["school"]
mycol = mydb["student"]
mycol.drop()




#Adding documents again

import pymongo

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

mydocs = [
  {"_id": 101,"firstname":"Kalai","lastname":"Selvi","city":"Aathalur"},
  {"_id": 102,"firstname":"Anna","lastname":"Malai","city":"Singapore"},
  {"_id": 103,"firstname":"Vanakkam","lastname":"da mappla","city":"Theni"},
  {"_id": 104 ,"firstname":"Anbu","lastname":"Sudha","city":"Bangalore"}
]

x = mycol.insert_many(mydocs)

#print list of the _id values of the inserted documents:
print(x.inserted_ids)


import pymongo

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

mydocs = [
  { "firstname":"Ravi","lastname":"Rahul","city":"Pallathur"},
  { "firstname":"Siva","lastname":"Prasad","city":"Vadagudi"},
  { "firstname":"Arun","lastname":"Kumar","city":"Kanadukathan"},
  { "firstname":"Awesome","lastname":"Nator","city":"London"}
]

x = mycol.insert_many(mydocs)

#print list of the _id values of the inserted documents:
print(x.inserted_ids)


#limit example.. display 3 records


import pymongo

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

myresult = mycol.find().limit(3)

#print the result:
for x in myresult:
  print(x)
  
  {'_id': ObjectId('5eba65732a9545fa868c9acf'), 'firstname': 'Ravi', 'lastname': 'Rahul', 'city': 'Pallathur'}
{'_id': ObjectId('5eba65732a9545fa868c9ad0'), 'firstname': 'Siva', 'lastname': 'Prasad', 'city': 'Vadagudi'}
{'_id': ObjectId('5eba65732a9545fa868c9ad1'), 'firstname': 'Arun', 'lastname': 'Kumar', 'city': 'Kanadukathan'}


#Update one record - Single record update
#London to Pudukkottai

import pymongo

myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["school"]
mycol = mydb["student"]
myquery = { "city": "London" }
newvalues = { "$set": { "city": "Pudukkottai" } }
mycol.update_one(myquery, newvalues)

for x in mycol.find():
  print(x)


{'_id': ObjectId('5eba65732a9545fa868c9acf'), 'firstname': 'Ravi', 'lastname': 'Rahul', 'city': 'Pallathur'}
{'_id': ObjectId('5eba65732a9545fa868c9ad0'), 'firstname': 'Siva', 'lastname': 'Prasad', 'city': 'Vadagudi'}
{'_id': ObjectId('5eba65732a9545fa868c9ad1'), 'firstname': 'Arun', 'lastname': 'Kumar', 'city': 'Kanadukathan'}
{'_id': ObjectId('5eba65732a9545fa868c9ad2'), 'firstname': 'Awesome', 'lastname': 'Nator', 'city': 'Pudukkottai'}
{'_id': 101, 'firstname': 'Kalai', 'lastname': 'Selvi', 'city': 'Aathalur'}
{'_id': 102, 'firstname': 'Anna', 'lastname': 'Malai', 'city': 'Singapore'}
{'_id': 103, 'firstname': 'Vanakkam', 'lastname': 'da mappla', 'city': 'Theni'}
{'_id': 104, 'firstname': 'Anbu', 'lastname': 'Sudha', 'city': 'Bangalore'}


#Update multiple documents

import pymongo

myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["school"]
mycol = mydb["student"]
myquery = { "city": { "$gt": "S" } }
newvalues = { "$set": { "city": "Pillaiyarpatti" } }
x = mycol.update_many(myquery, newvalues)
print(x.modified_count, "documents updated.")


2 documents updated.

No comments:

Post a Comment

Flume - Simple Demo

// create a folder in hdfs : $ hdfs dfs -mkdir /user/flumeExa // Create a shell script which generates : Hadoop in real world <n>...