MQL – Delete Collection Update document(s) Limit the Results

Delete Collection

You can delete a table, or collection as it is called in MongoDB, by using the drop() method.

In [ ]:

#Delete the "customers" collection:  - Not run

import pymongo

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

mycol.drop()

Update Collection You can update a record, or document as it is called in MongoDB, by using the update_one() method. The first parameter of the update_one() method is a query object defining which document to update.

In [2]:

#Change the address from "Valley 345" to "Canyon 123":

import pymongo

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

myquery = { "address": "Valley 345" }
newvalues = { "$set": { "address": "Canyon 123" } }

mycol.update_one(myquery, newvalues)

#print "customers" after the update:
for x in mycol.find():
  print(x)
{'_id': ObjectId('61385e5156e0542d940db712'), 'name': 'John', 'address': 'Highway 37'}
{'_id': ObjectId('61385fc856e0542d940db714'), 'name': 'Peter', 'address': 'Lowstreet 27'}
{'_id': ObjectId('6138618356e0542d940db716'), 'name': 'Amy', 'address': 'Apple st 652'}
{'_id': ObjectId('6138618356e0542d940db718'), 'name': 'Michael', 'address': 'Canyon 123'}
{'_id': ObjectId('6138618356e0542d940db719'), 'name': 'Sandy', 'address': 'Ocean blvd 2'}
{'_id': ObjectId('6138618356e0542d940db71a'), 'name': 'Betty', 'address': 'Green Grass 1'}
{'_id': ObjectId('6138618356e0542d940db71b'), 'name': 'Richard', 'address': 'Sky st 331'}
{'_id': ObjectId('6138618356e0542d940db71c'), 'name': 'Susan', 'address': 'One way 98'}
{'_id': ObjectId('6138618356e0542d940db71d'), 'name': 'Vicky', 'address': 'Yellow Garden 2'}
{'_id': ObjectId('6138618356e0542d940db71e'), 'name': 'Ben', 'address': 'Park Lane 38'}
{'_id': ObjectId('6138618356e0542d940db71f'), 'name': 'William', 'address': 'Central st 954'}
{'_id': ObjectId('6138618356e0542d940db720'), 'name': 'Chuck', 'address': 'Main Road 989'}
{'_id': ObjectId('6138618356e0542d940db721'), 'name': 'Viola', 'address': 'Sideway 1633'}
{'_id': 1, 'name': 'John', 'address': 'Highway 37'}
{'_id': 2, 'name': 'Peter', 'address': 'Lowstreet 27'}
{'_id': 3, 'name': 'Amy', 'address': 'Apple st 652'}
{'_id': 4, 'name': 'Hannah', 'address': 'Mountain 21'}
{'_id': 5, 'name': 'Michael', 'address': 'Valley 345'}
{'_id': 6, 'name': 'Sandy', 'address': 'Ocean blvd 2'}
{'_id': 7, 'name': 'Betty', 'address': 'Green Grass 1'}
{'_id': 8, 'name': 'Richard', 'address': 'Sky st 331'}
{'_id': 9, 'name': 'Susan', 'address': 'One way 98'}
{'_id': 10, 'name': 'Vicky', 'address': 'Yellow Garden 2'}
{'_id': 11, 'name': 'Ben', 'address': 'Park Lane 38'}
{'_id': 12, 'name': 'William', 'address': 'Central st 954'}
{'_id': 13, 'name': 'Chuck', 'address': 'Main Road 989'}
{'_id': 14, 'name': 'Viola', 'address': 'Sideway 1633'}
{'_id': ObjectId('6138663a6461436a279a77d2'), 'name': 'Amy', 'address': 'Apple st 652'}
{'_id': ObjectId('6138663a6461436a279a77d3'), 'name': 'Hannah', 'address': 'Mountain 21'}
{'_id': ObjectId('6138663a6461436a279a77d4'), 'name': 'Michael', 'address': 'Valley 345'}
{'_id': ObjectId('6138663a6461436a279a77d5'), 'name': 'Sandy', 'address': 'Ocean blvd 2'}
{'_id': ObjectId('6138663a6461436a279a77d6'), 'name': 'Betty', 'address': 'Green Grass 1'}
{'_id': ObjectId('6138663a6461436a279a77d7'), 'name': 'Richard', 'address': 'Sky st 331'}
{'_id': ObjectId('6138663a6461436a279a77d8'), 'name': 'Susan', 'address': 'One way 98'}
{'_id': ObjectId('6138663a6461436a279a77d9'), 'name': 'Vicky', 'address': 'Yellow Garden 2'}
{'_id': ObjectId('6138663a6461436a279a77da'), 'name': 'Ben', 'address': 'Park Lane 38'}
{'_id': ObjectId('6138663a6461436a279a77db'), 'name': 'William', 'address': 'Central st 954'}
{'_id': ObjectId('6138663a6461436a279a77dc'), 'name': 'Chuck', 'address': 'Main Road 989'}
{'_id': ObjectId('6138663a6461436a279a77dd'), 'name': 'Viola', 'address': 'Sideway 1633'}

Update Many To update all documents that meets the criteria of the query, use the update_many() method.

In [3]:

#Update all documents where the address starts with the letter "S":

import pymongo

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

myquery = { "address": { "$regex": "^S" } }
newvalues = { "$set": { "name": "Minnie" } }

x = mycol.update_many(myquery, newvalues)

print(x.modified_count, "documents updated.")
6 documents updated.

Limit the Result To limit the result in MongoDB, we use the limit() method.

The limit() method takes one parameter, a number defining how many documents to return. Consider you have a “customers” collection:

In [4]:

#Limit the result to only return 5 documents:

import pymongo

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

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

#print the result:
for x in myresult:
  print(x)
{'_id': ObjectId('61385e5156e0542d940db712'), 'name': 'John', 'address': 'Highway 37'}
{'_id': ObjectId('61385fc856e0542d940db714'), 'name': 'Peter', 'address': 'Lowstreet 27'}
{'_id': ObjectId('6138618356e0542d940db716'), 'name': 'Amy', 'address': 'Apple st 652'}
{'_id': ObjectId('6138618356e0542d940db718'), 'name': 'Michael', 'address': 'Canyon 123'}
{'_id': ObjectId('6138618356e0542d940db719'), 'name': 'Sandy', 'address': 'Ocean blvd 2'}

In [ ]: