Collection Management
Collections are the primary data structure in Milvus, similar to tables in traditional databases. This guide covers all collection management operations.
Creating Collections
Basic Collection Creation
import { MilvusClient, DataType } from '@zilliz/milvus2-sdk-node';
const schema = [
{
name: 'id',
data_type: DataType.Int64,
is_primary_key: true,
autoID: true,
},
{
name: 'vector',
data_type: DataType.FloatVector,
dim: 128,
},
];
await client.createCollection({
collection_name: 'my_collection',
fields: schema,
});Collection with Consistency Level
await client.createCollection({
collection_name: 'my_collection',
fields: schema,
consistency_level: 'Bounded', // 'Strong', 'Session', 'Bounded', 'Eventually'
});Collection with Properties
await client.createCollection({
collection_name: 'my_collection',
fields: schema,
properties: {
'collection.ttl.seconds': 3600, // TTL in seconds
},
});Collection with Partition Configuration
await client.createCollection({
collection_name: 'my_collection',
fields: schema,
num_partitions: 8, // Number of partitions
});Checking Collection Existence
Check if a collection exists:
const exists = await client.hasCollection({
collection_name: 'my_collection',
});
console.log('Collection exists:', exists.value);Listing Collections
List all collections:
const result = await client.showCollections();
console.log('Collections:', result.collection_names);
// Output: ['collection1', 'collection2', 'my_collection']List collections with details:
const result = await client.showCollections({
type: ShowCollectionsType.All, // 'All' or 'InMemory'
});
result.data.forEach(collection => {
console.log(`Collection: ${collection.name}`);
console.log(`ID: ${collection.id}`);
console.log(`Loaded: ${collection.loadedPercentage}%`);
});Describing Collections
Get detailed information about a collection:
const result = await client.describeCollection({
collection_name: 'my_collection',
});
console.log('Schema:', result.schema);
console.log('Collection ID:', result.collectionID);
console.log('Consistency Level:', result.consistency_level);Modifying Collections
Alter Collection Properties
Modify collection properties:
await client.alterCollectionProperties({
collection_name: 'my_collection',
properties: {
'collection.ttl.seconds': 7200,
},
});Rename Collection
Rename a collection:
await client.renameCollection({
collection_name: 'old_name',
new_collection_name: 'new_name',
});Drop Collection Properties
Remove specific properties:
await client.dropCollectionProperties({
collection_name: 'my_collection',
property_names: ['collection.ttl.seconds'],
});Dynamic Schema
Adding Fields to Existing Collection
Add a single field:
await client.addCollectionField({
collection_name: 'my_collection',
field: {
name: 'new_field',
data_type: DataType.Int64,
description: 'A new field',
},
});Add multiple fields:
await client.addCollectionFields({
collection_name: 'my_collection',
fields: [
{
name: 'field1',
data_type: DataType.Int64,
},
{
name: 'field2',
data_type: DataType.VarChar,
max_length: 256,
},
],
});Alter Field Properties
Modify field properties:
await client.alterCollectionFieldProperties({
collection_name: 'my_collection',
field_name: 'my_field',
properties: {
'field.max_length': 512,
},
});Collection Statistics
Get collection statistics:
const stats = await client.getCollectionStatistics({
collection_name: 'my_collection',
});
console.log('Row count:', stats.row_count);
console.log('Statistics:', stats.stats);Collection Loading
Load Collection
Load a collection into memory for search:
// Synchronous load (waits until loaded)
await client.loadCollectionSync({
collection_name: 'my_collection',
});
// Asynchronous load (returns immediately)
await client.loadCollectionAsync({
collection_name: 'my_collection',
});Check Loading Progress
const progress = await client.getLoadingProgress({
collection_name: 'my_collection',
});
console.log('Loading progress:', progress.progress);Check Load State
const state = await client.getLoadState({
collection_name: 'my_collection',
});
console.log('Load state:', state.state); // 'LoadStateNotExist', 'LoadStateNotLoad', 'LoadStateLoading', 'LoadStateLoaded'Refresh Load
Refresh the loaded collection:
await client.refreshLoad({
collection_name: 'my_collection',
});Release Collection
Release a collection from memory:
await client.releaseCollection({
collection_name: 'my_collection',
});Collection Aliases
Create Alias
Create an alias for a collection:
await client.createAlias({
collection_name: 'my_collection',
alias: 'my_alias',
});Describe Alias
Get information about an alias:
const result = await client.describeAlias({
alias: 'my_alias',
});
console.log('Collection name:', result.collection);
console.log('Alias:', result.alias);List Aliases
List all aliases for a collection:
const result = await client.listAliases({
collection_name: 'my_collection',
});
console.log('Aliases:', result.aliases);Alter Alias
Change which collection an alias points to:
await client.alterAlias({
collection_name: 'new_collection',
alias: 'my_alias',
});Drop Alias
Remove an alias:
await client.dropAlias({
alias: 'my_alias',
});Collection Compaction
Compact Collection
Trigger compaction:
const result = await client.compact({
collection_name: 'my_collection',
});
console.log('Compaction ID:', result.compactionID);Get Compaction State
Check compaction status:
const state = await client.getCompactionState({
compactionID: 12345,
});
console.log('State:', state.state);Get Compaction Plans
Get detailed compaction plans:
const plans = await client.getCompactionStateWithPlans({
compactionID: 12345,
});
console.log('Plans:', plans.mergeInfos);Collection Replicas
Get replica information:
const replicas = await client.getReplicas({
collection_name: 'my_collection',
});
console.log('Replicas:', replicas.replicas);Primary Key Information
Get Primary Key Field Name
const pkField = await client.getPkFieldName({
collection_name: 'my_collection',
});
console.log('Primary key field:', pkField);Get Primary Key Field Type
const pkType = await client.getPkFieldType({
collection_name: 'my_collection',
});
console.log('Primary key type:', pkType);Get Primary Key Field Schema
const pkFieldSchema = await client.getPkField({
collection_name: 'my_collection',
});
console.log('Primary key schema:', pkFieldSchema);Dropping Collections
Drop a collection (this permanently deletes all data):
await client.dropCollection({
collection_name: 'my_collection',
});Best Practices
- Naming: Use descriptive collection names
- Schema Design: Plan your schema carefully before creating collections
- Loading: Load collections before performing searches
- Aliases: Use aliases for easier collection management and zero-downtime updates
- Compaction: Regularly compact collections to optimize performance
- Release: Release collections from memory when not in use to free resources
Next Steps
- Learn about Partition Management
- Explore Index Management
- Check out Data Operations