Python

Python MongoDB Limit

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:

Python MongoDB Update

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. Note: If the query…

Python MongoDB Drop Collection

Delete Collection You can delete a table, or collection as it is called in MongoDB, by using the drop() method. Example Delete the “customers” collection: import pymongo myclient = pymongo.MongoClient(“mongodb://localhost:27017/”) mydb = myclient[“mydatabase”] mycol = mydb[“customers”] mycol.drop() The drop() method…

Python MongoDB Delete Document

Delete Document 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. Note: If the query finds more than one document, only the first occurrence…

Python MongoDB Sort

Sort the Result 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). Example Sort the result alphabetically by…

Python MongoDB Query

Filter the Result When finding documents in a collection, you can filter the result by using a query object. The first argument of the find() method is a query object, and is used to limit the search. Example Find document(s)…

Python MongoDB Find

In MongoDB we use the find() and find_one() methods to find data in a collection. Just like the SELECT statement is used to find data in a table in a MySQL database. Find One To select data from a collection…

Python MongoDB Insert Document

A document in MongoDB is the same as a record in SQL databases. Insert Into Collection To insert a record, or document as it is called in MongoDB, into a collection, we use the insert_one() method. The first parameter of…

Python MongoDB Create Collection

A collection in MongoDB is the same as a table in SQL databases. Creating a Collection To create a collection in MongoDB, use database object and specify the name of the collection you want to create. MongoDB will create the…