Skip to main content

C8QL Docs Queries Example

This page demonstrates how you can use C8QL and the Macrometa API to run CRUD operations on document collection records. For more information about C8QL queries, refer to C8QL and Queries.

Prerequisites

Code Sample

  1. Copy and paste the code block below in your favorite IDE.
  2. Update constants with your values, such as the API key.
  3. Run the code.
import json
import requests

# Define constants
URL = "api-play.paas.macrometa.io"
HTTP_URL = f"https://{URL}"
GEO_FABRIC = "_system"
API_KEY = "XXXXX" # Change this to your API key
AUTH_TOKEN = f"apikey {API_KEY}"
COLLECTION = "abc"

# Authenticate and log in
print("1. Connecting to GDN")

# Create HTTPS session
session = requests.session()
session.headers.update({"content-type": 'application/json'})
session.headers.update({"authorization": AUTH_TOKEN})

# Create collection
print(f"2. Creating collection {COLLECTION}")
url = f"{HTTP_URL}/_fabric/{GEO_FABRIC}/_api/collection"
resp = session.post(url, json={
"name": COLLECTION
})
resp_json = resp.json()
if resp_json.get("error") == False:
print("Collection created successfully.")
elif resp_json.get("errorNum") == 1207:
print("Collection already exists.")
else:
print(resp_json)

# Setting cursor url
url = f"{HTTP_URL}/_fabric/{GEO_FABRIC}/_api/cursor"

# Insert documents to the collection
print(f"3. Inserting data in collection {COLLECTION}")
resp = session.post(url, json={
"query": "INSERT{'name' : 'Julie', 'company' : 'ABC', '_key' : 'Julie'}" \
f"INTO {COLLECTION}"
})
resp_json = resp.json()
if resp_json.get("error") == False:
print("Document inserted successfully.")
else:
print(resp_json)

# Read from the collection
print(f"4. Reading data from collection {COLLECTION}")
resp = session.post(url, json={
"query": f"FOR doc IN {COLLECTION} RETURN doc"
})
resp_json = resp.json()
if resp_json.get("error") == False:
print("Documents retrieved successfully. Data: ")
for doc in resp_json.get("result", []):
print(json.dumps(doc, indent=4))
else:
print(resp_json)

# Update documents in the collection
print(f"5. Update data from collection {COLLECTION}")
resp = session.post(url, json={
"query": f"FOR c IN {COLLECTION} UPDATE c WITH{{'company':'XYZ'}} IN {COLLECTION}"
})
resp_json = resp.json()
if resp_json.get("error") == False:
print("Document updated successfully.")
else:
print(resp_json)

# Upsert documents in the collection
print(f"6. Upsert data from collection {COLLECTION}")
resp = session.post(url, json={
"query": "UPSERT {name: 'John'} INSERT "
"{_key:'John', name: 'John', logins:1, updatedAt: DATE_NOW()}"
f" UPDATE {{'logins': OLD.logins + 1, updatedAt: DATE_NOW()}} IN {COLLECTION}"
})
resp_json = resp.json()
if resp_json.get("error") == False:
print("Document upserted successfully.")
else:
print(resp_json)

# Read from the collection after upsert
print(f"7. Reading data from collection {COLLECTION}")
resp = session.post(url, json={
"query": f"FOR doc IN {COLLECTION} RETURN doc"
})
resp_json = resp.json()
if resp_json.get("error") == False:
print("Documents retrieved successfully after upsert. Data: ")
for doc in resp_json.get("result", []):
print(json.dumps(doc, indent=4))
else:
print(resp_json)

# Delete documents in the collection
print(f"8. Delete data from collection {COLLECTION}")
resp = session.post(url, json={
"query": f"FOR c IN {COLLECTION} REMOVE c IN {COLLECTION}"
})
resp_json = resp.json()
if resp_json.get("error") == False:
print("Documents deleted successfully.")
else:
print(resp_json)