I've looked up print pretty for MongoDB, and i understand how to do it from the shell. What I can't find is how to do it with PyMongo, so that when I run it in eclipse, the output will print pretty instead of all in one line. Here's what I have right now:
cursor = collection.find({})
for document in cursor: print(document)
This prints everything in my collection, but each document in my collection just prints in one line. How can i change this to get it to print pretty?
pretty() method in MongoDB : If we call find method in a collection, it shows all documents available in that collection.
PyMongo includes the distinct() function that finds and returns the distinct values for a specified field across a single collection and returns the results in an array. Parameters : key : field name for which the distinct values need to be found.
Return the _id Field The insert_one() method returns a InsertOneResult object, which has a property, inserted_id , that holds the id of the inserted document.
Getting a Single Document With find_one() This method returns a single document matching a query (or None if there are no matches). It is useful when you know there is only one matching document, or are only interested in the first match.
PyMongo fetches the documents as Python data structures. So you can use pprint
with it like this:
from pprint import pprint
cursor = collection.find({})
for document in cursor:
pprint(document)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With