Official Python client for RavenDB NoSQL Database ๐
Installation
Install from PyPi, as ravendb.
Introduction and changelog
Python client API (v7.2) for RavenDB, a NoSQL document database.
Type-hinted entire project and API results - using the API is now much more comfortable with IntelliSense
Releases

What's new?
Changes available in the releases section.
Documentation
-
This readme provides short examples for the following:
Getting started,
Crud example,
Query documents,
Attachments,
Changes API,
Suggestions,
Patching,
Subscriptions,
Counters,
Bulk Insert,
Using classes,
Working with secure server,
Building & running tests
-
For more information go to the online RavenDB Documentation.
Getting started
- Import the
DocumentStore class from the ravendb module
from ravendb import DocumentStore
- Initialize the document store (you should have a single DocumentStore instance per application)
store = DocumentStore('http://live-test.ravendb.net', 'databaseName')
store.initialize()
- Open a session
with store.open_session() as session:
- Call
save_changes() when you're done
user = session.load('users/1-A')
user.name = "Gracjan"
session.save_changes()
# Data is now persisted
# You can proceed e.g. finish web request
CRUD example
Store documents
product = Product(
Id=None,
title='iPhone 14 Pro Max',
price=1199.99,
currency='USD',
storage=256,
manufacturer='Apple',
in_stock=True,
last_update=datetime.datetime.now(),
)
session.store(product, 'products/1-A')
print(product.Id) # products/1-A
session.save_changes()
Some related tests:
store()
ID generation - session.store()
store document with @metadata
storing docs with same ID in same session should throw
Load documents
product = session.load('products/1-A')
print(product.title) # iPhone 14 Pro Max
print(product.Id) # products/1-A
Related tests:
load()
Load documents with include
# users/1
# {
# "name": "John",
# "kids": ["users/2", "users/3"]
# }
session = store.open_session()
user1 = session.include("kids").load("users/1")
# Document users/1 and all docs referenced in "kids"
# will be fetched from the server in a single request.
user2 = session.load("users/2")
# this won't call server again
assert(user1 is not None)
assert(user2 is not None)
assert(session.advanced.number_of_requests == 1)
Related tests:
can load with includes
Update documents
import datetime
product = session.load('products/1-A')
product.in_stock = False
product.last_update = datetime.datetime.now()
session.save_changes()
# ...
product = session.load('products/1-A')
print(product.in_stock) # false
print(product.last_update) # the current date
Related tests:
update document
update document metadata
Delete documents
- Using entity
product = session.load('products/1-A')
session.delete(product)
session.save_changes()
product = session.load('products/1-A')
print(product) # None
- Using document ID
session.delete('products/1-A')
Related tests:
delete doc by entity
delete doc by ID
cannot delete after change
loading deleted doc returns null
Query documents
- Use
query() session method:
Query by collection:
import datetime
from ravendb import DocumentStore, QueryOperator
store = DocumentStore()
store.initialize()
session = store.open_session()
session.query_collection(str()).not_()
Query by index name:
query = session.query_index("productsByCategory")
Query by index:
query = session.query_index_type(Product_ByName, Product) # the second argument (object_type) is optional, as always
Query by entity type:
query = session.query(object_type=User) # object_type is an optional argument, but we can call this method only with it
- Build up the query - apply search conditions, set ordering, etc.
Query supports chaining calls:
query = session.query_collection("Users")
.wait_for_non_stale_results()
.using_default_operator(QueryOperator.AND)
.where_equals("manufacturer", "Apple")
.where_equals("in_stock", True)
.where_between("last_update", datetime.datetime(2022,11,1), datetime.datetime.now()).order_by("price")
- Execute the query to get results:
results = list(query) # get all results
# ...
first_result = query.first() # gets first result
# ...
single = query.single() # gets single result
Query methods overview
select_fields() - projections using a single field
# RQL
# from users select name
# Query
class UserProj:
def __init__(self, name: str = None, age: int = None):
self.name = name
self.age = age
user_names = [user_proj.name for user_proj in session.query_collection("Users").select_fields(UserProj, "name")]
# Sample results
# John, Stefanie, Thomas
select_fields() - projections using multiple fields
# RQL
# from users select name, age
# Query
results = list(session.query_collection("Users").select_fields(UserProj, "name", "age"))
# Sample results
# [ { name: 'John', age: 30 },
# { name: 'Stefanie', age: 25 },
# { name: 'Thomas', age: 25 } ]
Related tests:
query with projections (query only two fields)
can_project_id_field
distinct()
# RQL
# from users select distinct age
# Query
[user_proj.age for user_proj in session.query_collection("Users").select_fields(UserProj, "age").distinct()]
# Sample results
# [ 30, 25 ]