检查 Colleciton 是否存在

最近更新时间:2024-10-14 18:01:21

我的收藏

接口定义

exists_collection()用于检查指定的数据库及其对应的集合是否已经存在。
create_collection_if_not_exists()用于检查指定的数据库及其对应的集合是否已经存在,若不存在直接创建集合。
exists_collection()
create_collection_if_not_exists()
def exists_collection(self,
database_name: str,
collection_name: str) -> bool
def create_collection_if_not_exists(self,
name: str,
shard: int,
replicas: int,
description: str = None,
index: Index = None,
embedding: Embedding = None,
timeout: float = None,
ttl_config: dict = None,
) -> Collection:

使用示例

检查 Collection 是否存在
检查 Collection 是否存在,若不存在创建集合
db_name = "db-test"
coll_name = "book-vector"
coll_exists = client.exists_collection(database_name=db_name, collection_name=coll_name)
print(f'Collection {coll_name} exists={coll_exists}')
coll_name = "sdk_collection_exists_test"
db = client.database('db-test')
coll = db.create_collection_if_not_exists(
name=coll_name,
shard=1,
replicas=0,
description='test collection',
index=index,
)
print(f'Collection {coll_name} exists={client.exists_collection(database_name=db_name, collection_name=coll_name)}')
参数名
是否必须
参数含义
database_name
指定检查的数据库名。
collection_name
指定检查的集合名。

输出描述

Collection book-vector exists=True
参数名
返回值
coll_exists
True:集合存在
False:集合不存在