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


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. – Md Haidar Ali Khan Oct 17 '17 at 5:19
  • Very good explanation for a beginner <3 – Menuka Ishan Dec 21 '18 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 – Md Haidar Ali Khan Oct 13 '17 at 19:53
  • Your query is wrong. it is dbname.collectionName.find( {} ) . Why are you using getcollection()? – SqlWorldWide Oct 13 '17 at 19:55
  • ,ok. Why are you using getcollection()? What do you mean by that? in respect of mongo shell query or else – Md Haidar Ali Khan Oct 13 '17 at 19:58
  • To find all documents in a collection the command is dbname.collectionname.find( {} ) – SqlWorldWide Oct 13 '17 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 – Md Haidar Ali Khan Oct 13 '17 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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