pygsti.io.mongodb.add_auxtree_write_ops_and_update_doc#
- add_auxtree_write_ops_and_update_doc(doc, write_ops, mongodb, collection_name, valuedict, auxfile_types=None, init_meta=None, overwrite_existing=False)#
Similar to write_auxtree_to_mongodb, but just collect write operations and update a main-doc dictionary.
This function effectively performs all the heavy-lifting to write a dictionary to multiple documents within a MongoDB database without actually executing any write operations. Instead, a dictionary representing the main document (which we typically assume will be written later) is updated and additional write operations (for auxiliary documents) are added to a
WriteOpsByCollectionobject. This function is intended for use within aMongoSerializable-derived object’s _add_auxiliary_write_ops_and_update_doc method.- Parameters:
doc (dict) – The root-document data, which is updated as needed and is expected to be initialized at least with an _id key-value pair.
write_ops (WriteOpsByCollection) – An object that keeps track of pymongo write operations on a per-collection basis. This object accumulates write operations to be performed at some point in the future.
mongodb (pymongo.database.Database) – The MongoDB instance that is planned to be written to. Used to test for existing records and not to write to, as writing is assumed to be done later, potentially as a bulk write operation.
collection_name (str) – the MongoDB collection within mongodb that is planned to write to.
valuedict (dict) – The dictionary of values to serialize to disk.
auxfile_types (dict, optional) – A dictionary whose keys are a subset of the keys of valuedict, and whose values are known “aux-file” types. auxfile_types[key] says that valuedict[key] should be serialized into a separate document with the given format rather than be included directly in the root document. If None, this dictionary is assumed to be valuedict[‘auxfile_types’].
init_meta (dict, optional) – A dictionary of “initial” meta-data to be included in the root document (but that isn’t in valuedict). For example, the class name of an object is often stored as in the “type” field of meta.json when the_model objects .__dict__ is used as valuedict.
overwrite_existing (bool, optional) – Whether existing documents should be overwritten. The default of False causes a ValueError to be raised if a document with the given doc_id already exists. Setting this to True mimics the behaviour of a typical filesystem, where writing to a path can be done regardless of whether it already exists.
- Returns:
The identifier of the root document that was written.
- Return type:
bson.objectid.ObjectId