As you're an experienced engineer, you know that once we've defined our database and the type of documents it'll store, the next step is storing these documents.
In a Document-Oriented Database, storing a document involves adding it to the collection of documents. For this, we'll usually be implementing a method named add or insert. This method will receive a document (a dictionary, in our Python case) and add it to the database, usually generating an ID to reference it later.
On the other hand, since our documents are dictionary-like objects, they can contain nested dictionaries, thus introducing hierarchical data. This is an advantage over traditional SQL databases where storing such hierarchical data can be a little bit complex.
Let's consider an example where we store our earlier created profile of a software engineer to our Document-Oriented Database:
In the Python code snippet below we define our 'database' as a dictionary. We then present a function add_document(db, document) which adds a document to the database and returns a generated ID for the inserted document.
xxxxxxxxxximport uuidif __name__ == '__main__': # Python logic here profile = { 'name': 'John Doe', 'age': 30, 'profession': 'Software Engineer', 'languages': ['Python', 'JavaScript', 'C++'], 'interests': ['AI', 'Finance'] } database = {} def add_document(db, document): doc_id = str(uuid.uuid4()) db[doc_id] = document return doc_id doc_id = add_document(database, profile) print(f'Document inserted with ID: {doc_id}') print('Database content:') for key, value in database.items(): print(f'ID: {key}, Document: {value}')


