This page serves as a guide on how to use the add() method in Memora. Before moving on, it’s a good idea to familiarize yourself with our main documents and collections pages.

Basic usage

Here’s how you can use the add() method to upload a document to Memora:

import Memora

memora = Memora.auth('your-api-key')

(memora
  .add('the text from your document')
  .go()
)

In the above example, we added a document to Memora that:

Syntax

add(content)
add(contents)

Parameters

content
string

This represents the text content of your document. Memora uses this content for document retrieval.

contents
string[]

If you pass an array of strings, Memora will use its batch mode to insert multiple documents simultaneously.

Even in batch mode, each insertion counts as a single request. For Hobby plan users, there’s a rate limit of 30 requests per minute. Therefore, you cannot perform more than 30 insertions at a time if you’re on the Hobby plan.

To learn more about limits, click here.

Return value

When you use the add() method, it returns the following:

// If adding a single document:
{
  sucess: boolean,
  id: 'e581f410-2fb7-40d8-880b-2327a4f15247'
}

// If adding in batch mode:
[
  {
    sucess: boolean,
    id: 'e581f410-2fb7-40d8-880b-2327a4f15247'
  },
  // ...
  {
    sucess: boolean,
    id: '297db465-d009-472d-bec5-ff368c099db4'
  }
]
// The responses are in the same order as the documents you passed to add()

Adding documents to a collection

Documents are always stored inside a collection. In the previous examples, we didn’t specify the collection, so the documents were added to your account’s default collection.

Example

Specifying a collection is simple: just use the on() method before calling add().

(memora
  .on('collection-name')
  .add(content)
  .go()
)

The on() method can be used even in batch mode.

Syntax

on(collection)

Parameters

collection
string
required

This is the collection name to which you want to add the document. If no collection exists with the supplied name, Memora will create a new one for you and insert the document into it.

Collection name

Certain rules apply when naming your collection. Here’s what you can include:

NameExample
English lettersabcXYZ
Numbersabc123XYZ
Special characters (_, -, ., @ and :)@abc.1:23-XYZ_

Adding metadata

Documents can also have also have metadata. You can pass any object as metadata.

Example

Here’s an example of adding a document with metadata into Memora:

(
  memora
    .add('Life is like a box of chocolates.')
    .metadata({ from: 'Forrest Gump' })
    .go()
)

Syntax

metadata(metadata)
metadata(metadatas)

Parameters

metadata
serializable object

This is the metadata associated with the document you’re adding to Memora.

metadatas
serializable objects[]

This is an array of metadata objects, each corresponding in order to the documents you’re adding in batch mode with add().

Example: Adding multiple documents with metadata

citations = [
  'Life is like a box of chocolates.',
  'May the Force be with you.'
]

metadata = [
  { 'from': 'Forrest Gump' },
  { 'from': 'Stars Wars' }
]

(
  memora
    .add(citations)
    .metadata(metadata)
    .go()
)

Wrapping up

That’s all you need to know about the add() method for now. With this knowledge, you should be able to easily add documents, complete with metadata, to any collection in Memora.

In the next section, we will explore the find() method, which is how you retrieve documents from your collections in Memora.

If you have any more questions or need further clarification, feel free to reach out to us through our support channels.