Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Mongo, how do I pretty-print results so .find() looks like .findOne()

findOne() results in pretty-print json object.

find() results in jarbled json object.

How can I make find() the same as findOne(), when it comes to display in the mongo shell?

like image 757
TIMEX Avatar asked Jul 20 '11 01:07

TIMEX


People also ask

What does pretty () do in MongoDB?

The pretty() method: Does not change the output format in mongosh . Changes the output format in the legacy mongo shell.

What is difference between find and findOne in MongoDB?

The findOne() returns first document if query matches otherwise returns null. The find() method does not return null, it returns a cursor.

What does find () do in MongoDB?

Find() Method. In MongoDB, find() method is used to select documents in a collection and return a cursor to the selected documents.

What does findOne return MongoDB?

findOne() method returns a Promise that resolves to the first document in the collection that matches the query. If no documents match the specified query, the promise resolves to null .


1 Answers

If you are scripting using javascript, you can use dcrosta's answer. But if you want to pretty print directly on the mongo interactive shell, you have to append pretty() to your find() queries.

Type on the shell: db.yourcollection.find().pretty()

like image 180
Rohan Avatar answered Sep 22 '22 02:09

Rohan