# Uploading Data to a Collection

Data points of all types are uploaded into Hyperspace Collection as documents and stored according to the identifier you specify during upload, as described below. Data upload can be performed in batches or by uploading a single vector, as follows.

## Uploading a Single Document

Use the following command to upload a single document –

{% tabs %}
{% tab title="Python" %}

<pre class="language-python" data-line-numbers><code class="lang-python">document = { "category": "product",
             "vec1" : [0,1]
           }
             
<strong>hyperspace_client.add_document(document, collection_name)
</strong></code></pre>

{% endtab %}

{% tab title="Java" %}
{% code lineNumbers="true" %}

```java
Document document = new Document();
document.putAdditionalProperty("category", "product");
document.putAdditionalProperty("vec1", [0,0,1]);
document.putAdditionalProperty("vec2", [0,1,0]);
client.addDocument(collectionName, document, true, false);
```

{% endcode %}
{% endtab %}

{% tab title="JavaScript" %}

<pre class="language-javascript" data-line-numbers><code class="lang-javascript"><strong>const document = {"category": "product",
</strong>                    "vec1" : [0,0,1],
                    "vec2" : [0,1,0]};
             
await hyperspaceClient.index({
    index: collectionName,
    body: document 
});
</code></pre>

{% endtab %}
{% endtabs %}

**Where –**

* <mark style="color:purple;">document</mark> – Represents the document to upload. The structure of each document must be according to the database schema configuration file. Must be of **type dictionary**.
* <mark style="color:purple;">collection\_name</mark> – Specifies the name of the Collection into which to load the document.

## **Assigning Id to a Document**

Each document must have a unique identifier, under the field "\_*id". You can manually set an id per document by defining a designated field named "\_*&#x69;d" in the document. Use the following example-

{% tabs %}
{% tab title="Python" %}

<pre class="language-python" data-line-numbers><code class="lang-python">document = {"_id": "1",
             "category": "product",
             "vec1" : [0,1]
           }
             
<strong>hyperspace_client.add_document(document, collection_name)
</strong></code></pre>

{% endtab %}

{% tab title="Java" %}
{% code lineNumbers="true" %}

```java
Document document = new Document();
document.setId("1");
document.putAdditionalProperty("category", "product");
document.putAdditionalProperty("vec1", [0,0,1]);
document.putAdditionalProperty("vec2", [0,1,0]);
client.addDocument(collectionName, document, true, false);
```

{% endcode %}
{% endtab %}

{% tab title="JavaScript" %}

<pre class="language-javascript" data-line-numbers><code class="lang-javascript"><strong>const document = {"category": "product",
</strong>                    "vec1" : [0,0,1],
                    "vec2" : [0,1,0]};
             
await hyperspaceClient.index({
    id: "1",
    index: collectionName,
    body: document 
});
</code></pre>

{% endtab %}
{% endtabs %}

If no *id is assigned in the document, "\_id"* will be assigned automatically.&#x20;

## Uploading a Batch of Documents

Data can be uploaded in batches by conversion of the data points to a document object before  uploading. The basic data point object for the Hyperspace database is a document of type dictionary.

**To upload a batch of documents into a Collection –**

For verification purposes, we recommend that you upload data to a Collection in batches of documents each which has the structure specified in the data schema configuration file.

The following code snippet builds a list of documents in a temporary variable named batch and then uploads each batch using –

{% tabs %}
{% tab title="Python" %}
{% code lineNumbers="true" %}

```
hyperspace_client.add_batch(batch, collection_name)
```

{% endcode %}
{% endtab %}

{% tab title="Untitled" %}
{% code lineNumbers="true" %}

```java
hyperspaceClient.addBatch(collection_name, batch);
```

{% endcode %}
{% endtab %}

{% tab title="JavaScript" %}
{% code lineNumbers="true" %}

```javascript
await hyperspaceClient.addBatch(collection_name, batch);
```

{% endcode %}
{% endtab %}
{% endtabs %}

The following example uploads batches of 250 documents for. Documents are added to the batch, and once a batch reaches 250 documents, it's uploaded to the Hyperspace Collection.

**Copy the following code snippet**

{% tabs %}
{% tab title="Python" %}
{% code lineNumbers="true" %}

```python
BATCH_SIZE = 250
batch = []
collection_name = "new_collection"
for i, document in enumerate(documents):
   batch.append(document )
   if (i+1) % BATCH_SIZE == 0:
      response = hyperspace_client.add_batch(batch, collection_name)
      batch.clear()
      
if batch:
  response = hyperspace_client.add_batch(batch, collection_name)
hyperspace_client.commit(collection_name)
```

{% endcode %}
{% endtab %}

