Manage Data

This topic describes how to insert and delete data in Milvus.

Insert data

First, prepare the data to insert.

This topic inserts randomly generated 2,000 rows of eight-dimensional vector data as the example data. Real applications will likely use much higher dimensional vectors than this. You can prepare your own data to replace the example.

import random
vectors = [[random.random() for _ in range(8)] for _ in range(2000)]
entities = [vectors]
const entities = Array.from({ length: 2000 }, () => ({
  ["example_field"]: Array.from({ length: 8 }, () => Math.random()),
}));

Insert the data to the collection. By specifying partition_name, you can decide to which partition to insert the data.

With the collection schema auto_id enabled, Milvus automatically assigns an ID (primary key value) to each inserted data.

from pymilvus import collection
collection = Collection("example_collection")      # Get an existing collection.
mr = collection.insert(entities)
const mr = await milvusClient.dataManager.insert({{
  collection_name: "example_collection",
  fields_data: entities,
});
Parameter Description
collection_name Name of the collection to get.
data Data to insert into Milvus.
partition_name (optional) Name of the partition to insert data into.
Parameter Description
collection_name Name of the collection to get.
partition_name (optional) Name of the partition to insert data into.
fields_data Data to insert into Milvus.

After the data are inserted, Milvus returns MutationResult as an object. You can check the value of MutationResult, which contains the corresponding primary keys of the inserted data.

mr.primary_keys
console.log(mr.IDs) 
[425790736918318406, 425790736918318407, 425790736918318408, ...]

Delete entities

Milvus supports deleting entities by primary key specified with boolean expression.

  • The delete operation is irreversible. Deleted entities cannot be retrieved again.
  • Frequent delete operations will impact the system performance.

All CRUD operations within Milvus are executed in memory. Before deleting, load the collection that contains the entities you expect to delete to memory.

from pymilvus import collection
collection = Collection("example_collection")      # Get an existing collection.
collection.load()
await milvusClient.collectionManager.loadCollection({
  collection_name: "example_collection",
});

Prepare the boolean expression that filters the entities to delete. See Boolean Expression Rules for more information.

The following example filters data with primary key values of 425790736918318406 and 425790736918318407.

expr = "pk in [425790736918318406,425790736918318407]"
const expr = "pk in [425790736918318406,425790736918318407]";

Delete the entities with the boolean expression you created. By specifying partition_name, you can decide from which partition to delete the entities and thus save the resources.

collection.delete(expr)
await milvusClient.dataManager.deleteEntities({
  collection_name: "example_collection",
  expr: expr,
});
Parameter Description
expr Boolean expression that specifies the entities to delete.
partition_name (optional) Name of the partition to delete entities from.

You can verify the delete operation by checking the number of entities after deleting.

collection.num_entities
1998
const res = await collectionManager.getCollectionStatistics({
  collection_name: "example_collection",
});
console.log(res.data.row_count);

What's next

Is this page helpful?
Scored Successfully!