How to find names of all collections using PyMongo?
How to find names of all collections using PyMongo and find all fields in chosen collection ?
I have name of database and name of chosen collection. (Scenario : user input name of database, need to find all collections and show in dropdown list, when user click on one item need to find all fields in that collection)
python pymongo
add a comment |
How to find names of all collections using PyMongo and find all fields in chosen collection ?
I have name of database and name of chosen collection. (Scenario : user input name of database, need to find all collections and show in dropdown list, when user click on one item need to find all fields in that collection)
python pymongo
As mongo is schema-less, how would you find list of fields?
– Dogbert
Mar 21 '12 at 13:24
add a comment |
How to find names of all collections using PyMongo and find all fields in chosen collection ?
I have name of database and name of chosen collection. (Scenario : user input name of database, need to find all collections and show in dropdown list, when user click on one item need to find all fields in that collection)
python pymongo
How to find names of all collections using PyMongo and find all fields in chosen collection ?
I have name of database and name of chosen collection. (Scenario : user input name of database, need to find all collections and show in dropdown list, when user click on one item need to find all fields in that collection)
python pymongo
python pymongo
asked Mar 21 '12 at 13:19
DamirDamir
19.2k72223330
19.2k72223330
As mongo is schema-less, how would you find list of fields?
– Dogbert
Mar 21 '12 at 13:24
add a comment |
As mongo is schema-less, how would you find list of fields?
– Dogbert
Mar 21 '12 at 13:24
As mongo is schema-less, how would you find list of fields?
– Dogbert
Mar 21 '12 at 13:24
As mongo is schema-less, how would you find list of fields?
– Dogbert
Mar 21 '12 at 13:24
add a comment |
4 Answers
4
active
oldest
votes
To find the collections, you can use collection_names()
- http://api.mongodb.org/python/current/api/pymongo/database.html#pymongo.database.Database.collection_names
8
I believe this has been replaced withlist_collection_names()
now
– AlexG
Aug 11 '18 at 15:28
add a comment |
This is very simple.
e.g.
import pymongo
import json
if __name__ == '__main__':
client = pymongo.MongoClient("localhost", 27017, maxPoolSize=50)
d = dict((db, [collection for collection in client[db].collection_names()])
for db in client.database_names())
print json.dumps(d)
result -> "database1":["collection1","collection2"...], "database2": [...], ..., like:
"test": ["score", "test4", "test5", "test6", "test3", "test7", "user", "test2", "test8"],
"testdb": ["test5", "test8", "test2", "test9", "test3", "test4", "test6", "test"],
"local": ["startup_log"],
"stackoverflow": ["questions"]
Just updating. client[db].collection_names() was deprecated in favor of client[db].list_collection_names().api.mongodb.com/python/current/api/pymongo/…
– Arthur Alvim
Oct 2 '18 at 14:59
I gave an up for the root of the solution, but it would have been much more informative and transparent (thus simply better) without the one liners loop.
– Geeocode
Oct 31 '18 at 1:05
add a comment |
Here is a script that I created that does essentially what you want.
It displays a list of all collections in the database (in this case the 'dh' database).
The user types the collection of choice and the script displays the fields and fields within documents down 2 levels. It displays in mongo entry format, which can be copied directly into a mongo query. It also will check the first level fields for lists of dictionaries and display those subfields in lists surrounded by brackets 'field.[subfield_in_list]' .
There is also optional command line input of collection name (e.g. python path/to/script/scriptname.py collection_name
import pymongo
from pymongo import Connection
mon_con = Connection('localhost', 27017)
mon_db = mon_con.dh
cols = mon_db.collection_names()
for c in cols:
print c
col = raw_input('Input a collection from the list above to show its field names: ')
collection = mon_db[col].find()
keylist =
for item in collection:
for key in item.keys():
if key not in keylist:
keylist.append(key)
if isinstance(item[key], dict):
for subkey in item[key]:
subkey_annotated = key + "." + subkey
if subkey_annotated not in keylist:
keylist.append(subkey_annotated)
if isinstance(item[key][subkey], dict):
for subkey2 in item[subkey]:
subkey2_annotated = subkey_annotated + "." + subkey2
if subkey2_annotated not in keylist:
keylist.append(subkey2_annotated)
if isinstance(item[key], list):
for l in item[key]:
if isinstance(l, dict):
for lkey in l.keys():
lkey_annotated = key + ".[" + lkey + "]"
if lkey_annotated not in keylist:
keylist.append(lkey_annotated)
keylist.sort()
for key in keylist:
keycnt = mon_db[col].find(key:'$exists':1).count()
print "%-5dt%s" % (keycnt, key)
I'm sure you could write a function to iterate down levels infinitely until there is no data left, but this was quick and dirty and serves my needs for now. You could also modify to show the fields for just a particular set of records in a collection. Hope you find it useful.
add a comment |
I always used this way to get all collection names from my MongoDB database.
import pymongo
db_connect = pymongo.MongoClient('192.168.4.202', 20020)
database_name = 'MY_DATABASE_NAME'
database = db_connect[database_name]
collection = database.collection_names(include_system_collections=False)
for collect in collection:
print collect
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f9805451%2fhow-to-find-names-of-all-collections-using-pymongo%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
To find the collections, you can use collection_names()
- http://api.mongodb.org/python/current/api/pymongo/database.html#pymongo.database.Database.collection_names
8
I believe this has been replaced withlist_collection_names()
now
– AlexG
Aug 11 '18 at 15:28
add a comment |
To find the collections, you can use collection_names()
- http://api.mongodb.org/python/current/api/pymongo/database.html#pymongo.database.Database.collection_names
8
I believe this has been replaced withlist_collection_names()
now
– AlexG
Aug 11 '18 at 15:28
add a comment |
To find the collections, you can use collection_names()
- http://api.mongodb.org/python/current/api/pymongo/database.html#pymongo.database.Database.collection_names
To find the collections, you can use collection_names()
- http://api.mongodb.org/python/current/api/pymongo/database.html#pymongo.database.Database.collection_names
answered Mar 21 '12 at 13:22
DogbertDogbert
152k28240265
152k28240265
8
I believe this has been replaced withlist_collection_names()
now
– AlexG
Aug 11 '18 at 15:28
add a comment |
8
I believe this has been replaced withlist_collection_names()
now
– AlexG
Aug 11 '18 at 15:28
8
8
I believe this has been replaced with
list_collection_names()
now– AlexG
Aug 11 '18 at 15:28
I believe this has been replaced with
list_collection_names()
now– AlexG
Aug 11 '18 at 15:28
add a comment |
This is very simple.
e.g.
import pymongo
import json
if __name__ == '__main__':
client = pymongo.MongoClient("localhost", 27017, maxPoolSize=50)
d = dict((db, [collection for collection in client[db].collection_names()])
for db in client.database_names())
print json.dumps(d)
result -> "database1":["collection1","collection2"...], "database2": [...], ..., like:
"test": ["score", "test4", "test5", "test6", "test3", "test7", "user", "test2", "test8"],
"testdb": ["test5", "test8", "test2", "test9", "test3", "test4", "test6", "test"],
"local": ["startup_log"],
"stackoverflow": ["questions"]
Just updating. client[db].collection_names() was deprecated in favor of client[db].list_collection_names().api.mongodb.com/python/current/api/pymongo/…
– Arthur Alvim
Oct 2 '18 at 14:59
I gave an up for the root of the solution, but it would have been much more informative and transparent (thus simply better) without the one liners loop.
– Geeocode
Oct 31 '18 at 1:05
add a comment |
This is very simple.
e.g.
import pymongo
import json
if __name__ == '__main__':
client = pymongo.MongoClient("localhost", 27017, maxPoolSize=50)
d = dict((db, [collection for collection in client[db].collection_names()])
for db in client.database_names())
print json.dumps(d)
result -> "database1":["collection1","collection2"...], "database2": [...], ..., like:
"test": ["score", "test4", "test5", "test6", "test3", "test7", "user", "test2", "test8"],
"testdb": ["test5", "test8", "test2", "test9", "test3", "test4", "test6", "test"],
"local": ["startup_log"],
"stackoverflow": ["questions"]
Just updating. client[db].collection_names() was deprecated in favor of client[db].list_collection_names().api.mongodb.com/python/current/api/pymongo/…
– Arthur Alvim
Oct 2 '18 at 14:59
I gave an up for the root of the solution, but it would have been much more informative and transparent (thus simply better) without the one liners loop.
– Geeocode
Oct 31 '18 at 1:05
add a comment |
This is very simple.
e.g.
import pymongo
import json
if __name__ == '__main__':
client = pymongo.MongoClient("localhost", 27017, maxPoolSize=50)
d = dict((db, [collection for collection in client[db].collection_names()])
for db in client.database_names())
print json.dumps(d)
result -> "database1":["collection1","collection2"...], "database2": [...], ..., like:
"test": ["score", "test4", "test5", "test6", "test3", "test7", "user", "test2", "test8"],
"testdb": ["test5", "test8", "test2", "test9", "test3", "test4", "test6", "test"],
"local": ["startup_log"],
"stackoverflow": ["questions"]
This is very simple.
e.g.
import pymongo
import json
if __name__ == '__main__':
client = pymongo.MongoClient("localhost", 27017, maxPoolSize=50)
d = dict((db, [collection for collection in client[db].collection_names()])
for db in client.database_names())
print json.dumps(d)
result -> "database1":["collection1","collection2"...], "database2": [...], ..., like:
"test": ["score", "test4", "test5", "test6", "test3", "test7", "user", "test2", "test8"],
"testdb": ["test5", "test8", "test2", "test9", "test3", "test4", "test6", "test"],
"local": ["startup_log"],
"stackoverflow": ["questions"]
edited Jun 2 '16 at 3:25
answered Jun 2 '16 at 3:06
Little RoysLittle Roys
1,69121421
1,69121421
Just updating. client[db].collection_names() was deprecated in favor of client[db].list_collection_names().api.mongodb.com/python/current/api/pymongo/…
– Arthur Alvim
Oct 2 '18 at 14:59
I gave an up for the root of the solution, but it would have been much more informative and transparent (thus simply better) without the one liners loop.
– Geeocode
Oct 31 '18 at 1:05
add a comment |
Just updating. client[db].collection_names() was deprecated in favor of client[db].list_collection_names().api.mongodb.com/python/current/api/pymongo/…
– Arthur Alvim
Oct 2 '18 at 14:59
I gave an up for the root of the solution, but it would have been much more informative and transparent (thus simply better) without the one liners loop.
– Geeocode
Oct 31 '18 at 1:05
Just updating. client[db].collection_names() was deprecated in favor of client[db].list_collection_names().api.mongodb.com/python/current/api/pymongo/…
– Arthur Alvim
Oct 2 '18 at 14:59
Just updating. client[db].collection_names() was deprecated in favor of client[db].list_collection_names().api.mongodb.com/python/current/api/pymongo/…
– Arthur Alvim
Oct 2 '18 at 14:59
I gave an up for the root of the solution, but it would have been much more informative and transparent (thus simply better) without the one liners loop.
– Geeocode
Oct 31 '18 at 1:05
I gave an up for the root of the solution, but it would have been much more informative and transparent (thus simply better) without the one liners loop.
– Geeocode
Oct 31 '18 at 1:05
add a comment |
Here is a script that I created that does essentially what you want.
It displays a list of all collections in the database (in this case the 'dh' database).
The user types the collection of choice and the script displays the fields and fields within documents down 2 levels. It displays in mongo entry format, which can be copied directly into a mongo query. It also will check the first level fields for lists of dictionaries and display those subfields in lists surrounded by brackets 'field.[subfield_in_list]' .
There is also optional command line input of collection name (e.g. python path/to/script/scriptname.py collection_name
import pymongo
from pymongo import Connection
mon_con = Connection('localhost', 27017)
mon_db = mon_con.dh
cols = mon_db.collection_names()
for c in cols:
print c
col = raw_input('Input a collection from the list above to show its field names: ')
collection = mon_db[col].find()
keylist =
for item in collection:
for key in item.keys():
if key not in keylist:
keylist.append(key)
if isinstance(item[key], dict):
for subkey in item[key]:
subkey_annotated = key + "." + subkey
if subkey_annotated not in keylist:
keylist.append(subkey_annotated)
if isinstance(item[key][subkey], dict):
for subkey2 in item[subkey]:
subkey2_annotated = subkey_annotated + "." + subkey2
if subkey2_annotated not in keylist:
keylist.append(subkey2_annotated)
if isinstance(item[key], list):
for l in item[key]:
if isinstance(l, dict):
for lkey in l.keys():
lkey_annotated = key + ".[" + lkey + "]"
if lkey_annotated not in keylist:
keylist.append(lkey_annotated)
keylist.sort()
for key in keylist:
keycnt = mon_db[col].find(key:'$exists':1).count()
print "%-5dt%s" % (keycnt, key)
I'm sure you could write a function to iterate down levels infinitely until there is no data left, but this was quick and dirty and serves my needs for now. You could also modify to show the fields for just a particular set of records in a collection. Hope you find it useful.
add a comment |
Here is a script that I created that does essentially what you want.
It displays a list of all collections in the database (in this case the 'dh' database).
The user types the collection of choice and the script displays the fields and fields within documents down 2 levels. It displays in mongo entry format, which can be copied directly into a mongo query. It also will check the first level fields for lists of dictionaries and display those subfields in lists surrounded by brackets 'field.[subfield_in_list]' .
There is also optional command line input of collection name (e.g. python path/to/script/scriptname.py collection_name
import pymongo
from pymongo import Connection
mon_con = Connection('localhost', 27017)
mon_db = mon_con.dh
cols = mon_db.collection_names()
for c in cols:
print c
col = raw_input('Input a collection from the list above to show its field names: ')
collection = mon_db[col].find()
keylist =
for item in collection:
for key in item.keys():
if key not in keylist:
keylist.append(key)
if isinstance(item[key], dict):
for subkey in item[key]:
subkey_annotated = key + "." + subkey
if subkey_annotated not in keylist:
keylist.append(subkey_annotated)
if isinstance(item[key][subkey], dict):
for subkey2 in item[subkey]:
subkey2_annotated = subkey_annotated + "." + subkey2
if subkey2_annotated not in keylist:
keylist.append(subkey2_annotated)
if isinstance(item[key], list):
for l in item[key]:
if isinstance(l, dict):
for lkey in l.keys():
lkey_annotated = key + ".[" + lkey + "]"
if lkey_annotated not in keylist:
keylist.append(lkey_annotated)
keylist.sort()
for key in keylist:
keycnt = mon_db[col].find(key:'$exists':1).count()
print "%-5dt%s" % (keycnt, key)
I'm sure you could write a function to iterate down levels infinitely until there is no data left, but this was quick and dirty and serves my needs for now. You could also modify to show the fields for just a particular set of records in a collection. Hope you find it useful.
add a comment |
Here is a script that I created that does essentially what you want.
It displays a list of all collections in the database (in this case the 'dh' database).
The user types the collection of choice and the script displays the fields and fields within documents down 2 levels. It displays in mongo entry format, which can be copied directly into a mongo query. It also will check the first level fields for lists of dictionaries and display those subfields in lists surrounded by brackets 'field.[subfield_in_list]' .
There is also optional command line input of collection name (e.g. python path/to/script/scriptname.py collection_name
import pymongo
from pymongo import Connection
mon_con = Connection('localhost', 27017)
mon_db = mon_con.dh
cols = mon_db.collection_names()
for c in cols:
print c
col = raw_input('Input a collection from the list above to show its field names: ')
collection = mon_db[col].find()
keylist =
for item in collection:
for key in item.keys():
if key not in keylist:
keylist.append(key)
if isinstance(item[key], dict):
for subkey in item[key]:
subkey_annotated = key + "." + subkey
if subkey_annotated not in keylist:
keylist.append(subkey_annotated)
if isinstance(item[key][subkey], dict):
for subkey2 in item[subkey]:
subkey2_annotated = subkey_annotated + "." + subkey2
if subkey2_annotated not in keylist:
keylist.append(subkey2_annotated)
if isinstance(item[key], list):
for l in item[key]:
if isinstance(l, dict):
for lkey in l.keys():
lkey_annotated = key + ".[" + lkey + "]"
if lkey_annotated not in keylist:
keylist.append(lkey_annotated)
keylist.sort()
for key in keylist:
keycnt = mon_db[col].find(key:'$exists':1).count()
print "%-5dt%s" % (keycnt, key)
I'm sure you could write a function to iterate down levels infinitely until there is no data left, but this was quick and dirty and serves my needs for now. You could also modify to show the fields for just a particular set of records in a collection. Hope you find it useful.
Here is a script that I created that does essentially what you want.
It displays a list of all collections in the database (in this case the 'dh' database).
The user types the collection of choice and the script displays the fields and fields within documents down 2 levels. It displays in mongo entry format, which can be copied directly into a mongo query. It also will check the first level fields for lists of dictionaries and display those subfields in lists surrounded by brackets 'field.[subfield_in_list]' .
There is also optional command line input of collection name (e.g. python path/to/script/scriptname.py collection_name
import pymongo
from pymongo import Connection
mon_con = Connection('localhost', 27017)
mon_db = mon_con.dh
cols = mon_db.collection_names()
for c in cols:
print c
col = raw_input('Input a collection from the list above to show its field names: ')
collection = mon_db[col].find()
keylist =
for item in collection:
for key in item.keys():
if key not in keylist:
keylist.append(key)
if isinstance(item[key], dict):
for subkey in item[key]:
subkey_annotated = key + "." + subkey
if subkey_annotated not in keylist:
keylist.append(subkey_annotated)
if isinstance(item[key][subkey], dict):
for subkey2 in item[subkey]:
subkey2_annotated = subkey_annotated + "." + subkey2
if subkey2_annotated not in keylist:
keylist.append(subkey2_annotated)
if isinstance(item[key], list):
for l in item[key]:
if isinstance(l, dict):
for lkey in l.keys():
lkey_annotated = key + ".[" + lkey + "]"
if lkey_annotated not in keylist:
keylist.append(lkey_annotated)
keylist.sort()
for key in keylist:
keycnt = mon_db[col].find(key:'$exists':1).count()
print "%-5dt%s" % (keycnt, key)
I'm sure you could write a function to iterate down levels infinitely until there is no data left, but this was quick and dirty and serves my needs for now. You could also modify to show the fields for just a particular set of records in a collection. Hope you find it useful.
answered May 7 '12 at 17:38
AlanAlan
671
671
add a comment |
add a comment |
I always used this way to get all collection names from my MongoDB database.
import pymongo
db_connect = pymongo.MongoClient('192.168.4.202', 20020)
database_name = 'MY_DATABASE_NAME'
database = db_connect[database_name]
collection = database.collection_names(include_system_collections=False)
for collect in collection:
print collect
add a comment |
I always used this way to get all collection names from my MongoDB database.
import pymongo
db_connect = pymongo.MongoClient('192.168.4.202', 20020)
database_name = 'MY_DATABASE_NAME'
database = db_connect[database_name]
collection = database.collection_names(include_system_collections=False)
for collect in collection:
print collect
add a comment |
I always used this way to get all collection names from my MongoDB database.
import pymongo
db_connect = pymongo.MongoClient('192.168.4.202', 20020)
database_name = 'MY_DATABASE_NAME'
database = db_connect[database_name]
collection = database.collection_names(include_system_collections=False)
for collect in collection:
print collect
I always used this way to get all collection names from my MongoDB database.
import pymongo
db_connect = pymongo.MongoClient('192.168.4.202', 20020)
database_name = 'MY_DATABASE_NAME'
database = db_connect[database_name]
collection = database.collection_names(include_system_collections=False)
for collect in collection:
print collect
answered Jun 2 '16 at 3:15
Rajiv SharmaRajiv Sharma
2,1072033
2,1072033
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f9805451%2fhow-to-find-names-of-all-collections-using-pymongo%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
As mongo is schema-less, how would you find list of fields?
– Dogbert
Mar 21 '12 at 13:24