MongoDB with Python - pyMongo - Basic II

In [1]:
from pymongo import MongoClient, ASCENDING, DESCENDING
import pprint
import random
In [2]:
con = MongoClient("mongodb://mdbuser:[email protected]:27017,cluster0-shard-00-01-mswpe.mongodb.net:27017,\
cluster0-shard-00-02-mswpe.mongodb.net:27017/Cluster0?ssl=true&replicaSet=Cluster0-shard-0&authSource=admin&retryWrites=true")
con.list_database_names()
Out[2]:
['Cluster0', 'admin', 'local']
In [3]:
db = con.Cluster0
db.list_collection_names()
Out[3]:
['user', 'movies_scratch', 'testidx', 'movies_initial']

Delete all Toronto City Data

In [4]:
db.user.delete_many({'City':'Toronto'})
Out[4]:
<pymongo.results.DeleteResult at 0x2ca7bc13cc8>
In [5]:
# any dictionary can be stored or inserted

user_1 = {'Name':'Atul', 'Age':28, 'Job':'DA', 'City':'Toronto'}
user_2 = {'Name':'Ashley', 'Age':25, 'Job':'Student', 'City':'Toronto'}
user_3 = {'Name':'Akash', 'Age':26, 'Job':'Doctor', 'City':'Toronto'}
user_4 = {'Name':'Vishwas', 'Age':37, 'Job':'Painter', 'City':'Toronto'}
user_5 = {'Name':'Bhusan', 'Age':64, 'Job':'Retired', 'City':'Toronto'}
user_6 = {'Name':'Rajesh', 'Age':58, 'Job':'Teacher', 'City':'Toronto'}
user_7 = {'Name':'Priya', 'Age':38, 'Job':'HouseWife', 'City':'Toronto'}
user_8 = {'Name':'Rakhi', 'Age':42, 'Job':'Engineer', 'City':'Toronto'}
user_9 = {'Name':'Divya', 'Age':34, 'Job':'Singer', 'City':'Toronto'}
user_10 = {'Name':'Vishal', 'Age':69, 'Job':'Teacher', 'City':'Toronto'}

insert_many

In [6]:
user_data = [user_1, user_2,user_3,user_4,user_5,user_6,user_7,user_8,user_9,user_10]
In [7]:
print(db.user.count_documents({}))
db.user.insert_many(user_data)
print(db.user.count_documents({}))
12
22
In [8]:
print(db.user.count_documents({'City':'Toronto'}))
10

Bulk Insert : insert_many

In [9]:
Jobs = ['Teacher', 'Nanny', 'Generator', 'BellBoy', 'Cashier', 'Manager', 'Physicist', 'Bio-tech', 'Student']
Gender = ['M', 'F', 'U']

print(db.user.count_documents({'City':'Toronto'}))
db.user.insert_many([{'name': 'User'+str(i), 'Age':random.randint(0, 120), 'Gender': random.choice(Gender), 'City':'Toronto', 'Job':random.choice(Jobs)} \
                     for i in range(50)])#.inserted_ids
print(db.user.count_documents({'City':'Toronto'}))
10
60

$find

