Usage#
Instantiate Collection#
Create a collection to store the documents (equivalent to a table in SQL)
from rockydb import RockyDB
db = RockyDB('database/') # specify the path to store the database
posts = db.collection("posts") # creating a collection named "posts"
Basic Usage#
Insert#
Store a document in the database
insert(doc: dict)
Parameters
doc (required) - dict
If there is no _id field in the document, an _id will be randomly generated.
The document supports the following data-types: int, str, float, bool and list.
Example
doc_id = posts.insert({
"title": "A Housing Crisis Has More Politicians Saying Yes to Big Real Estate",
"author": "Mihir Zaveri",
"year": 2022,
"source": "https://www.nytimes.com/2022/10/16/nyregion/politicians-housing-crisis-real-estate.html",
})
Returns the _id of the document
Get#
Retrieves a document from the database using its _id.
get(id: str)
Parameters
id (required) - str
Example
doc = posts.get("DBDV73BQ")
Returns the document if it exists, otherwise will return None.
Delete#
Deletes a document from the database using its _id .
delete(id: str)
Parameters
id (required) - str
Example
posts.delete("DBDV73BQ")
Returns True if document was found and deleted, otherwise will return False.
Find#
Returns documents that match a query
find(query: dict, limit: int = 1)
Parameters
- query (required) -
dict The query
keycorresponds to the documentkeyand the queryvaluecorresponds to itsvaluein the document.
limit (optional) - int, default is 1
Example
docs = posts.query({"title": "some value", "year": 2022})
The above query translates to ("title" == "some value") AND ("year" == 2022). There is currently no support
for OR type queries. Its currently being implemented.
Can also search for lt (less-than), lte (less-than equals), gt (greater-than) and gte (greater-than equals).
To do so, place a ? after the key in the query, and type lt or any of the other options.
Example
docs = posts.query({"year?lte": 2050})
Returns a list containing all the documents found. list will
be empty if no documents matched the query.
Indexes#
Create an index on a specific document field to speed up queries
Create#
Create the index
create_index(name: str, field: str)
Parameters
name (required) - str
field (required) - str
Example
posts.create_index("age_index", "age")
Returns an Index object if creation was successful. Otherwise will return None.
Find#
Query the index
find(query: dict, limti: int = 1)
Parameters
- query (required) -
dict The query
keycorresponds to the document field that has been indexed and the queryvaluecorresponds to itsvaluein the document.
limit (optional) - int, default is 1
Example
index.find({"age?lte": 30})
Can search for lt (less-than), lte (less-than equals), gt (greater-than) and gte (greater-than equals).
Returns a list of document _id s that match the query.