Step 3: Writing
In this step, we will write data to the database we created in Step 1
An item in the database consists of a key, a value, metadata, and a vector. The key uniquely identifies the item. Setting an item with the same key will overwrite the existing item. The value allows you to store arbitrary bytes but is not searchable. Metadata allows you to store additional information about the item to use with query filtering.
Writing data
To add data use the setItem
method, this will upsert the data into the database. Let's insert some items representing a series of jobs:
- JavaScript
- Python
await client.setItem({
databaseId,
key: `job#1`,
value: Buffer.from(`{"id": "1", "posted_by": "user#873240"}`), // Value is stored as bytes, use Buffer.from to convert a string to bytes
vector: [0,1,2,4],
metadata: {
'title': {string: 'Software Engineer'},
'tags': {stringArray: ['software', 'databases']},
'salary': {number: 10000}
}
})
await client.setItem({
databaseId,
key: `job#2`,
value: Buffer.from(`{"id": "2", "posted_by": "user#873241"}`),
vector: [0,1,2,5],
metadata: {
'title': {string: 'Product Manager'},
'tags': {stringArray: ['product', 'management']},
'salary': {number: 12000}
}
})
await client.setItem({
databaseId,
key: `job#3`,
value: Buffer.from(`{"id": "3", "posted_by": "user#873242"}`),
vector: [0,1,2,6],
metadata: {
'title': {string: 'Haskell Hacker'},
'tags': {stringArray: ['haskell', 'functional-programming']},
'salary': {number: 15000}
}
})
await client.setItem({
databaseId,
key: `job#4`,
value: Buffer.from(`{"id": "4", "posted_by": "user#873243"}`),
vector: [0,1,2,7],
metadata: {
'title': {string: 'Frontend Developer'},
'tags': {stringArray: ['frontend', 'javascript']},
'salary': {number: 20000}
}
})
await client.setItem({
databaseId,
key: `job#5`,
value: Buffer.from(`{"id": "5", "posted_by": "user#873244"}`),
vector: [0,1,2,8],
metadata: {
'title': {string: 'Backend Developer'},
'tags': {stringArray: ['backend', 'javascript']},
'salary': {number: 25000}
}
})
await client.set_item(SetItemInput(
database_id=databaseId,
key='job#1',
value=b'{"id": "1", "posted_by": "user#873240"}',
vector=[0,1,2,4],
metadata={
'title': MetadataValueString(value='Software Engineer'),
'tags': MetadataValueStringArray(value=['software', 'databases']),
'salary': MetadataValueNumber(value=10000)
}
))
await client.set_item(SetItemInput(
database_id=databaseId,
key='job#2',
value=b'{"id": "2", "posted_by": "user#873241"}',
vector=[0,1,2,5],
metadata={
'title': MetadataValueString(value='Product Manager'),
'tags': MetadataValueStringArray(value=['product', 'management']),
'salary': MetadataValueNumber(value=12000)
}
))
await client.set_item(SetItemInput(
database_id=databaseId,
key='job#3',
value=b'{"id": "3", "posted_by": "user#873242"}',
vector=[0,1,2,6],
metadata={
'title': MetadataValueString(value='Haskell Hacker'),
'tags': MetadataValueStringArray(value=['haskell', 'functional-programming']),
'salary': MetadataValueNumber(value=15000)
}
))
await client.set_item(SetItemInput(
database_id=databaseId,
key='job#4',
value=b'{"id": "4", "posted_by": "user#873243"}',
vector=[0,1,2,7],
metadata={
'title': MetadataValueString(value='Frontend Developer'),
'tags': MetadataValueStringArray(value=['frontend', 'javascript']),
'salary': MetadataValueNumber(value=20000)
}
))
await client.set_item(SetItemInput(
database_id=databaseId,
key='job#5',
value=b'{"id": "5", "posted_by": "user#873244"}',
vector=[0,1,2,8],
metadata={
'title': MetadataValueString(value='Backend Developer'),
'tags': MetadataValueStringArray(value=['backend', 'javascript']),
'salary': MetadataValueNumber(value=25000)
}
))
Using Metadata
Metadata is used to store data that can be used for filtering. In the example above, we stored the job title, tags, and salary as metadata. This data can be used to filter jobs based on those fields and vector similarity.
When adding metadata, you can use the following types:
string
: A string valuestringArray
: An array of stringsnumber
: A number value, stored as a doublenumberArray
: An array of numbers, stored as a double array
Deleting data
To delete a document, use the deleteItem
method:
- JavaScript
- Python
await client.deleteItem({databaseId, key: 'job#1'});
await client.delete_item(DeleteItemInput(database_id=databaseId, key="job#1"))