1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 | # %% # Import the needed libraries import requests from pymongo import MongoClient # %% # Setup the connection to the mongodb server mongodb_client = MongoClient('10.0.0.120', 27017) mongodb_client # %% # Ping mongodb_client.admin.command('ping') # %% # List the databases on the server mongodb_client.list_database_names() # %% # List all collections in the securitynik database securitynik_db = mongodb_client['securitynik-mongo-db'] securitynik_db.list_collection_names() # %% # Get the db host information securitynik_db.HOST # %% # Get the column names for the employees collections securitynik_db.employees.find_one().keys() # %% # Create a collection wnamed employees within the employee_db employees = securitynik_db.employees # Create a collection named user users = securitynik_db.users securitynik_db, employees, users # %% employee = { 'fname' : 'Nik', 'lname' : 'Alleyne', 'Active' : 'True', 'profession' : 'Blogger' } # %% # Add a record to the employees_db employees.insert_one(employee) # %% # Read the employee collection and print the first record employees.find_one() # %% insert_many = [ {'user':'NA', 'blog':'www.blogspot.com', 'age':10, 'sex':'Female'}, {'user':'NA', 'blog':'www.blogspot.com', 'age':20, 'sex':'Male'}, {'user':'SA', 'blog':'www.blogspot.com', 'age':30, 'sex':'Female'}, {'user':'TQ', 'blog':'www.blogspot.com', 'age':40, 'sex':'Male'}, {'user':'DP', 'blog':'www.blogspot.com', 'age':50, 'sex':'Female'}, {'user':'TA', 'blog':'www.blogspot.com', 'age':60, 'sex':'Male'}, {'user':'PK', 'blog':'www.blogspot.com', 'age':70, 'sex':'Female'}, {'user':'User-1', 'blog':'www.blogspot.com', 'age':80, 'sex':'Male'}, {'user':'User-2', 'blog':'www.blogspot.com', 'age':90, 'sex':'Female'}, {'user':'User-3', 'blog':'www.blogspot.com', 'age':1000, 'sex':'Male'}, ] # %% # Insert the above records employees.insert_many(insert_many) # %% # Leveraging the query operators for document in securitynik_db.employees.find({}): print(document) # %% # Count the number of documents in the users and employees table securitynik_db.user.count_documents({}), securitynik_db.employees.count_documents(filter={}) # %% # Find one record securitynik_db.employees.find_one(filter={}) # %% # Count all records where the sex is male securitynik_db.employees.count_documents({'sex':'Male'}) # %% # Count all records where the sex is Female securitynik_db.employees.count_documents({'sex':'Female'}) # %% # Combining the filter to look for a more targeted record securitynik_db.employees.count_documents({'sex':'Female', 'age':90, 'user':'User-2'}) # %% # Find the record that matches the above criterion securitynik_db.employees.find_one({'sex':'Female', 'age':90, 'user':'User-2'}) # %% # Leveraging the query to find all records where the age is equal to 20 for document in securitynik_db.employees.find({ 'age' : { '$eq':20 } }): print(document) # %% # Leveraging the query to find all records where the age is less than 20 for document in securitynik_db.employees.find({ 'age' : { '$lt':20 } }): print(document) # %% # Leveraging the query to find all records where the age is greater than 20 for document in securitynik_db.employees.find({ 'age' : { '$gt':80 } }): print(document) # %% # Leveraging the query to find all records within an array for document in securitynik_db.employees.find({ 'age' : { '$in':[90, 1000] } }): print(document) # %% # Leveraging the query to find all records not within an array for document in securitynik_db.employees.find({ 'age' : { '$nin':[90, 1000] } }): print(document) # %% # Leveraging the query to find all records where the blogsOn field exists for document in securitynik_db.employees.find({ 'blogsOn' : { '$exists': 'true' } }): print(document) # %% # Leveraging the query to find all records where the blogsOn field exists and has 2 or more entries # in the blogsOn Field for document in securitynik_db.employees.find({ 'blogsOn.1' : { '$exists': 'true' } }): print(document) # %% # finding all the unique values for the blogsOn field securitynik_db.employees.distinct('blogsOn') # %% # finding all the unique values for the sex field securitynik_db.employees.distinct('sex') # %% # finding all the unique values for the user field in the users collection securitynik_db.user.distinct('user') # %% # Using distinct with regular expressions # Looking for all records where the user starts with # Nak, Ney, Pam or contains the characters sadi and ends with Alleyne # While ignorning case sensitivity securitynik_db.user.distinct('user', { 'user' : {'$regex' : '^(Nak\w+|Ney|Pam|[sadi]).*alleyne$', '$options': 'i'} }) # %% # Leveraging projections to find the fields of interest # Then return only the columns of interest # 1 means return the field, id is returned by default, # so using 0 to suppress it. list(securitynik_db.employees.find(filter={}, projection={'_id':0, 'fname':1, 'lname':1})) # %% # Expanding on above to sort the results sorted_docs = securitynik_db.employees.find({}, sort=[('age', -1)]) list(sorted_docs) # %% # Expanding on above to sort the results sorted_docs = securitynik_db.employees.find({'fname':'Nik'}, sort=[('_id', -1)]) list(sorted_docs) # %% # Limiting the number of returned record sorted_docs = securitynik_db.employees.find({'fname':'Nik'}, sort=[('_id', -1)], limit=1) list(sorted_docs) # %% # Limiting the number of returned record while taking advantage of skip sorted_docs = securitynik_db.employees.find({'fname':'Nik'}, sort=[('_id', -1)], skip=1, limit=1) list(sorted_docs) # %% # Creating an index and leveraging same securitynik_db.employees.create_index([('age', -1)]) # %% # Leverage the index # Creating an index and leveraging same list(securitynik_db.employees.find({'age': -1})) # %% # Create a compound index securitynik_db.employees.create_index([('age', -1), ('fname', -1)]) # %% # Grabbing initial information on the index securitynik_db.employees.index_information() # %% # Getting an explanation about my query securitynik_db.employees.find({'fname':'Nik', '_id': '6263735f43142a78a4071445'}).explain() # %% # Using aggregate feature # doing most of the work at the server list(securitynik_db.employees.aggregate([{'$match' : { 'fname' : 'Nik'}}, {'$project' : {'lname' : 3, 'fname':1, '_id':0 }}, { '$limit' : 3}])) # %% # Using aggregate feature # doing most of the work at the server # count the number of returned records list(securitynik_db.employees.aggregate([{'$match' : { 'fname' : 'Nik'}}, {'$project' : {'lname' : 3, 'fname':1, '_id':0 }}, { '$limit' : 3}, {'$count' : 'fname'}])) # %% # %% # %% ''' References: https://hevodata.com/learn/python-pymongo-mongoclient/ https://campus.datacamp.com/courses/introduction-to-using-mongodb-for-data-science-with-python/ https://www.linkedin.com/pulse/mongodb-101-beginners-part1-amany-mounes/ https://www.mongodb.com/docs/manual/tutorial/query-documents/#std-label-read-operations-query-argument https://www.tutorialspoint.com/python_mongodb/python_mongodb_tutorial.pdf https://www.bogotobogo.com/python/MongoDB_PyMongo/python_MongoDB_pyMongo_tutorial_connecting_accessing.php https://pymongo.readthedocs.io/en/stable/api/pymongo/mongo_client.html https://www.mongodb.com/docs/manual/reference/operator/query/ https://www.educba.com/mongodb-query-operators/ https://www.w3schools.in/mongodb/projection-queries ''' |
Thursday, April 28, 2022
Beginning MongoDB - MongoClient
To see the full notebook, check out my GitHub site.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment