Check if data exist in collection

I have to check in my code if a collection have some data. I have created a function where it loads all the data from collection, but this will take time everytime, is there any other way to do it quickly without loading all the data from collection.

Below is my sample function to load all data, Thanks!

def load_all_data_from_class_with_embeddings(class_name):

Get the collection

collection = client.collections.get(class_name)
# Fetch all objects using iterator
all_results = []
for item in collection.iterator():
    result = {'skill_name': item.properties.get('skill_name')}
    all_results.append(result)
# Convert the list of results into a DataFrame
df = pd.DataFrame(all_results)
return df (edited)

Hi,

you can just do

collection = client.collections.get(class_name)
nr_objects = len(collection)

then you know how many objects there are in the collection

1 Like