MQL – MongoDB Sort and Delete documents

Use the sort() method to sort the result in ascending or descending order.

The sort() method takes one parameter for “fieldname” and one parameter for “direction” (ascending is the default direction).

In [1]:

import pymongo

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

mydoc = mycol.find().sort("name")

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

sort(“name”, 1) #ascending sort(“name”, -1) #descending

In [2]:

#Sort the result reverse alphabetically by name:
import pymongo

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

mydoc = mycol.find().sort("name", -1)

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

To delete one document, we use the delete_one() method.

The first parameter of the delete_one() method is a query object defining which document to delete.

In [3]:

#Delete the document with the address "Mountain 21":

import pymongo

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

myquery = { "address": "Mountain 21" }

mycol.delete_one(myquery)

Out[3]:

<pymongo.results.DeleteResult at 0x27dc69bef00>

To delete more than one document, use the delete_many() method. The first parameter of the delete_many() method is a query object defining which documents to delete.

In [ ]:

#Delete all documents were the address starts with the letter S:  - Not run

import pymongo

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

myquery = { "address": {"$regex": "^S"} }

x = mycol.delete_many(myquery)

print(x.deleted_count, " documents deleted.")

Delete All Documents in a Collection To delete all documents in a collection, pass an empty query object to the delete_many() method:

In [ ]:

#Delete all documents in the "customers" collection: - Not run

import pymongo

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

x = mycol.delete_many({})

print(x.deleted_count, " documents deleted.")

In [ ]: