ในเชลล์ MongoDB ฉันจะแสดงรายการคอลเลกชันทั้งหมดสำหรับฐานข้อมูลปัจจุบันที่ฉันใช้อยู่ได้อย่างไร
ในเชลล์ MongoDB ฉันจะแสดงรายการคอลเลกชันทั้งหมดสำหรับฐานข้อมูลปัจจุบันที่ฉันใช้อยู่ได้อย่างไร
คำตอบ:
คุณทำได้...
JavaScript (เชลล์):
db.getCollectionNames()
Node.js:
db.listCollections()
ไม่ใช่ JavaScript (เชลล์เท่านั้น):
show collections
เหตุผลที่ฉันเรียกว่าไม่ใช่ JavaScript นั้นเป็นเพราะ:
$ mongo prodmongo/app --eval "show collections"
MongoDB shell version: 3.2.10
connecting to: prodmongo/app
2016-10-26T19:34:34.886-0400 E QUERY [thread1] SyntaxError: missing ; before statement @(shell eval):1:5
$ mongo prodmongo/app --eval "db.getCollectionNames()"
MongoDB shell version: 3.2.10
connecting to: prodmongo/app
[
"Profiles",
"Unit_Info"
]
หากคุณต้องการshow collectionsผลผลิตที่หวานและหวานจริงๆคุณสามารถ:
$ mongo prodmongo/app --eval "db.getCollectionNames().join('\n')"
MongoDB shell version: 3.2.10
connecting to: prodmongo/app
Profiles
Unit_Info
db.listCollections()ตามคำตอบที่แสดงไว้ที่นี่และเช็คเป็นสีเขียว? มิฉะนั้นคนทำผิดพลาดเหมือนที่ฉันได้นับครั้งไม่ถ้วนเมื่อพวกเขามาถึงคำตอบนี้ - และพยายามที่จะใช้และข้อผิดพลาดกลับมาdb.getCollectionNames db.collectionNames is not a function
db.getCollectionNames()ยังคงเป็นคำตอบที่ถูกต้องสำหรับเชลล์
> show collections
จะแสดงรายการคอลเลกชันทั้งหมดในฐานข้อมูลที่เลือกในปัจจุบันตามที่ระบุในวิธีใช้บรรทัดคำสั่ง ( help)
content 1145.586MB / 1506.855MBตัวอย่างเช่น.
ฉันจะแสดงรายการคอลเล็กชันทั้งหมดสำหรับฐานข้อมูลปัจจุบันที่ฉันใช้อยู่ได้อย่างไร
show collectionsshow tablesdb.getCollectionNames()show dbs
use databasename
show collections
เอาท์พุท:
collection1 collection2 system.indexes
(หรือ)
show tables
เอาท์พุท:
collection1 collection2 system.indexes
(หรือ)
db.getCollectionNames()
เอาท์พุท:
[ "collection1", "collection2", "system.indexes" ]
use collectionname
show tablesมีประโยชน์มากสำหรับผู้ที่มาจากพื้นหลัง dbms เชิงสัมพันธ์
useคือการใช้ฐานข้อมูล, ไม่มีส่วนเกี่ยวข้องกับคอลเลกชัน
> show tables
มันให้ผลลัพธ์เช่นเดียวกับคำตอบของคาเมรอน
นอกเหนือจากตัวเลือกที่คนอื่นแนะนำ:
show collections // Output every collection
show tables
db.getCollectionNames() // Shows all collections as a list
นอกจากนี้ยังมีวิธีอื่นที่มีประโยชน์จริง ๆ ถ้าคุณต้องการทราบว่าแต่ละคอลเลกชันนั้นถูกสร้างขึ้นมาอย่างไร (ตัวอย่างเช่นมันเป็นคอลเลกชันที่ต่อยอดที่มีขนาดเฉพาะ):
db.system.namespaces.find()
ก่อนอื่นคุณต้องใช้ฐานข้อมูลเพื่อแสดงการรวบรวม / ตารางทั้งหมดภายในนั้น
>show dbs
users 0.56787GB
test (empty)
>db.test.help() // this will give you all the function which can be used with this db
>use users
>show tables //will show all the collection in the db
คุณสามารถใช้หรือshow tablesshow collections
ลอง:
help // To show all help methods
show dbs // To show all dbs
use dbname // To select your db
show collections // To show all collections in selected db
คำสั่งที่ใช้สำหรับแสดงคอลเลกชันทั้งหมดในฐานข้อมูล MongoDB คือ
show collections
ก่อนที่จะรันshow collectionsคำสั่งคุณต้องเลือกฐานข้อมูล:
use mydb // mydb is the name of the database being selected
หากต้องการดูฐานข้อมูลทั้งหมดคุณสามารถใช้คำสั่ง
show dbs // Shows all the database names present
หากคุณต้องการแสดงคอลเลกชันทั้งหมดจากเชลล์ MongoDB (บรรทัดคำสั่ง) ให้ใช้ตัวช่วยเชลล์
show collections
ที่แสดงคอลเลกชันทั้งหมดสำหรับฐานข้อมูลปัจจุบัน หากคุณต้องการรับรายชื่อคอลเล็กชันทั้งหมดจากแอปพลิเคชันของคุณคุณสามารถใช้วิธีฐานข้อมูล MongoDB
db.getCollectionNames()
สำหรับข้อมูลเพิ่มเติมเกี่ยวกับผู้ช่วยที่ MongoDB เปลือกคุณสามารถดูเชลล์อ้างอิงด่วนmongo
คำสั่งต่อไปนี้บน mongoshell นั้นเป็นเรื่องธรรมดา
show databases
show collections
นอกจากนี้
show dbs
use mydb
db.getCollectionNames()
บางครั้งมันมีประโยชน์ในการดูคอลเลกชันทั้งหมดรวมถึงดัชนีในคอลเลกชันซึ่งเป็นส่วนหนึ่งของเนมสเปซโดยรวม:
นี่คือวิธีที่คุณจะทำ:
db.getCollectionNames().forEach(function(collection) {
indexes = db[collection].getIndexes();
print("Indexes for " + collection + ":");
printjson(indexes);
});
ระหว่างคำสั่งทั้งสามกับตัวอย่างนี้คุณควรได้รับการคุ้มครองอย่างดี!
ฉันคิดว่าหนึ่งใน confusions ที่ใหญ่ที่สุดคือความแตกต่างระหว่างสิ่งที่คุณสามารถทำได้กับmongo(หรือเปลือกโต้ตอบ / ไฮบริด) กับmongo --eval(หรือเปลือก JavaScript บริสุทธิ์) ฉันเก็บเอกสารที่เป็นประโยชน์เหล่านี้มีประโยชน์:
นี่คือตัวอย่างของการสคริปต์สิ่งที่คุณอาจทำกับshowคำสั่ง:
# List all databases and the collections in them
mongo --eval "
db.getMongo().getDBNames().forEach(
function(v, i){
print(
v + '\n\t' +
db.getSiblingDB(v).getCollectionNames().join('\n\t')
)
}
)
"
หมายเหตุ: มันใช้งานได้ดีเหมือนซับในเดียว (แต่มันดูแย่มากใน Stack Overflow)
mongo --eval "db.getMongo().getDBNames().forEach(function(v, i){print(v+'\n\t'+db.getSiblingDB(v).getCollectionNames().join('\n\t'))})"
ใน> = 2.x คุณสามารถทำได้
db.listCollections()
บน 1.x คุณสามารถทำได้
db.getCollectionNames()
db.getCollectionNames()ฉันจะได้รับ[ "users" ]เนื่องจากฉันมีคอลเลกชันผู้ใช้ ถ้าฉันลองdb.listCollections()ก็จะส่งผลให้[thread1] TypeError: db.listCollections is not a function : @(shell):1:1
รายการคอลเลกชันทั้งหมดจากmongoเปลือก:
- db.getCollectionNames ()
- แสดงคอลเล็กชัน
- แสดงตาราง
หมายเหตุ: คอลเล็กชันจะแสดงจากฐานข้อมูลปัจจุบันที่คุณอยู่ใน ปัจจุบัน
สำหรับเปลี่ยนเป็นฐานข้อมูล
โดย:
ใช้ตัวอย่าง{your_database_name} :
use friends
ที่ไหน friendsเป็นชื่อของฐานข้อมูลของคุณ
จากนั้นเขียน:
db.getCollectionNames()
show collections
สิ่งนี้จะทำให้คุณมีชื่อของคอลเลกชัน
> show dbs
anuradhfirst 0.000GB
local 0.000GB
> use anuradhfirst
switched to db anuradhfirst
> show collections
record
mongoใช้ นี่จะเป็นการเริ่มการเชื่อมต่อshow dbsคำสั่ง นี่จะแสดงฐานข้อมูลที่มีอยู่ / ออกทั้งหมดdatabaseคุณต้องการ anuradhfirstในข้างต้นก็คือ use anuradhfirstจากนั้นเรียก นี่จะเปลี่ยนเป็นฐานข้อมูลที่คุณต้องการshow collectionsคำสั่ง นี่จะแสดงทั้งหมดcollectionsภายในฐานข้อมูลที่คุณเลือกแสดงคอลเล็กชัน
คำสั่งนี้มักจะทำงานบนเปลือก MongoDB เมื่อคุณเปลี่ยนไปยังฐานข้อมูล
สำหรับการปรับใช้ MongoDB 3.0 โดยใช้เอ็นจิ้นการเก็บข้อมูล WiredTiger หากคุณเรียกใช้
db.getCollectionNames()จากรุ่นของ mongo shell ก่อน 3.0 หรือรุ่นของไดรเวอร์ก่อนหน้า 3.0 รุ่นที่เข้ากันได้db.getCollectionNames()จะไม่ส่งคืนข้อมูลแม้ว่าจะมีคอลเลกชันที่มีอยู่แล้วก็ตาม
show collections
หรือ
show tables
หรือ
db.getCollectionNames();
ฉันใช้listCollections(รองรับ MongoDB 3.0 ขึ้นไป) เพื่อวัตถุประสงค์นี้
ตัวอย่าง:
db.runCommand({ listCollections: 1, filter: {}, nameOnly: true });
หากต้องการดึงข้อมูลเพิ่มเติมเช่นดัชนีของคอลเล็กชัน:
db.runCommand({ listCollections: 1, filter: {}, nameOnly: false });
หากต้องการพิมพ์เฉพาะชื่อคอลเล็กชัน:
db.runCommand({ listCollections: 1, filter: {}, nameOnly: true }).cursor.firstBatch.forEach(v => {print(v.name)})
ฉันรู้สึกว่านี่ให้ความยืดหยุ่นมากกว่า
อ่านเพิ่มเติม: listCollections
1. show collections; // Display all collections
2. show tables // Display all collections
3. db.getCollectionNames(); // Return array of collection. Example :[ "orders", "system.profile" ]
ข้อมูลโดยละเอียดสำหรับทุกการรวบรวม:
db.runCommand( { listCollections: 1.0, authorizedCollections: true, nameOnly: true } )
เพื่อแสดงรายการคอลเล็กชันตามสตริงการค้นหา
db.getCollectionNames().filter(function (CollectionName) { return /<Search String>/.test(CollectionName) })
ตัวอย่าง: ค้นหาคอลเล็กชันทั้งหมดที่มี "อิมพอร์ต" ในชื่อ
db.getCollectionNames().filter(function (CollectionName) { return /import/.test(CollectionName) })
ใช้คำสั่งต่อไปนี้จากmongoเปลือก:
show collections