In [10]:
pprint.pprint(list(db.user.find({'Job':'Teacher'})))
[{'Age': 58,
  'City': 'Toronto',
  'Job': 'Teacher',
  'Name': 'Rajesh',
  '_id': ObjectId('5c26b1af802bd91b0058c21e')},
 {'Age': 69,
  'City': 'Toronto',
  'Job': 'Teacher',
  'Name': 'Vishal',
  '_id': ObjectId('5c26b1af802bd91b0058c222')},
 {'Age': 16,
  'City': 'Toronto',
  'Gender': 'M',
  'Job': 'Teacher',
  '_id': ObjectId('5c26b1af802bd91b0058c234'),
  'name': 'User17'},
 {'Age': 70,
  'City': 'Toronto',
  'Gender': 'U',
  'Job': 'Teacher',
  '_id': ObjectId('5c26b1af802bd91b0058c23f'),
  'name': 'User28'},
 {'Age': 2,
  'City': 'Toronto',
  'Gender': 'U',
  'Job': 'Teacher',
  '_id': ObjectId('5c26b1af802bd91b0058c244'),
  'name': 'User33'},
 {'Age': 100,
  'City': 'Toronto',
  'Gender': 'U',
  'Job': 'Teacher',
  '_id': ObjectId('5c26b1af802bd91b0058c24c'),
  'name': 'User41'},
 {'Age': 3,
  'City': 'Toronto',
  'Gender': 'F',
  'Job': 'Teacher',
  '_id': ObjectId('5c26b1af802bd91b0058c24d'),
  'name': 'User42'},
 {'Age': 99,
  'City': 'Toronto',
  'Gender': 'M',
  'Job': 'Teacher',
  '_id': ObjectId('5c26b1af802bd91b0058c251'),
  'name': 'User46'},
 {'Age': 44,
  'City': 'Toronto',
  'Gender': 'U',
  'Job': 'Teacher',
  '_id': ObjectId('5c26b1af802bd91b0058c253'),
  'name': 'User48'}]
In [11]:
pprint.pprint(db.user.find_one({'Job':'Teacher'}))
{'Age': 58,
 'City': 'Toronto',
 'Job': 'Teacher',
 'Name': 'Rajesh',
 '_id': ObjectId('5c26b1af802bd91b0058c21e')}

\$lt , \$lte , \$gt and \$gte

In [12]:
pprint.pprint(db.user.find({'Job':'Teacher', 'Age':{'$lt':40}}))
<pymongo.cursor.Cursor object at 0x000002CA7BC40C18>
In [13]:
pprint.pprint(list(db.user.find({'Job':'Teacher', 'Age':{'$lt':40}})))
[{'Age': 16,
  'City': 'Toronto',
  'Gender': 'M',
  'Job': 'Teacher',
  '_id': ObjectId('5c26b1af802bd91b0058c234'),
  'name': 'User17'},
 {'Age': 2,
  'City': 'Toronto',
  'Gender': 'U',
  'Job': 'Teacher',
  '_id': ObjectId('5c26b1af802bd91b0058c244'),
  'name': 'User33'},
 {'Age': 3,
  'City': 'Toronto',
  'Gender': 'F',
  'Job': 'Teacher',
  '_id': ObjectId('5c26b1af802bd91b0058c24d'),
  'name': 'User42'}]
In [14]:
pprint.pprint(list(db.user.find({'Job':'Teacher', 'Age':{'$gte':40}})))
[{'Age': 58,
  'City': 'Toronto',
  'Job': 'Teacher',
  'Name': 'Rajesh',
  '_id': ObjectId('5c26b1af802bd91b0058c21e')},
 {'Age': 69,
  'City': 'Toronto',
  'Job': 'Teacher',
  'Name': 'Vishal',
  '_id': ObjectId('5c26b1af802bd91b0058c222')},
 {'Age': 70,
  'City': 'Toronto',
  'Gender': 'U',
  'Job': 'Teacher',
  '_id': ObjectId('5c26b1af802bd91b0058c23f'),
  'name': 'User28'},
 {'Age': 100,
  'City': 'Toronto',
  'Gender': 'U',
  'Job': 'Teacher',
  '_id': ObjectId('5c26b1af802bd91b0058c24c'),
  'name': 'User41'},
 {'Age': 99,
  'City': 'Toronto',
  'Gender': 'M',
  'Job': 'Teacher',
  '_id': ObjectId('5c26b1af802bd91b0058c251'),
  'name': 'User46'},
 {'Age': 44,
  'City': 'Toronto',
  'Gender': 'U',
  'Job': 'Teacher',
  '_id': ObjectId('5c26b1af802bd91b0058c253'),
  'name': 'User48'}]

\$in and \$nin

