Listing Collection Names Performance

What is the performance cost for a line like this, where we use listCollections and a filter to get if a collection already exists in a database?

There is a chance there may be a lot of collections in our database. Would a line like this be costly?

col_list = db.list_collection_names(filter={"name": name})

You can either use runCommand or show collection on terminal to list collections on database . This will not have any performance impact , as the information about the collection is pulled from metadata. Usually this metadata information is kept in memory for fast access .

Ref:

Implementation of List Command

For the sake of argument, what if the collection list had thousands or even tens of thousands of items? Would the query still be lightweight enough, or would it have a noticeable impact on performance?

No it won’t.

Each collection is at least 1 file. If your server has to manage that many files you would end up with an anti-pattern.

You will have other issues a lot sooner than any issue with listing the collections.

1 Like

@Amitoj_Singh1 , run command returns cursor not the entire resultSet. There is a filter option in list Command so that can help to limit your result . As long as you don’t write that in a while loop your good :sweat_smile:

The anti pattern @steevej mentioned is about having large numbers of collection when you can still optimize to have smaller numbers.