Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB Print Pretty with PyMongo [duplicate]

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?

like image 718
Vandexel Avatar asked Jan 04 '16 19:01

Vandexel


People also ask

What does pretty () do in MongoDB?

pretty() method in MongoDB : If we call find method in a collection, it shows all documents available in that collection.

How do I get unique values in PyMongo?

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.

What does Insert_one return PyMongo?

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.

What does PyMongo Find_one return?

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.


1 Answers

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)
like image 165
masnun Avatar answered Sep 21 '22 06:09

masnun