In [15]:
pprint.pprint(list(db.user.find({'Job':{'$in':['Teacher', 'BellBoy']}, 'Age':{'$lte':40}})))
[{'Age': 16,
  'City': 'Toronto',
  'Gender': 'M',
  'Job': 'Teacher',
  '_id': ObjectId('5c26b1af802bd91b0058c234'),
  'name': 'User17'},
 {'Age': 25,
  'City': 'Toronto',
  'Gender': 'F',
  'Job': 'BellBoy',
  '_id': ObjectId('5c26b1af802bd91b0058c23b'),
  'name': 'User24'},
 {'Age': 2,
  'City': 'Toronto',
  'Gender': 'U',
  'Job': 'Teacher',
  '_id': ObjectId('5c26b1af802bd91b0058c244'),
  'name': 'User33'},
 {'Age': 3,
  'City': 'Toronto',
  'Gender': 'F',
  'Job': 'Teacher',
  '_id': ObjectId('5c26b1af802bd91b0058c24d'),
  'name': 'User42'}]

\$and and \$or

In [16]:
pprint.pprint(list(db.user.find({'Job':{'$in':['Generator', 'BellBoy']}, 'Age':{'$lte':40}})))  # $and operation
[{'Age': 36,
  'City': 'Toronto',
  'Gender': 'F',
  'Job': 'Generator',
  '_id': ObjectId('5c26b1af802bd91b0058c227'),
  'name': 'User4'},
 {'Age': 25,
  'City': 'Toronto',
  'Gender': 'F',
  'Job': 'BellBoy',
  '_id': ObjectId('5c26b1af802bd91b0058c23b'),
  'name': 'User24'}]
In [17]:
pprint.pprint(list(db.user.find({ '$and': [{'Job':{'$in':['Generator', 'BellBoy']}}, {'Age':{'$gt':100}}]})))  # $and operation
[{'Age': 104,
  'City': 'Toronto',
  'Gender': 'F',
  'Job': 'BellBoy',
  '_id': ObjectId('5c26b1af802bd91b0058c23e'),
  'name': 'User27'}]
In [18]:
pprint.pprint(list(db.user.find({ '$or': [{'Job':{'$in':['Generator', 'BellBoy']}}, {'Age':{'$gte':110}}]})))  # $and operation
[{'Age': 36,
  'City': 'Toronto',
  'Gender': 'F',
  'Job': 'Generator',
  '_id': ObjectId('5c26b1af802bd91b0058c227'),
  'name': 'User4'},
 {'Age': 77,
  'City': 'Toronto',
  'Gender': 'F',
  'Job': 'BellBoy',
  '_id': ObjectId('5c26b1af802bd91b0058c228'),
  'name': 'User5'},
 {'Age': 117,
  'City': 'Toronto',
  'Gender': 'U',
  'Job': 'Cashier',
  '_id': ObjectId('5c26b1af802bd91b0058c22e'),
  'name': 'User11'},
 {'Age': 89,
  'City': 'Toronto',
  'Gender': 'U',
  'Job': 'BellBoy',
  '_id': ObjectId('5c26b1af802bd91b0058c231'),
  'name': 'User14'},
 {'Age': 99,
  'City': 'Toronto',
  'Gender': 'U',
  'Job': 'Generator',
  '_id': ObjectId('5c26b1af802bd91b0058c233'),
  'name': 'User16'},
 {'Age': 86,
  'City': 'Toronto',
  'Gender': 'U',
  'Job': 'Generator',
  '_id': ObjectId('5c26b1af802bd91b0058c235'),
  'name': 'User18'},
 {'Age': 25,
  'City': 'Toronto',
  'Gender': 'F',
  'Job': 'BellBoy',
  '_id': ObjectId('5c26b1af802bd91b0058c23b'),
  'name': 'User24'},
 {'Age': 64,
  'City': 'Toronto',
  'Gender': 'F',
  'Job': 'Generator',
  '_id': ObjectId('5c26b1af802bd91b0058c23c'),
  'name': 'User25'},
 {'Age': 104,
  'City': 'Toronto',
  'Gender': 'F',
  'Job': 'BellBoy',
  '_id': ObjectId('5c26b1af802bd91b0058c23e'),
  'name': 'User27'},
 {'Age': 120,
  'City': 'Toronto',
  'Gender': 'F',
  'Job': 'Nanny',
  '_id': ObjectId('5c26b1af802bd91b0058c242'),
  'name': 'User31'},
 {'Age': 59,
  'City': 'Toronto',
  'Gender': 'U',
  'Job': 'BellBoy',
  '_id': ObjectId('5c26b1af802bd91b0058c249'),
  'name': 'User38'},
 {'Age': 68,
  'City': 'Toronto',
  'Gender': 'F',
  'Job': 'BellBoy',
  '_id': ObjectId('5c26b1af802bd91b0058c24a'),
  'name': 'User39'}]

