Insert
OneNode DB supports MongoDB Extended JSON and OneNode Extended JSON (EmbJSON). This guide explains how to insert documents into OneNode DB using the /document
endpoint via the REST API. The following examples demonstrate the use of MongoDB Extended JSON format for Object IDs and EmbJSON for embedding text.
Endpoint
To insert documents, use the following endpoint:
{collection_url}/document
Where {collection_url}
is the complete URL of the collection, which includes your db_id
and collection_name
.
Example: Python 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 endpoint. This example includes documents with Object IDs and EmbJSON fields:
import requests
import json
# Hardcoded API Key and Collection URL (example usage only, not for production)
ONENODE_API_KEY = "your_example_api_key_here"
collection_url = "https://api.onenode.ai/v1/db/123abc/collection/my_collection"
# Insert Document URL
url = f"{collection_url}/document"
# Documents to insert (MongoDB Extended JSON with Object ID and EmbJSON)
data = {
"documents": [
{
"_id": {"$oid": "64d2f8f01234abcd5678ef90"},
"name": "John Doe",
"email": "johndoe@example.com",
"age": 30,
"bio": {
"@embText": {
"text": "John is a software engineer with expertise in AI.",
"model": "text-embedding-3-small"
}
}
},
{
"_id": {"$oid": "64d2f8f01234abcd5678ef91"},
"name": "Alice Smith",
"email": "alice@example.com",
"age": 25,
"bio": {
"@embText": {
"text": "Alice is a data scientist with a background in machine learning.",
"model": "text-embedding-3-small"
}
}
},
{
"_id": {"$oid": "64d2f8f01234abcd5678ef92"},
"name": "Bob Johnson",
"email": "bob@example.com",
"age": 40
}
]
}
# Headers with API Key
headers = {
"Authorization": f"Bearer {ONENODE_API_KEY}",
"Content-Type": "application/json"
}
# Sending the request
response = requests.post(url, headers=headers, data=json.dumps(data))
# Print the response
print(response.json())
Insert Response
A successful insert operation will return the following JSON response:
{
"inserted_ids": [
"64d2f8f01234abcd5678ef90",
"64d2f8f01234abcd5678ef91",
"64d2f8f01234abcd5678ef92"
],
"task_id": "abc123xyz" // or `null` if all processes were synchronously completed
}
The inserted_ids
field contains the IDs of the successfully inserted documents. The task_id
field will have a value if there is an asynchronous task related to EmbJSON. If task_id
is null
, it indicates that all processes, including embedding and indexing, were completed synchronously.
If you need additional help with the OneNode DB API, feel free to explore our documentation or reach out to support.