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?
The pretty() method: Does not change the output format in mongosh . Changes the output format in the legacy mongo shell.
The findOne() returns first document if query matches otherwise returns null. The find() method does not return null, it returns a cursor.
Find() Method. In MongoDB, find() method is used to select documents in a collection and return a cursor to the selected documents.
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 .
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()
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