使用Python操作ArangoDB的方法步骤
2020-02-13 11:30
357 查看
前面说过怎样使用 ArangoDB 的 Web,Shell 和 Restful API 来操作数据库,今天看一下怎样使用Python语言来操作ArangoDB数据库。
要通过 Python 脚本来访问 ArangoDB,我们需要先安装 pyArango 库
$ pip install pyArango
下面写了一个简单的 Python 脚本,其中包含了一些常用的操作,如:创建连接,数据库操作,集合操作和文档操作等。
from pyArango.connection import * # 创建连接 conn = Connection(arangoURL='http://106.54.228.237:8529', username="root", password="Letmein") print('conn: %s' % conn) # 创建数据库 if not conn.hasDatabase('mydb'): conn.createDatabase(name="mydb") db = conn['mydb'] print('db: %s' % db) # 创建集合 if not db.hasCollection('users'): db.createCollection(name='users') collection = db['users'] print('collection: %s' % collection) # 插入文档数据 print('collection count before insert: %s' % collection.count()) for i in range(0, 10): user = { 'name': 'user_' + str(i), 'age': 20 + i, 'address': { 'home': 'home address', 'office': 'office address' } } collection.createDocument(user).save() print('collection count after insert: %s' % collection.count()) # 分页查询文档 print('fetchAll ...') query = collection.fetchAll(skip=5, limit=2) for doc in query: print(doc) # 按条件查询文档 print('fetchByExample ...') query = collection.fetchByExample({'name': 'user_5'}, batchSize=10, count=True) for doc in query: print(doc) # 使用AQL查询文档 print('query by AQL ...') aql = "FOR user IN users FILTER user.name == @name || user.age > 25 LIMIT 5 RETURN user" bindVars = {'name': 'user_0'} query = db.AQLQuery(aql, rawResults=False, batchSize=1, bindVars=bindVars) for doc in query: print(doc) # 删除文档 print('collection count before delete: %s' % collection.count()) query = collection.fetchAll() for doc in query: doc.delete() print('collection count after delete: %s' % collection.count()) # 删除集合 collection.delete()
参考文档
https://pyarango.readthedocs.io/en/stable/
以上就是本文的全部内容,希望对大家的学习有所帮助
您可能感兴趣的文章:
相关文章推荐
- python基础(文件输入/输出 内建类型 字典操作使用方法)
- Python操作列表之List.insert()方法的使用
- python中使用pyexcel操作excel方法
- 在Python中操作字符串之replace()方法的使用
- 在Python中操作时间之tzset()方法的使用教程
- python 列表操作 及 使用方法
- Windows10上使用VS2017编译OpenCV3.4.2+OpenCV_Contrib3.4.2+Python3.6.2操作步骤
- 在Python中操作字符串之rstrip()方法的使用
- 在Python中操作字典之setdefault()方法的使用
- 在Python程序中操作文件之isatty()方法的使用教程
- py2exe使用方法详解 Python生成exe步骤
- python中关于操作时间的方法(二):使用datetime模块
- Python切片怎么操作使用,这才是正确的方法!
- selenium+python自动化测试之使用webdriver操作浏览器的方法
- python操作mysql数据库的常用方法使用详解
- 在Python中操作字符串之startswith()方法的使用
- Python操作列表之List.insert()方法的使用
- 在Python中操作日期和时间之gmtime()方法的使用
- 在Python中操作时间之strptime()方法的使用
- 在Python中使用next()方法操作文件的教程