MongoDB शेल में, मैं वर्तमान डेटाबेस के लिए सभी संग्रहों को कैसे सूचीबद्ध करूं जो मैं उपयोग कर रहा हूं?
MongoDB शेल में, मैं वर्तमान डेटाबेस के लिए सभी संग्रहों को कैसे सूचीबद्ध करूं जो मैं उपयोग कर रहा हूं?
जवाबों:
तुम कर सकते हो...
जावास्क्रिप्ट (शेल):
db.getCollectionNames()
Node.js:
db.listCollections()
गैर-जावास्क्रिप्ट (केवल शेल):
show collections
कारण यह है कि मुझे गैर-जावास्क्रिप्ट कहते हैं क्योंकि:
$ 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
वर्तमान में चयनित DB में सभी संग्रहों को सूचीबद्ध करेगा, जैसा कि कमांड लाइन मदद ( 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 tablesया show 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शेल क्विक संदर्भ देख सकते हैं ।
आम पर निम्न आदेश आम हैं।
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);
});
तीन आज्ञाओं और इस स्निपेट के बीच, आपको अच्छी तरह से कवर किया जाना चाहिए!
मुझे लगता है कि सबसे बड़े भ्रमों में से एक यह है कि आप mongo(या एक इंटरैक्टिव / हाइब्रिड शेल) बनाम mongo --eval(या शुद्ध जावास्क्रिप्ट शेल) के साथ क्या कर सकते हैं । मैं इन सहायक दस्तावेजों को संभाल कर रखता हूं:
यहाँ स्क्रिप्टिंग का एक उदाहरण है जो आप अन्यथा 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')
)
}
)
"
नोट: यह वास्तव में एक लाइनर के रूप में अच्छी तरह से काम करता है। (लेकिन यह स्टैक ओवरफ्लो पर भयानक लग रहा है।)
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()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)})
मुझे लगता है कि यह अधिक लचीलापन प्रदान करता है।
और पढ़ें: सूची
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