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) with the address “Park Lane 38”:

import pymongo

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

myquery = { "address": "Park Lane 38" }

mydoc = mycol.find(myquery)

for x in mydoc:
  print(x)

Continue reading Python MongoDB Query

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 in MongoDB, we can use the find_one() method.

The find_one() method returns the first occurrence in the selection. Continue reading Python MongoDB Find

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 in this tutorial, you will need access to a MongoDB database.

You can download a free MongoDB database at

https://www.mongodb.com.

Or get started right away with a MongoDB cloud service at

https://www.mongodb.com/cloud/atlas.

Continue reading Python MongoDB