Using Python to Operate ArangoDB
Using Python to operate the ArangoDB database requires the installation of the pyArango library.
1. Install the pyArango library:
shell
pip install pyArango
2. Connect to ArangoDB database:
python
from pyArango.connection import *
#Create a database connection
conn = Connection(username="root", password="password")
#Get Database
db = conn["mydatabase"]
3. Data insertion:
python
#Get Collection
collection = db["mycollection"]
#Create a document
doc = collection.createDocument()
#Set the properties of the document
doc["name"] = "John Doe"
doc["age"] = 30
#Save Document to Collection
doc.save()
4. Data Query:
python
#Query all documents in the collection
for doc in collection.fetchAll():
print(doc["name"])
#Query documents based on criteria
query = "FOR doc IN mycollection FILTER doc.age > 25 RETURN doc"
results = db.AQLQuery(query)
for doc in results:
print(doc["name"])
5. Data modification:
python
#According to the document_ Modify attributes with key
doc = collection["123"]
doc["age"] = 35
doc.save()
6. Data deletion:
python
#According to the document_ Key Delete Document
doc = collection["123"]
doc.delete()
#Delete all documents in the collection
collection.truncate()
This is a simple example of using the pyArango library to operate an ArangoDB database. According to specific requirements, other functions and operations can also be used to complete more complex operations.