Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> MongoDB

Cách liệt kê tất cả collection trong Mongo shell

Hai cách liệt kê collection trong MongoDB shell:

Cách 1: getCollectionNames()

> db.getCollectionNames();

Trả về mảng string tên collection. Dễ xử lý trong script.

Cách 2: show collections

> show collections;

Lệnh shell built-in, hiển thị dạng danh sách đơn giản.

Ví dụ đầu ra

[
  "ConvertStringToDateDemo", "IdUpdateDemo", "ProductsInformation",
  "addFieldDemo", "arrayInnerElementsDemo", "arrayOfArraysDemo",
  "caseInsensitiveDemo", "checkFieldContainsStringDemo", ...
]

Hoặc dạng show collections:

ConvertStringToDateDemo IdUpdateDemo ProductsInformation
addFieldDemo arrayInnerElementsDemo arrayOfArraysDemo
caseInsensitiveDemo checkFieldContainsStringDemo ...

Lưu ý: Cả hai chỉ liệt kê collection của database hiện tại (xem bằng db). Chuyển database bằng use tenDB trước khi liệt kê.