Document
Insert

Insert

OneNode DB supports MongoDB Extended JSON and OneNode Extended JSON (EmbJSON). This guide explains how to insert documents into OneNode DB using the /insertOne and /insertMany endpoints via the REST API. The following examples include MongoDB Extended JSON format for Object IDs and the use of EmbJSON for embedding text.

To learn more about setting up your Collection URL and API Key, refer to the Collection URL and API Key page.

InsertOne Operation

The insertOne operation is used to insert a single document into a collection.

Endpoint

To insert a single document, use the following endpoint:

{collection_url}/insertOne

Where {collection_url} is the full collection URL that includes your db_id and collection_name. See the Collection URL and API Key page for more details.

Example Python Code for insertOne

Here’s how you can insert a single document using Python, which includes an Object ID and an EmbJSON field:

import os
import requests
 
# API Key and Collection URL
ONENODE_API_KEY = os.getenv('ONENODE_API_KEY')
collection_url = "https://api.onenode.ai/v1/db/123abc/collection/my_collection"
 
# InsertOne URL
url = f"{collection_url}/insertOne"
 
# Document to insert (MongoDB Extended JSON with Object ID and EmbJSON)
data = {
    "_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": "gpt-4o-mini"
        }
    }
}
 
# Headers with API Key
headers = {
    "Authorization": f"Bearer {ONENODE_API_KEY}",
    "Content-Type": "application/json"
}
 
# Sending the request
response = requests.post(url, json=data, headers=headers)
 
# Print the response
print(response.json())

insertOne Response

A successful insert operation will return a JSON response containing the inserted document's ID and a success message:

{
  "status": "success",
  "document_id": "64d2f8f01234abcd5678ef90"
}

InsertMany Operation

The insertMany operation is used to insert multiple documents in one request.

Endpoint

To insert multiple documents, use the following endpoint:

{collection_url}/insertMany

Where {collection_url} is the full collection URL. See the Collection URL and API Key page for more details.

Example Python Code for insertMany

Here’s how you can insert multiple documents using Python, with one of them containing an EmbJSON field:

import os
import requests
 
# API Key and Collection URL
ONENODE_API_KEY = os.getenv('ONENODE_API_KEY')
collection_url = "https://api.onenode.ai/v1/db/123abc/collection/my_collection"
 
# InsertMany URL
url = f"{collection_url}/insertMany"
 
# Documents to insert (MongoDB Extended JSON with Object ID and EmbJSON)
data = [
    {
        "_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": "gpt-4o-mini"
            }
        }
    },
    {
        "_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, json=data, headers=headers)
 
# Print the response
print(response.json())

insertMany Response

A successful insertMany operation will return a JSON response containing the inserted document IDs and a success message:

{
  "status": "success",
  "document_ids": [
    "64d2f8f01234abcd5678ef91",
    "64d2f8f01234abcd5678ef92"
  ]
}