Computer >> Máy Tính >  >> Lập trình >> MongoDB

Làm cách nào để kết nối với bảng MongoDB của tôi bằng dòng lệnh?

Để kết nối với bảng của tôi bằng dòng lệnh, bạn cần sử dụng lệnh db

db.yourCollectionName.find();

Giả sử chúng tôi có một "mẫu" cơ sở dữ liệu với một số bộ sưu tập. Đầu tiên hãy kiểm tra cơ sở dữ liệu hiện tại

> 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;

Điều này sẽ tạo ra kết quả sau

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

Đây là cách chính xác để kết nối với bảng, tức là bộ sưu tập. Bạn cần sử dụng lệnh db. Sau đây là truy vấn

> db.userInformation.find();

Điều này sẽ tạo ra kết quả sau

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