How to views all documents in a particular collection of database in MongoDB through mongo Shell?
-
09-10-2020 - |
Вопрос
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 :
DB.prototype.getCollection@src/mongo/shell/db.js:34:16
@(shell):1:1
I am also uploading the screen shot of mongo shell command prompt 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.
Другие советы
Ref:
https://docs.mongodb.com/manual/tutorial/query-documents/
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
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