Node.js
Learn how to connect to Milvus databases in Sealos DevBox using Node.js
This guide will walk you through the process of connecting to a Milvus database using Node.js within your Sealos DevBox project.
Prerequisites
- A Sealos DevBox project with Node.js environment
- A Milvus database created using the Database app in Sealos
Install Required Packages
In your Cursor terminal, install the necessary packages:
This command installs:
@zilliz/milvus2-sdk-node
: The official Milvus Node.js SDKdotenv
: A zero-dependency module that loads environment variables from a.env
file
Connection Setup
Set up the environment variables
First, let's set up the environment variables for our Milvus connection. Create a .env
file in your project root with the following content:
Replace the placeholders with your actual Milvus credentials from the Database app in Sealos.
Create a connection file
Create a new file named milvusClient.js
with the following content:
Create database operations
Now, let's create a file named milvusOperations.js
to handle our database operations:
Create a main script
Finally, let's create a main.js
file to demonstrate all the operations:
Usage
To run the script, use the following command in your Cursor terminal:
This will execute all the operations defined in the main
function, demonstrating the connection to Milvus, collection creation, data insertion, and vector search.
Best Practices
- Use environment variables for Milvus connection details.
- Create indexes for better search performance.
- Load collections before performing search operations.
- Implement proper error handling.
- Use batch operations for inserting multiple vectors.
- Release resources by releasing collections when they're no longer needed.
Troubleshooting
If you encounter connection issues:
- Verify your Milvus credentials in the
.env
file. - Ensure your Milvus database is running and accessible.
- Check for any network restrictions in your DevBox environment.
- Confirm that the required packages are correctly installed.
- Verify that the vector dimensions match your collection schema.
For more detailed information on using Milvus with Node.js, refer to the Milvus Node.js SDK documentation.
Last updated on