{% tab title="Java" %}
{% code lineNumbers="true" %}

```java
import java.util.ArrayList;
final int batchSize = 250;

for (int i= 0; index < documents.size(); i++) {
    batch.add(documents.get(i));
    if ((i+ 1) % batchSize == 0) {
          List<DataPoint> batchCopy = new ArrayList<>(batch);
          futures.add(hyperspaceClient.addBatch(batchCopy, collectionName));
          batch.clear();
      }    
}

if (!batch.isEmpty()) {
    futures.add(hyperspaceClient.addBatch(new ArrayList<>(batch), collectionName));
}
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join();
hyperspaceClient.commit(collectionName).join();
```

{% endcode %}
{% endtab %}

{% tab title="JavaScript" %}

<pre class="language-javascript" data-line-numbers><code class="lang-javascript">const batchSize = 250;
<strong>let batch = [];
</strong>
documents.forEach((dataPoint, index) => {
    batch.push(dataPoint);
    if ((index + 1) % batchSize === 0) {
        await hyperspaceClient.addBatch(batch, collectionName);
        batch = [];
    }
});

if (batch.length > 0) {
    await hyperspaceClient.addBatch(collectionName, documents)
<strong>};
</strong>hyperspaceClient.commit(collectionName);
</code></pre>

{% endtab %}
{% endtabs %}

**Where** –

* <mark style="color:purple;">document</mark> – Represents the document to upload. The structure of each document must be according to the database schema configuration file. Must be of **type dictionary**.
* <mark style="color:purple;">BATCH\_SIZE</mark> – Specifies the number of documents in a batch.
* <mark style="color:purple;">commit -</mark> is required for vector search only. commit should only be performed after the data upload is complete.

In this method, each <mark style="color:purple;">document</mark> will be assigned with an automatic identifier.

{% hint style="info" %}
Optimizing the batch size can improve the data upload speed. Larger batches will be uploaded faster, but in case of a upload failure (i.e. mismatch between a document and the data schema), the whole batch should be re-uploaded
{% endhint %}

**To manually assign Id to documents, copy the following code snippet**

{% tabs %}
{% tab title="Python" %}
{% code lineNumbers="true" %}

```python
BATCH_SIZE = 250
batch = []
for i, data_point in enumerate(documents):
   data_point["Id"] = str(i)
   batch.append(data_point)
   if (i+1) % BATCH_SIZE == 0:
      response = hyperspace_client.add_batch(batch, collection_name)
      batch.clear()
      
if batch:
  response = hyperspace_client.add_batch(batch, collection_name)
hyperspace_client.commit(collection_name)
```

{% endcode %}
{% endtab %}

{% tab title="Java" %}
{% code lineNumbers="true" %}

```java
import java.util.ArrayList;
final int batchSize = 250;

for (int i = 0; index < documents.size(); i++) {
    Document document = documents.get(i);
    String Id= String.valueOf(i);    
    document.setId(Id);
    
    batch.add(document);
    if ((i+ 1) % batchSize == 0) {
          List<DataPoint> batchCopy = new ArrayList<>(batch);
          futures.add(hyperspaceClient.addBatch(batchCopy, collectionName));
          batch.clear();
      }    
}

if (!batch.isEmpty()) {
    futures.add(hyperspaceClient.addBatch(new ArrayList<>(batch), collectionName));
}
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join();
hyperspaceClient.commit(collectionName).join();
```

{% endcode %}
{% endtab %}

{% tab title="JavaScript" %}
{% code lineNumbers="true" %}

```javascript
const batchSize = 250;
let batch = [];

documents.forEach((dataPoint, index) => {
    dataPoint["Id"] = String(i);
    batch.push(dataPoint);
    if ((index + 1) % batchSize === 0) {
        await hyperspaceClient.addBatch(batch, collectionName);
        batch = [];
    }
});

if (batch.length > 0) {
    await hyperspaceClient.addBatch(collectionName, documents)
}
hyperspaceClient.commit(collection_name);
```

{% endcode %}
{% endtab %}
{% endtabs %}

**Where** –

* <mark style="color:purple;">Id</mark> - Represents the id field of the documents. The field should be set in the [Database Schema Configuration file](https://docs.hyper-space.io/hyperspace-docs/projects/setting-up/creating-a-database-schema-configuration-file)
* <mark style="color:purple;">i</mark> – Specifies the identifier that you assign to the document that you are uploading, which must be unique per Collection. You can assign any identifier as long as it's unique.

This step is optional. If no id is defined in the data schema configuration file, automatic Id will be set during the upload.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.hyper-space.io/hyperspace-docs/flows/setting-up/uploading-data-to-a-collection.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
