MongoDB
hololinked.storage.MongoDB
Bases: BaseDB
Provides persistence for Thing properties using MongoDB.
Properties are stored in a 'properties' collection, with fields:
- id: Thing instance identifier
- name: property name
- serialized_value: serialized property value
Source code in repo/hololinked/hololinked/storage/mongodb.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 | |
Attributes
config
instance-attribute
config = load_conf(config_file=config_file, default_file_path=join(TEMP_DIR_DB, get_sanitized_filename_from_random_string(f'{__name__}.{id}', extension='db')))
Functions
__init__
Initialize MongoDB for a Thing instance.
Connects to MongoDB and sets up collections.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Thing
|
The |
required |
|
str
|
Path to the MongoDB configuration file. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the loaded configuration is not a valid Mongo DB Config (but a valid config of another type). |
Source code in repo/hololinked/hololinked/storage/mongodb.py
from_thing
classmethod
Build the database engine for a Thing, taking its configuration from the db_config_file argument.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Thing
|
the |
required |
|
Any
|
the keyword arguments given to the |
{}
|
Returns:
| Type | Description |
|---|---|
Self
|
the database engine, connected and ready to use |
Source code in repo/hololinked/hololinked/storage/bases.py
fetch_own_info
Fetch Thing instance's own information (some useful metadata which helps the Thing run).
Highly unused and irrelevant currently.
Returns:
| Type | Description |
|---|---|
dict[str, Any] | None
|
Metadata document for the Thing instance, or None if not found. |
Source code in repo/hololinked/hololinked/storage/mongodb.py
get_property
Fetch a single property value from the database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str | Property
|
string name or descriptor object |
required |
|
bool
|
deserialize the property if True |
True
|
Returns:
| Type | Description |
|---|---|
Any
|
property value |
Raises:
| Type | Description |
|---|---|
PyMongoError
|
if the property is not found in database |
Source code in repo/hololinked/hololinked/storage/mongodb.py
get_properties
Get multiple properties at once from the database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
dict[str | Property, Any]
|
string names or the descriptor of the properties as a list |
required |
|
bool
|
deserialize the properties if True |
True
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
property names and values as items |
Source code in repo/hololinked/hololinked/storage/mongodb.py
get_all_properties
Get all properties of the Thing instance stored in the database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
bool
|
deserialize the properties if True |
True
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
property names and values as items |
Source code in repo/hololinked/hololinked/storage/mongodb.py
set_property
Change the value of an already existing property in the database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str | Property
|
string name or descriptor object |
required |
|
Any
|
value of the property |
required |
Source code in repo/hololinked/hololinked/storage/mongodb.py
set_properties
Change the values of already existing properties at once in the database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
dict[str | Property, Any]
|
string names or the descriptor of the property and any value as dictionary pairs |
required |
Source code in repo/hololinked/hololinked/storage/mongodb.py
create_missing_properties
create_missing_properties(properties: dict[str, Property], get_missing_property_names: bool = False) -> Any
Create any and all missing properties of Thing instance in database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
dict[str, Property]
|
descriptors of the properties |
required |
|
bool
|
whether to return the list of missing property names |
False
|
Returns:
| Type | Description |
|---|---|
List[str]
|
list of missing properties if get_missing_property_names is True |