In the MongoDB shell, how to views all documents in a particular collection for the current database that I'm using?

when i am trying through the query

> db.getCollection().find()

To getting the error as mention below

2017-10-14T00:57:34.363+0530 E QUERY    [thread1] Error: collection constructor called with undefined argument :

I am also uploading the screen shot of mongo shell command prompt here

enter image description here

4 Answers 4


OK, let's start from basics!

After you have connected to mongod with command mongo.

  • List databases with command show dbs

iot:PRIMARY> show dbs admin 0.000GB iot 0.020GB local 0.042GB test 0.000GB testi 0.000GB

  • Select one of the DB's with use iot command

iot:PRIMARY> use iot

switched to db iot

  • List collections on that DB with show collections command

iot:PRIMARY> show collections data header key

  • Make query to one of those collections

iot:PRIMARY> db.header.find() { "_id" : "1b5caa", "temp1" : "Temperature", "pressure1" : "Pressure", "humidity1" : "Humidity", "uv1" : "UV", "BusV1" : "Solar Panel (V)", "Current1" : "Solar Panel Current (mA)", "BusV2" : "Battery (V)", "Current2" : "Battery Current (mA)" } { "_id" : "30444", "temp1" : "Temperature", "pressure1" : "Pressure", "humidity1" : "Humidity" } { "_id" : "239684", "temp1" : "Temperature", "pressure1" : "Pressure", "humidity1" : "Humidity" }

So, you need to be connected WANTED database with use command and you need to show the one collection what you want to query with db.<collection_name>.find()

How to see to what database I'm connected currently? Just give command db and you get the answer what is your current DB.

  • Yes JJussi through db.<collection_name>.find(), i am able to find out all documents in a particular collection. Even db.getCollection('Document Name').find({}), also able to get the complete documents in a particular collection. so, i am going to a accept as a answer. Oct 17, 2017 at 5:19
  • Very good explanation for a beginner <3 Dec 21, 2018 at 10:46



Select All Documents in a Collection

To select all documents in the collection, pass an empty document as the query filter parameter to the find method. The query filter parameter determines the select criteria:

db.inventory.find( {} )

These operation corresponds to the following SQL statement:

SELECT * FROM inventory
  • unable to find out through your query db.inventory.find( {} ). Even i have gone through the mention ref mongo documents, but not find out the exact my query desire.thanks Oct 13, 2017 at 19:53
  • Your query is wrong. it is dbname.collectionName.find( {} ) . Why are you using getcollection()? Oct 13, 2017 at 19:55
  • ,ok. Why are you using getcollection()? What do you mean by that? in respect of mongo shell query or else Oct 13, 2017 at 19:58
  • To find all documents in a collection the command is dbname.collectionname.find( {} ) Oct 13, 2017 at 20:00
  • As per your instruction when i am running the query like dbname.collectionname.find( {} ) then getting the error such as 2017-10-14T01:26:59.230+0530 E QUERY [thread1] ReferenceError: collectionname is not defined : @(shell):1:1 Oct 13, 2017 at 20:03

First choose the database: use <database_name>

Show out all collections: show collections OR db.getCollectionNames()

Show all documents: db.<collection_name>.find()

Show all document in easy-to-read and attractive format: db.<collection_name>.find().pretty

show dbs

use your database name in my case, I'm using - use smartbank then - show collections - just to check the document collections name. and finally, db. your collection name.find() or find({}) -

db.users.find() or db.users.find({}) or db.users.find({_id: ObjectId("60c8823cbe9c1c21604f642b")})

you can specify _id:ObjectId(write the document id here) to get the single document

enter image description here

  • This answer doesn't seem to add much to the earlier answers.
    – mustaccio
    Jun 15, 2021 at 11:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.