How to connect to my MongoDB table by command line?


In order to connect to my table by command line, you need to use db command

db.yourCollectionName.find();

Let’s say we have a database “sample” with some collections. First check the current database

> use sample;
switched to db sample
> db;
Sample
Now we have reached the database sample. The database “sample” is having the following collections:
> show collections;

This will produce the following output

arraySizeErrorDemo
basicInformationDemo
copyThisCollectionToSampleDatabaseDemo
deleteAllRecordsDemo
deleteDocuments
deleteDocumentsDemo
deleteSomeInformation
documentWithAParticularFieldValueDemo
employee
findListOfIdsDemo
findSubstring
getAllRecordsFromSourceCollectionDemo
getElementWithMaxIdDemo
internalArraySizeDemo
largestDocumentDemo
makingStudentInformationClone
oppositeAddToSetDemo
prettyDemo
returnOnlyUniqueValuesDemo
selectWhereInDemo
sourceCollection
studentInformation
sumOfValueDemo
sumTwoFieldsDemo
truncateDemo
updateInformation
userInformation

Here is the correct way of connecting to a table i.e. collection. You need to use db command. Following is the query

> db.userInformation.find();

This will produce the following output

{ "_id" : ObjectId("5c6a765964f3d70fcc9147f5"), "Name" : "John", "Age" : 30, "isStudent" : false, "Subjects" : [ "Introduction to java", "Introduction to MongoDB" ] }

Updated on: 30-Jul-2019

137 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements