python

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 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…

Python MongoDB Create Database

Creating a Database To create a database in MongoDB, start by creating a MongoClient object, then specify a connection URL with the correct ip address and the name of the database you want to create. MongoDB will create the database…

Python MongoDB

Python can be used in database applications. One of the most popular NoSQL database is MongoDB. MongoDB MongoDB stores data in JSON-like documents, which makes the database very flexible and scalable. To be able to experiment with the code examples…