---
title: "Bulk Insert Attachments"
url: "https://docs.ravendb.net/6.2/document-extensions/attachments/bulk-insert"
version: "6.2"
language: "nodejs"
---

# Bulk Insert Attachments

> Client language: Node.js. Also available for [C#](https://docs.ravendb.net/6.2/document-extensions/attachments/bulk-insert.md), [Python](https://docs.ravendb.net/6.2/document-extensions/attachments/bulk-insert.python.md).
>
> RavenDB publishes an official agent skill for coding assistants at https://docs.ravendb.net/skills/ravendb/SKILL.md (also installable with `npx skills add ravendb/skills`). The skill describes itself as: "Read this before writing any RavenDB query, client call or /databases/ request. Covers server setup, document modelling, the Node.js, .NET and Python clients, RQL, indexes, vector search and AI agents. Code written from recall tends to run and return the wrong rows, because RQL resembles SQL and the clients take field names as plain strings."

> **Note**
>
> - [BulkInsert](https://docs.ravendb.net/6.2/client-api/bulk-insert/how-to-work-with-bulk-insert-operation.nodejs.md) is RavenDB's high-performance data insertion operation.  
>   Use its `attachmentsFor` interface to add attachments to documents with great speed.
>
> - In this page:
>   - [Usage flow](#usage-flow)
>   - [Usage example](#usage-example)
>   - [Syntax](#syntax)

## Usage flow

- Create a `bulkInsert` instance.

- Pass the Document ID to the instance's `attachmentsFor` method.

- To add an attachment, call `store`.  
  Pass it the attachment's name, content, and type (optional).  
  The `store` function can be called repeatedly as necessary.

- Note:  
  If an attachment with the specified name already exists on the document,  
  the bulk insert operation will overwrite it.

## Usage example

In this example, we attach a file to all User documents that match a query.

### BulkInsert\_Attachments

```javascript
// Open a session
const session = documentStore.openSession();

// Choose user profiles for which to attach a file
const users = await session.query({ collection: "users" })
  .whereLessThan("age", 30)
  .all();

// Prepare content that will be attached
const text = "Some contents here";
const byteArray = Buffer.from(text);

// Create a bulkInsert instance
const bulkInsert = documentStore.bulkInsert();

try {
  for (let i = 0; i < users.length; i++) {

      // Call `attachmentsFor`, pass the document ID for which to attach the file
      const attachmentsBulkInsert = bulkInsert.attachmentsFor(users[i].id);
      
      // Call 'store' to attach the byte array to the bulkInsert instance
      // The data stored in bulkInsert will be streamed to the server in batches 
      await attachmentsBulkInsert.store("attachmentName", byteArray);
  }
} finally {
  // Call finish to send all remaining data to the server
  await bulkInsert.finish();
}
```

### User\_Class

```javascript
class User {
  constructor(
      id = null,
      age = 0,
      name = ''
  ) {
      Object.assign(this, {
          id,
          age,
          name
      });
  }
}
```

## Syntax

```javascript
attachmentsFor(id);
```

| Parameter | Type | Description |
| - | - | - |
| `id` | `string` | The document ID to which the attachment should be added. |

```javascript
store(name, bytes);
store(name, bytes, contentType);
```

| Parameter | Type | Description |
| - | - | - |
| `name` | `string` | Name of attachment |
| `bytes` | `Buffer` | The attachment's content |
| `contentType` | `string` | Type of attachment (default: null) |

## See also

- [Storing](https://docs.ravendb.net/6.2/document-extensions/attachments/storing.nodejs.md)
- [Loading](https://docs.ravendb.net/6.2/document-extensions/attachments/loading.nodejs.md)
- [Deleting](https://docs.ravendb.net/6.2/document-extensions/attachments/deleting.nodejs.md)
