Insert

Inserting documents into OneNode is straightforward. You can save a single document or multiple documents at once with multimodal data types.

Example Code for insert Operation

Here's an example of how to insert documents using Python. You can insert a single document or multiple documents using the same method:

from onenode import Text, Image

# Create multimodal data instances first
bio_text = Text("John is a software engineer with expertise in AI.").enable_index()
profile_image = Image("path/to/profile.jpg").enable_index()

bio_text_2 = Text("Jane is a data scientist specializing in machine learning.").enable_index()
profile_image_2 = Image("path/to/jane_profile.jpg").enable_index()

# Create documents with the instances
docs = [{
    "name": "John Doe",
    "email": "johndoe@example.com",
    "age": 30,
    "bio": bio_text,
    "profile_picture": profile_image
}, {
    "name": "Jane Smith",
    "email": "janesmith@example.com", 
    "age": 28,
    "bio": bio_text_2,
    "profile_picture": profile_image_2
}]

# Insert the documents
response = collection.insert(docs)

print(response.inserted_ids)

Using Custom Embedding Models

You can specify custom embedding models when creating Text and Image instances to optimize for specific use cases. This allows you to use different embedding models for different types of content or to match the models used in your queries.

Here's how to use custom embedding models when creating Text and Image instances in Python:

from onenode import Text, Image, Models

# Create Text instance with custom embedding model
bio_text = Text("John is a software engineer with expertise in AI.").enable_index(
    emb_model=Models.TextToEmbedding.OpenAI.TEXT_EMBEDDING_3_LARGE
)

# Create Image instance with custom vision model  
profile_image = Image("path/to/profile.jpg").enable_index(
    vision_model=Models.ImageToText.OpenAI.GPT_4O
)

# Create document with custom embedding instances
doc = [{
    "name": "John Doe",
    "email": "johndoe@example.com",
    "age": 30,
    "bio": bio_text,
    "profile_picture": profile_image
}]

# Insert the document
response = collection.insert(doc)
print(response.inserted_ids)
Learn More About Multimodal Data Types

Dive deeper into how Text and Image classes work, and explore the available embedding models to unlock the full potential of multimodal search and indexing.

Insert Response

A successful insert operation will return the following JSON response:

{
  "inserted_ids": [
    ObjectId("64d2f8f01234abcd5678ef90"), // BSON ObjectId
    ObjectId("64d2f8f01234abcd5678ef91") // BSON ObjectId
  ]
}
🔑 Auto-generated Document IDs

ObjectId is a unique 12-byte identifier used by MongoDB-style databases. It consists of a timestamp, machine identifier, and a counter, ensuring uniqueness across distributed systems.

When you insert documents without specifying an _id field, OneNode automatically generates a unique ObjectId and assigns it as the document's _id. This ensures every document has a unique identifier for future queries and updates.

How can we improve this documentation?

Got question? Email us and we'll get back to you within 24 hours.