Examples
JSON Documents
Store and query nested JSON data in CogDB.
CogDB can store JSON documents and automatically creates edges for nested data.
Storing JSON Documents
Use putj() to store JSON documents:
from cog.torque import Graph
g = Graph("docs")
g.putj({
"_id": "user1",
"name": "Alice",
"address": {"city": "Toronto", "country": "Canada"}
})How JSON is Stored
CogDB converts JSON into graph edges:
- The
_idfield becomes the document identifier - Each field becomes an edge from the document node
- Nested objects create intermediate nodes
Querying JSON Data
Query the document fields using graph traversal:
# Get the name
g.v("_:_id_user1").out("name").all()
# {'result': [{'id': 'Alice'}]}
# Get the city from nested address
g.v("_:_id_user1").out("address").out("city").all()
# {'result': [{'id': 'Toronto'}]}
# Get the full address
g.v("_:_id_user1").out("address").all()Complex Documents
Store more complex nested structures:
g.putj({
"_id": "order1",
"customer": "user1",
"items": [
{"product": "laptop", "qty": 1},
{"product": "phone", "qty": 2}
],
"total": 1500
})Linking Documents
Reference other documents:
# Find orders for a customer
g.v("_:_id_user1").inc("customer").all()