0

I'd like to know what call I can make from a python MongoEngine instance to get a list of collection names from my mongodb database? If I were using pymongo directly I could call db.list_collection_names(), but I can't find a similar call from MongoEngine. Specifically I'm using flask-mongoengine if that matters.

1 Answers1

2

MongoEngine

from mongoengine import connect

db_name = 'test'
connection = connect(db_name)
connection.get_database(db_name).list_collection_names()

Flask-MongoEngine

from flask import Flask
from flask_mongoengine import MongoEngine

app = Flask(__name__)
db = MongoEngine(app)
#app.config.from_pyfile('the-config.cfg')
#app.config['MONGODB_SETTINGS'] = {}
db.get_db().list_collection_names()
Valijon
  • 12,667
  • 4
  • 34
  • 67
  • 1
    This worked, thank you. Also to note, `get_db()` takes as its first argument `alias`, which you can provide the appropriate database alias if you have multiple database connections. Appreciate the help. :-) – Theodore Williams Mar 02 '20 at 16:28
  • `list_collection_names()` is not in the documentation but it works . Can you send a link to the exact place in any documentation where this method is shown? The MongoEngine docs here using the search bar brings up nothing: http://docs.mongoengine.org/search.html?q=list&check_keywords=yes&area=default# – mLstudent33 Sep 27 '20 at 04:43
  • @mLstudent33 Check [here](https://api.mongodb.com/python/current/api/pymongo/database.html) – Valijon Sep 27 '20 at 13:14
  • so it's actually a PyMongo method and not MongoEngine. – mLstudent33 Sep 28 '20 at 01:54