After successfully storing documents in our database, the next step is to actually use that data. To do this, we commonly use a get
or retrieve
method. This method typically accepts an ID corresponding to the document we are interested in, and returns that document.
Retrieving a document from our document-oriented database is as simple as accessing the value of a key in a Python dictionary. We can implement a function get_document(db, id)
, where db
is our database and id
is the identifier for our document.
With these methods in place, it's easy to see how we could build out the operations of a more sophisticated database system, even topping it off with an interface for a language such as SQL.
In complex systems like distributed databases or big data applications, retrieving documents might not be as straightforward due to concerns like data consistency, network latencies or fault tolerance. However, those are more advanced topics that go beyond the scope of this simple tutorial. For now, we're focusing on the fundamental concept of retrieving documents from a document-oriented database.
Below is the Python code implementing retrieval of document from our database:
xxxxxxxxxx
if __name__ == "__main__":
# Python logic here
db = {'1': {'name': 'Software Engineer', 'skills': ['python', 'java', 'c++', 'data analysis']}}
def get_document(db, id):
if id in db:
return db[id]
else:
raise KeyError('Document with given ID is not found in the database.')
try:
doc = get_document(db, '1')
print(doc)
except KeyError as e:
print(e)