limit and skip

In [19]:
pprint.pprint(len(list(db.user.find({'Job':'Teacher'}))))
9
In [20]:
pprint.pprint(list(db.user.find({'Job':'Teacher'}).limit(2)))
[{'Age': 58,
  'City': 'Toronto',
  'Job': 'Teacher',
  'Name': 'Rajesh',
  '_id': ObjectId('5c26b1af802bd91b0058c21e')},
 {'Age': 69,
  'City': 'Toronto',
  'Job': 'Teacher',
  'Name': 'Vishal',
  '_id': ObjectId('5c26b1af802bd91b0058c222')}]
In [21]:
pprint.pprint(list(db.user.find({'Job':'Teacher'}).skip(2).limit(3)))
[{'Age': 16,
  'City': 'Toronto',
  'Gender': 'M',
  'Job': 'Teacher',
  '_id': ObjectId('5c26b1af802bd91b0058c234'),
  'name': 'User17'},
 {'Age': 70,
  'City': 'Toronto',
  'Gender': 'U',
  'Job': 'Teacher',
  '_id': ObjectId('5c26b1af802bd91b0058c23f'),
  'name': 'User28'},
 {'Age': 2,
  'City': 'Toronto',
  'Gender': 'U',
  'Job': 'Teacher',
  '_id': ObjectId('5c26b1af802bd91b0058c244'),
  'name': 'User33'}]

Sorting in Ascending - Python way

In [22]:
pprint.pprint(list(db.user.find({'Job':'Teacher'}).sort('Age').skip(2).limit(3)))
[{'Age': 16,
  'City': 'Toronto',
  'Gender': 'M',
  'Job': 'Teacher',
  '_id': ObjectId('5c26b1af802bd91b0058c234'),
  'name': 'User17'},
 {'Age': 44,
  'City': 'Toronto',
  'Gender': 'U',
  'Job': 'Teacher',
  '_id': ObjectId('5c26b1af802bd91b0058c253'),
  'name': 'User48'},
 {'Age': 58,
  'City': 'Toronto',
  'Job': 'Teacher',
  'Name': 'Rajesh',
  '_id': ObjectId('5c26b1af802bd91b0058c21e')}]
In [23]:
pprint.pprint(list(db.user.find({'Job':'Teacher'}).sort('Age', DESCENDING).skip(2).limit(3)))
[{'Age': 70,
  'City': 'Toronto',
  'Gender': 'U',
  'Job': 'Teacher',
  '_id': ObjectId('5c26b1af802bd91b0058c23f'),
  'name': 'User28'},
 {'Age': 69,
  'City': 'Toronto',
  'Job': 'Teacher',
  'Name': 'Vishal',
  '_id': ObjectId('5c26b1af802bd91b0058c222')},
 {'Age': 58,
  'City': 'Toronto',
  'Job': 'Teacher',
  'Name': 'Rajesh',
  '_id': ObjectId('5c26b1af802bd91b0058c21e')}]
In [ ]: