atomicshop 2.16.41__py3-none-any.whl → 2.16.43__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of atomicshop might be problematic. Click here for more details.
- atomicshop/__init__.py +1 -1
- atomicshop/basics/bytes_arrays.py +24 -0
- atomicshop/dns.py +11 -0
- atomicshop/http_parse.py +4 -4
- atomicshop/mitm/connection_thread_worker.py +2 -7
- atomicshop/mitm/mitm_main.py +9 -4
- atomicshop/web.py +5 -2
- atomicshop/websocket_parse.py +176 -0
- atomicshop/wrappers/factw/install/install_after_restart.py +13 -2
- atomicshop/wrappers/mongodbw/mongodbw.py +312 -110
- atomicshop/wrappers/playwrightw/javascript.py +7 -3
- atomicshop/wrappers/playwrightw/scenarios.py +14 -5
- atomicshop/wrappers/pycharmw/ubuntu.py +3 -2
- atomicshop/wrappers/winregw/winreg_network.py +45 -16
- {atomicshop-2.16.41.dist-info → atomicshop-2.16.43.dist-info}/METADATA +2 -1
- {atomicshop-2.16.41.dist-info → atomicshop-2.16.43.dist-info}/RECORD +19 -18
- {atomicshop-2.16.41.dist-info → atomicshop-2.16.43.dist-info}/LICENSE.txt +0 -0
- {atomicshop-2.16.41.dist-info → atomicshop-2.16.43.dist-info}/WHEEL +0 -0
- {atomicshop-2.16.41.dist-info → atomicshop-2.16.43.dist-info}/top_level.txt +0 -0
|
@@ -92,7 +92,7 @@ class MongoDBWrapper:
|
|
|
92
92
|
|
|
93
93
|
def delete(
|
|
94
94
|
self,
|
|
95
|
-
|
|
95
|
+
filter_instance: Union[list[dict], dict],
|
|
96
96
|
collection_name: str
|
|
97
97
|
):
|
|
98
98
|
"""
|
|
@@ -100,7 +100,7 @@ class MongoDBWrapper:
|
|
|
100
100
|
For pure mongo, this is the list of queries to remove.
|
|
101
101
|
Each query for a single item.
|
|
102
102
|
|
|
103
|
-
:param
|
|
103
|
+
:param filter_instance: dict or list of dictionaries (the list of filters to remove from the collection).
|
|
104
104
|
:param collection_name: str, the name of the collection.
|
|
105
105
|
|
|
106
106
|
:return: None
|
|
@@ -109,21 +109,21 @@ class MongoDBWrapper:
|
|
|
109
109
|
self.connect()
|
|
110
110
|
|
|
111
111
|
delete(
|
|
112
|
-
|
|
112
|
+
filter_instance=filter_instance,
|
|
113
113
|
database=self.db, collection_name=collection_name,
|
|
114
114
|
mongo_client=self.client, close_client=False)
|
|
115
115
|
|
|
116
116
|
def delete_many(
|
|
117
117
|
self,
|
|
118
|
-
|
|
118
|
+
filter_query: dict,
|
|
119
119
|
collection_name: str
|
|
120
120
|
):
|
|
121
121
|
"""
|
|
122
|
-
Remove all entries that match the query from a MongoDB collection.
|
|
122
|
+
Remove all entries that match the filter query from a MongoDB collection.
|
|
123
123
|
|
|
124
|
-
:param
|
|
124
|
+
:param filter_query: dict, the filter query to search for.
|
|
125
125
|
Example, search for all entries with column name 'name' equal to 'John':
|
|
126
|
-
query = {'name': 'John'}
|
|
126
|
+
filter query = {'name': 'John'}
|
|
127
127
|
:param collection_name: str, the name of the collection.
|
|
128
128
|
|
|
129
129
|
:return: result of the operation.
|
|
@@ -132,17 +132,52 @@ class MongoDBWrapper:
|
|
|
132
132
|
self.connect()
|
|
133
133
|
|
|
134
134
|
return delete_many(
|
|
135
|
-
|
|
135
|
+
filter_query=filter_query,
|
|
136
136
|
database=self.db, collection_name=collection_name,
|
|
137
137
|
mongo_client=self.client, close_client=False)
|
|
138
138
|
|
|
139
|
+
def create_index(
|
|
140
|
+
self,
|
|
141
|
+
collection_name: str,
|
|
142
|
+
fields_list: list[tuple[str, int]],
|
|
143
|
+
name: str = None
|
|
144
|
+
):
|
|
145
|
+
"""
|
|
146
|
+
Create an index in a MongoDB collection.
|
|
147
|
+
:param collection_name: str, the name of the collection.
|
|
148
|
+
:param fields_list: list of tuples, each tuple will contain
|
|
149
|
+
[0] string of the field name and
|
|
150
|
+
[1] the integer value of the order
|
|
151
|
+
to sort by, this is pymongo default, 1 for ascending and -1 for descending.
|
|
152
|
+
Example:
|
|
153
|
+
[
|
|
154
|
+
('vendor', 1),
|
|
155
|
+
('model', -1)
|
|
156
|
+
]
|
|
157
|
+
|
|
158
|
+
Explanation:
|
|
159
|
+
This will create a compound index that will sort the collection by the field 'vendor'
|
|
160
|
+
in ascending order, and then by the field 'model' in descending order.
|
|
161
|
+
:param name: str, the name of the index.
|
|
162
|
+
|
|
163
|
+
:return: None
|
|
164
|
+
"""
|
|
165
|
+
|
|
166
|
+
self.connect()
|
|
167
|
+
|
|
168
|
+
create_index(
|
|
169
|
+
database=self.db, collection_name=collection_name,
|
|
170
|
+
fields_list=fields_list, name=name,
|
|
171
|
+
mongo_client=self.client, close_client=False)
|
|
172
|
+
|
|
139
173
|
def find(
|
|
140
174
|
self,
|
|
141
175
|
collection_name: str,
|
|
142
|
-
|
|
176
|
+
filter_query: dict = None,
|
|
177
|
+
projection: dict = None,
|
|
143
178
|
page: int = None,
|
|
144
179
|
items: int = None,
|
|
145
|
-
|
|
180
|
+
sort: dict[str, Literal[
|
|
146
181
|
'asc', 'desc',
|
|
147
182
|
1, -1]] = None,
|
|
148
183
|
convert_object_id_to_str: bool = False,
|
|
@@ -151,18 +186,19 @@ class MongoDBWrapper:
|
|
|
151
186
|
"""
|
|
152
187
|
Find entries in a MongoDB collection by query.
|
|
153
188
|
:param collection_name: str, the name of the collection.
|
|
154
|
-
:param
|
|
189
|
+
:param filter_query: dict, the query to search for.
|
|
155
190
|
Example, search for all entries with column name 'name' equal to 'John':
|
|
156
|
-
|
|
191
|
+
filter_query = {'name': 'John'}
|
|
157
192
|
Example, return all entries from collection:
|
|
158
|
-
|
|
193
|
+
filter_query = None
|
|
159
194
|
|
|
160
195
|
CHECK MORE EXAMPLES IN THE DOCSTRING OF THE FUNCTION 'find' BELOW which is not in this class.
|
|
196
|
+
:param projection: dict, the only fields to return or exclude.
|
|
161
197
|
:param page: int, the page number (Optional).
|
|
162
198
|
The results are filtered after results are fetched from db.
|
|
163
199
|
:param items: int, the number of results per page (Optional).
|
|
164
200
|
The results are filtered after results are fetched from db.
|
|
165
|
-
:param
|
|
201
|
+
:param sort: dict, the name of the field and the order to sort the containers by.
|
|
166
202
|
You can use several fields to sort the containers by several fields.
|
|
167
203
|
In this case the containers will be sorted by the first field, then by the second field, etc.
|
|
168
204
|
You can also use only singular field to sort the containers by only one field.
|
|
@@ -195,7 +231,8 @@ class MongoDBWrapper:
|
|
|
195
231
|
|
|
196
232
|
entries: list[dict] = find(
|
|
197
233
|
database=self.db, collection_name=collection_name,
|
|
198
|
-
|
|
234
|
+
filter_query=filter_query, projection=projection,
|
|
235
|
+
page=page, items=items, sort=sort,
|
|
199
236
|
convert_object_id_to_str=convert_object_id_to_str, key_convert_to_dict=keys_convert_to_dict,
|
|
200
237
|
mongo_client=self.client, close_client=False)
|
|
201
238
|
|
|
@@ -205,7 +242,7 @@ class MongoDBWrapper:
|
|
|
205
242
|
self,
|
|
206
243
|
collection_name: str,
|
|
207
244
|
field_name: str,
|
|
208
|
-
|
|
245
|
+
filter_query: dict = None
|
|
209
246
|
) -> list:
|
|
210
247
|
"""
|
|
211
248
|
Get distinct values of a field from a MongoDB collection.
|
|
@@ -227,7 +264,7 @@ class MongoDBWrapper:
|
|
|
227
264
|
|
|
228
265
|
:param collection_name: str, the name of the collection.
|
|
229
266
|
:param field_name: str, the name of the field.
|
|
230
|
-
:param
|
|
267
|
+
:param filter_query: dict, the filter query to search for. If None, the filter query will not be executed.
|
|
231
268
|
|
|
232
269
|
:return: list, the list of distinct values.
|
|
233
270
|
"""
|
|
@@ -236,26 +273,26 @@ class MongoDBWrapper:
|
|
|
236
273
|
|
|
237
274
|
distinct_values = distinct(
|
|
238
275
|
database=self.db, collection_name=collection_name,
|
|
239
|
-
field_name=field_name,
|
|
276
|
+
field_name=field_name, filter_query=filter_query, mongo_client=self.client, close_client=False)
|
|
240
277
|
|
|
241
278
|
return distinct_values
|
|
242
279
|
|
|
243
280
|
def update(
|
|
244
281
|
self,
|
|
245
282
|
collection_name: str,
|
|
246
|
-
|
|
283
|
+
filter_query: dict,
|
|
247
284
|
update_instance: Union[dict, list[dict]],
|
|
248
285
|
add_timestamp: bool = False,
|
|
249
286
|
convert_mixed_lists_to_strings: bool = False
|
|
250
287
|
):
|
|
251
288
|
"""
|
|
252
|
-
Update one entry in a MongoDB collection by query.
|
|
289
|
+
Update one entry in a MongoDB collection by filter query.
|
|
253
290
|
:param collection_name: str, the name of the collection.
|
|
254
|
-
:param
|
|
291
|
+
:param filter_query: dict, the filter query to search for.
|
|
255
292
|
Example, search for all entries with column name 'name' equal to 'John':
|
|
256
|
-
|
|
293
|
+
filter_query = {'name': 'John'}
|
|
257
294
|
Find by Object id:
|
|
258
|
-
|
|
295
|
+
filter_query = {'_id': ObjectId('5f3e3b3b4b9f3b3b4b9f3b3b')}
|
|
259
296
|
:param update_instance: dict or list of dicts, the update to apply.
|
|
260
297
|
Get examples for operators for each dict in the docstring of the function 'update' below.
|
|
261
298
|
:param add_timestamp: bool, if True, a current time timestamp will be added to the object.
|
|
@@ -268,26 +305,26 @@ class MongoDBWrapper:
|
|
|
268
305
|
|
|
269
306
|
return update(
|
|
270
307
|
database=self.db, collection_name=collection_name,
|
|
271
|
-
|
|
308
|
+
filter_query=filter_query, update_instance=update_instance, add_timestamp=add_timestamp,
|
|
272
309
|
convert_mixed_lists_to_strings=convert_mixed_lists_to_strings,
|
|
273
310
|
mongo_client=self.client, close_client=False)
|
|
274
311
|
|
|
275
312
|
def replace(
|
|
276
313
|
self,
|
|
277
314
|
collection_name: str,
|
|
278
|
-
|
|
315
|
+
filter_query: dict,
|
|
279
316
|
replacement: dict,
|
|
280
317
|
add_timestamp: bool = False,
|
|
281
318
|
convert_mixed_lists_to_strings: bool = False
|
|
282
319
|
):
|
|
283
320
|
"""
|
|
284
|
-
Replace one entry in a MongoDB collection by query.
|
|
321
|
+
Replace one entry in a MongoDB collection by filter query.
|
|
285
322
|
:param collection_name: str, the name of the collection.
|
|
286
|
-
:param
|
|
323
|
+
:param filter_query: dict, the filter query to search for.
|
|
287
324
|
Example, search for all entries with column name 'name' equal to 'John':
|
|
288
|
-
|
|
325
|
+
filter_query = {'name': 'John'}
|
|
289
326
|
Find by Object id:
|
|
290
|
-
|
|
327
|
+
filter_query = {'_id': ObjectId('5f3e3b3b4b9f3b3b4b9f3b3b')}
|
|
291
328
|
:param replacement: dict, the replacement to apply.
|
|
292
329
|
:param add_timestamp: bool, if True, a current time timestamp will be added to the object.
|
|
293
330
|
:param convert_mixed_lists_to_strings: bool, if True, mixed lists or tuples when entries are
|
|
@@ -299,24 +336,62 @@ class MongoDBWrapper:
|
|
|
299
336
|
|
|
300
337
|
return replace(
|
|
301
338
|
database=self.db, collection_name=collection_name,
|
|
302
|
-
|
|
339
|
+
filter_query=filter_query, replacement=replacement,
|
|
303
340
|
add_timestamp=add_timestamp, convert_mixed_lists_to_strings=convert_mixed_lists_to_strings,
|
|
304
341
|
mongo_client=self.client, close_client=False)
|
|
305
342
|
|
|
343
|
+
def get_all_indexes_in_collection(
|
|
344
|
+
self,
|
|
345
|
+
collection_name: str
|
|
346
|
+
) -> dict:
|
|
347
|
+
"""
|
|
348
|
+
Get all indexes in a MongoDB collection.
|
|
349
|
+
:param collection_name: str, the name of the collection.
|
|
350
|
+
:return: list of dictionaries, the list of indexes.
|
|
351
|
+
"""
|
|
352
|
+
|
|
353
|
+
self.connect()
|
|
354
|
+
|
|
355
|
+
indexes: dict = get_all_indexes_in_collection(
|
|
356
|
+
database=self.db, collection_name=collection_name,
|
|
357
|
+
mongo_client=self.client, close_client=False)
|
|
358
|
+
|
|
359
|
+
return indexes
|
|
360
|
+
|
|
361
|
+
def is_index_name_in_collection(
|
|
362
|
+
self,
|
|
363
|
+
collection_name: str,
|
|
364
|
+
index_name: str
|
|
365
|
+
) -> bool:
|
|
366
|
+
"""
|
|
367
|
+
Check if an index name exists in a MongoDB collection.
|
|
368
|
+
:param collection_name: str, the name of the collection.
|
|
369
|
+
:param index_name: str, the name of the index.
|
|
370
|
+
:return: bool, if the index name exists in the collection.
|
|
371
|
+
"""
|
|
372
|
+
|
|
373
|
+
self.connect()
|
|
374
|
+
|
|
375
|
+
exists: bool = is_index_name_in_collection(
|
|
376
|
+
database=self.db, collection_name=collection_name,
|
|
377
|
+
index_name=index_name, mongo_client=self.client, close_client=False)
|
|
378
|
+
|
|
379
|
+
return exists
|
|
380
|
+
|
|
306
381
|
def count_entries_in_collection(
|
|
307
382
|
self,
|
|
308
383
|
collection_name: str,
|
|
309
|
-
|
|
384
|
+
filter_query: dict = None
|
|
310
385
|
) -> int:
|
|
311
386
|
"""
|
|
312
387
|
Count entries in a MongoDB collection by query.
|
|
313
388
|
|
|
314
389
|
:param collection_name: str, the name of the collection.
|
|
315
|
-
:param
|
|
390
|
+
:param filter_query: dict, the query to search for.
|
|
316
391
|
Example, search for all entries with column name 'name' equal to 'John':
|
|
317
|
-
|
|
392
|
+
filter_query = {'name': 'John'}
|
|
318
393
|
Example, return all entries from collection:
|
|
319
|
-
|
|
394
|
+
filter_query = None
|
|
320
395
|
|
|
321
396
|
:return: int, the number of entries that match the query.
|
|
322
397
|
"""
|
|
@@ -325,7 +400,7 @@ class MongoDBWrapper:
|
|
|
325
400
|
|
|
326
401
|
count = count_entries_in_collection(
|
|
327
402
|
database=self.db, collection_name=collection_name,
|
|
328
|
-
|
|
403
|
+
filter_query=filter_query, mongo_client=self.client, close_client=False)
|
|
329
404
|
|
|
330
405
|
return count
|
|
331
406
|
|
|
@@ -452,7 +527,7 @@ def insert(
|
|
|
452
527
|
|
|
453
528
|
|
|
454
529
|
def delete(
|
|
455
|
-
|
|
530
|
+
filter_instance: Union[list[dict], dict],
|
|
456
531
|
database: Union[str, pymongo.database.Database],
|
|
457
532
|
collection_name: str,
|
|
458
533
|
mongo_client: pymongo.MongoClient = None,
|
|
@@ -461,9 +536,10 @@ def delete(
|
|
|
461
536
|
"""
|
|
462
537
|
Remove a dict or list of dictionaries or a dictionary from a MongoDB collection.
|
|
463
538
|
|
|
464
|
-
:param
|
|
465
|
-
|
|
466
|
-
|
|
539
|
+
:param filter_instance: dict or list of dictionaries,
|
|
540
|
+
dict, the regular filter for pymongo.
|
|
541
|
+
list of dictionaries to remove from the collection, for pure mongo, this is the list of filtered to remove.
|
|
542
|
+
Each filter for a single item.
|
|
467
543
|
:param database: String or the database object.
|
|
468
544
|
str - the name of the database. In this case the database object will be created.
|
|
469
545
|
pymongo.database.Database - the database object that will be used instead of creating a new one.
|
|
@@ -475,7 +551,7 @@ def delete(
|
|
|
475
551
|
:return: None
|
|
476
552
|
"""
|
|
477
553
|
|
|
478
|
-
_is_object_list_of_dicts_or_dict(
|
|
554
|
+
_is_object_list_of_dicts_or_dict(filter_instance)
|
|
479
555
|
|
|
480
556
|
if not mongo_client:
|
|
481
557
|
mongo_client = connect()
|
|
@@ -484,10 +560,10 @@ def delete(
|
|
|
484
560
|
db = _get_pymongo_db_from_string_or_pymongo_db(database, mongo_client)
|
|
485
561
|
collection = db[collection_name]
|
|
486
562
|
|
|
487
|
-
if isinstance(
|
|
488
|
-
collection.delete_one(
|
|
489
|
-
elif isinstance(
|
|
490
|
-
for doc in
|
|
563
|
+
if isinstance(filter_instance, dict):
|
|
564
|
+
collection.delete_one(filter_instance)
|
|
565
|
+
elif isinstance(filter_instance, list):
|
|
566
|
+
for doc in filter_instance:
|
|
491
567
|
collection.delete_one(doc)
|
|
492
568
|
|
|
493
569
|
if close_client:
|
|
@@ -495,18 +571,18 @@ def delete(
|
|
|
495
571
|
|
|
496
572
|
|
|
497
573
|
def delete_many(
|
|
498
|
-
|
|
574
|
+
filter_query: dict,
|
|
499
575
|
database: Union[str, pymongo.database.Database],
|
|
500
576
|
collection_name: str,
|
|
501
577
|
mongo_client: pymongo.MongoClient = None,
|
|
502
578
|
close_client: bool = False
|
|
503
579
|
):
|
|
504
580
|
"""
|
|
505
|
-
Remove all entries that match the query from a MongoDB collection.
|
|
581
|
+
Remove all entries that match the filter query from a MongoDB collection.
|
|
506
582
|
|
|
507
|
-
:param
|
|
583
|
+
:param filter_query: dict, the filter query to search for.
|
|
508
584
|
Example, search for all entries with column name 'name' equal to 'John':
|
|
509
|
-
|
|
585
|
+
filter_query = {'name': 'John'}
|
|
510
586
|
:param database: String or the database object.
|
|
511
587
|
str - the name of the database. In this case the database object will be created.
|
|
512
588
|
pymongo.database.Database - the database object that will be used instead of creating a new one.
|
|
@@ -525,7 +601,7 @@ def delete_many(
|
|
|
525
601
|
db = _get_pymongo_db_from_string_or_pymongo_db(database, mongo_client)
|
|
526
602
|
collection = db[collection_name]
|
|
527
603
|
|
|
528
|
-
result = collection.delete_many(
|
|
604
|
+
result = collection.delete_many(filter_query)
|
|
529
605
|
|
|
530
606
|
if close_client:
|
|
531
607
|
mongo_client.close()
|
|
@@ -533,13 +609,62 @@ def delete_many(
|
|
|
533
609
|
return result
|
|
534
610
|
|
|
535
611
|
|
|
612
|
+
def create_index(
|
|
613
|
+
database: Union[str, pymongo.database.Database],
|
|
614
|
+
collection_name: str,
|
|
615
|
+
fields_list: list[tuple[str, int]],
|
|
616
|
+
name: str = None,
|
|
617
|
+
mongo_client: pymongo.MongoClient = None,
|
|
618
|
+
close_client: bool = False
|
|
619
|
+
):
|
|
620
|
+
"""
|
|
621
|
+
Create an index in a MongoDB collection.
|
|
622
|
+
:param database: String or the database object.
|
|
623
|
+
str - the name of the database. In this case the database object will be created.
|
|
624
|
+
pymongo.database.Database - the database object that will be used instead of creating a new one.
|
|
625
|
+
:param collection_name: str, the name of the collection.
|
|
626
|
+
:param fields_list: list of tuples, each tuple will contain
|
|
627
|
+
[0] string of the field name and
|
|
628
|
+
[1] the integer value of the order
|
|
629
|
+
to sort by, this is pymongo default, 1 for ascending and -1 for descending.
|
|
630
|
+
Example:
|
|
631
|
+
[
|
|
632
|
+
('vendor', 1),
|
|
633
|
+
('model', -1)
|
|
634
|
+
]
|
|
635
|
+
|
|
636
|
+
Explanation:
|
|
637
|
+
This will create a compound index that will sort the collection by the field 'vendor' in ascending order,
|
|
638
|
+
and then by the field 'model' in descending order.
|
|
639
|
+
:param name: str, the name of the index.
|
|
640
|
+
:param mongo_client: pymongo.MongoClient, the connection object.
|
|
641
|
+
If None, a new connection will be created to default URI.
|
|
642
|
+
:param close_client: bool, if True, the connection will be closed after the operation.
|
|
643
|
+
|
|
644
|
+
:return: None
|
|
645
|
+
"""
|
|
646
|
+
|
|
647
|
+
if not mongo_client:
|
|
648
|
+
mongo_client = connect()
|
|
649
|
+
close_client = True
|
|
650
|
+
|
|
651
|
+
db = _get_pymongo_db_from_string_or_pymongo_db(database, mongo_client)
|
|
652
|
+
collection = db[collection_name]
|
|
653
|
+
|
|
654
|
+
collection.create_index(fields_list, name=name)
|
|
655
|
+
|
|
656
|
+
if close_client:
|
|
657
|
+
mongo_client.close()
|
|
658
|
+
|
|
659
|
+
|
|
536
660
|
def find(
|
|
537
661
|
database: Union[str, pymongo.database.Database],
|
|
538
662
|
collection_name: str,
|
|
539
|
-
|
|
663
|
+
filter_query: dict = None,
|
|
664
|
+
projection: dict = None,
|
|
540
665
|
page: int = None,
|
|
541
666
|
items: int = None,
|
|
542
|
-
|
|
667
|
+
sort: Union[
|
|
543
668
|
dict[str, Literal[
|
|
544
669
|
'asc', 'desc',
|
|
545
670
|
'ASC', 'DESC',
|
|
@@ -558,55 +683,62 @@ def find(
|
|
|
558
683
|
str - the name of the database. In this case the database object will be created.
|
|
559
684
|
pymongo.database.Database - the database object that will be used instead of creating a new one.
|
|
560
685
|
:param collection_name: str, the name of the collection.
|
|
561
|
-
:param
|
|
686
|
+
:param filter_query: dict, the query to search for.
|
|
562
687
|
Example, return all entries from collection:
|
|
563
|
-
|
|
688
|
+
filter_query = None
|
|
564
689
|
Example, search for all entries with column name 'name' equal to 'John':
|
|
565
|
-
|
|
690
|
+
filter_query = {'name': 'John'}
|
|
566
691
|
|
|
567
692
|
Additional parameters to use in the value of the query:
|
|
568
693
|
$regex: Will search for a regex pattern in the field.
|
|
569
694
|
Example for searching for a value that contains 'test':
|
|
570
|
-
|
|
695
|
+
filter_query = {'field_name': {'$regex': 'test'}}
|
|
571
696
|
This will return all entries where the field 'field_name' contains the word 'test':
|
|
572
697
|
'test', 'test1', '2test', etc.
|
|
573
698
|
|
|
574
699
|
Example for searching for a value that starts with 'test':
|
|
575
|
-
|
|
700
|
+
filter_query = {'field_name': {'$regex': '^test'}}
|
|
576
701
|
$options: The options for the regex search.
|
|
577
702
|
'i': case-insensitive search.
|
|
578
703
|
Example for case-insensitive search:
|
|
579
|
-
|
|
704
|
+
filter_query = {'field_name': {'$regex': 'test', '$options': 'i'}}
|
|
580
705
|
$and: Will search for entries that match all the conditions.
|
|
581
706
|
Example for searching for entries that match all the conditions:
|
|
582
|
-
|
|
707
|
+
filter_query = {'$and': [
|
|
583
708
|
{'field_name1': 'value1'},
|
|
584
709
|
{'field_name2': 'value2'}
|
|
585
710
|
]}
|
|
586
711
|
$or: Will search for entries that match at least one of the conditions.
|
|
587
712
|
Example for searching for entries that match at least one of the conditions:
|
|
588
|
-
|
|
713
|
+
filter_query = {'$or': [
|
|
589
714
|
{'field_name1': 'value1'},
|
|
590
715
|
{'field_name2': 'value2'}
|
|
591
716
|
]}
|
|
592
717
|
$in: Will search for a value in a list of values.
|
|
593
718
|
Example for searching for a value that is in a list of values:
|
|
594
|
-
|
|
719
|
+
filter_query = {'field_name': {'$in': ['value1', 'value2', 'value3']}}
|
|
595
720
|
$nin: Will search for a value not in a list of values.
|
|
596
721
|
Example for searching for a value that is not in a list of values:
|
|
597
|
-
|
|
722
|
+
filter_query = {'field_name': {'$nin': ['value1', 'value2', 'value3']}}
|
|
598
723
|
$exists: Will search for entries where the field exists or not.
|
|
599
724
|
Example for searching for entries where the field exists:
|
|
600
|
-
|
|
725
|
+
filter_query = {'field_name': {'$exists': True}}
|
|
601
726
|
Example for searching for entries where the field does not exist:
|
|
602
|
-
|
|
727
|
+
filter_query = {'field_name': {'$exists': False}}
|
|
603
728
|
$ne: Will search for entries where the field is not equal to the value.
|
|
604
729
|
Example for searching for entries where the field is not equal to the value:
|
|
605
|
-
|
|
606
|
-
|
|
730
|
+
filter_query = {'field_name': {'$ne': 'value'}}
|
|
731
|
+
|
|
732
|
+
:param projection: dict, the only fields to return or exclude.
|
|
733
|
+
Example, return only the field 'name' and 'age':
|
|
734
|
+
projection = {'name': 1, 'age': 1}
|
|
735
|
+
Example, return all fields except the field 'age':
|
|
736
|
+
projection = {'age': 0}
|
|
737
|
+
Example, return all fields except the field 'age' and 'name':
|
|
738
|
+
projection = {'age': 0, 'name': 0}
|
|
607
739
|
:param page: int, the page number (Optional).
|
|
608
740
|
:param items: int, the number of results per page (Optional).
|
|
609
|
-
:param
|
|
741
|
+
:param sort: dict or list of tuples:
|
|
610
742
|
dict, the name of the field and the order to sort the containers by.
|
|
611
743
|
You can use several fields to sort the containers by several fields.
|
|
612
744
|
In this case the containers will be sorted by the first field, then by the second field, etc.
|
|
@@ -647,8 +779,8 @@ def find(
|
|
|
647
779
|
elif items and not page:
|
|
648
780
|
page = 1
|
|
649
781
|
|
|
650
|
-
if
|
|
651
|
-
for key_to_sort_by, order in
|
|
782
|
+
if sort and isinstance(sort, dict):
|
|
783
|
+
for key_to_sort_by, order in sort.items():
|
|
652
784
|
if order.lower() not in ['asc', 'desc', 1, -1]:
|
|
653
785
|
raise ValueError("The order must be 'asc', 'desc', 1 or -1.")
|
|
654
786
|
|
|
@@ -659,39 +791,46 @@ def find(
|
|
|
659
791
|
db = _get_pymongo_db_from_string_or_pymongo_db(database, mongo_client)
|
|
660
792
|
collection = db[collection_name]
|
|
661
793
|
|
|
662
|
-
if
|
|
663
|
-
|
|
794
|
+
if filter_query is None:
|
|
795
|
+
filter_query = {}
|
|
796
|
+
|
|
797
|
+
# 'skip_items' can be 0, if we ask for the first page, so we still need to cut the number of items.
|
|
798
|
+
# In this case checking if 'items' is not None is enough.
|
|
799
|
+
if items is None:
|
|
800
|
+
items = 0
|
|
664
801
|
|
|
665
802
|
# Calculate the number of documents to skip
|
|
666
803
|
skip_items = 0
|
|
667
804
|
if page and items:
|
|
668
805
|
skip_items = (page - 1) * items
|
|
669
806
|
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
if
|
|
673
|
-
sorting_list_of_tuples
|
|
674
|
-
if isinstance(
|
|
675
|
-
for key_to_sort_by, order in
|
|
807
|
+
# noinspection PyTypeChecker
|
|
808
|
+
sorting_list_of_tuples: list[tuple[str, int]] = None
|
|
809
|
+
if sort:
|
|
810
|
+
sorting_list_of_tuples = []
|
|
811
|
+
if isinstance(sort, dict):
|
|
812
|
+
for key_to_sort_by, order in sort.items():
|
|
676
813
|
if order.lower() == 'asc':
|
|
677
814
|
order = pymongo.ASCENDING
|
|
678
815
|
elif order.lower() == 'desc':
|
|
679
816
|
order = pymongo.DESCENDING
|
|
680
817
|
|
|
681
818
|
sorting_list_of_tuples.append((key_to_sort_by, order))
|
|
682
|
-
elif
|
|
683
|
-
sorting_list_of_tuples =
|
|
819
|
+
elif sort and isinstance(sort, list):
|
|
820
|
+
sorting_list_of_tuples = sort
|
|
684
821
|
|
|
685
|
-
collection_items = collection_items.sort(sorting_list_of_tuples)
|
|
822
|
+
# collection_items = collection_items.sort(sorting_list_of_tuples)
|
|
823
|
+
collection_items = collection.find(
|
|
824
|
+
filter_query, projection=projection, sort=sorting_list_of_tuples, skip=skip_items, limit=items)
|
|
686
825
|
|
|
687
|
-
# 'skip_items' can be 0, if we ask for the first page, so we still need to cut the number of items.
|
|
688
|
-
# In this case checking if 'items' is not None is enough.
|
|
689
|
-
if items:
|
|
690
|
-
|
|
826
|
+
# # 'skip_items' can be 0, if we ask for the first page, so we still need to cut the number of items.
|
|
827
|
+
# # In this case checking if 'items' is not None is enough.
|
|
828
|
+
# if items:
|
|
829
|
+
# collection_items = collection_items.skip(skip_items).limit(items)
|
|
691
830
|
|
|
692
831
|
entries: list[dict] = list(collection_items)
|
|
693
832
|
|
|
694
|
-
if convert_object_id_to_str:
|
|
833
|
+
if entries and convert_object_id_to_str and '_id' in entries[0]:
|
|
695
834
|
for entry_index, entry in enumerate(entries):
|
|
696
835
|
entries[entry_index]['_id'] = str(entry['_id'])
|
|
697
836
|
|
|
@@ -708,7 +847,7 @@ def distinct(
|
|
|
708
847
|
database: Union[str, pymongo.database.Database],
|
|
709
848
|
collection_name: str,
|
|
710
849
|
field_name: str,
|
|
711
|
-
|
|
850
|
+
filter_query: dict = None,
|
|
712
851
|
mongo_client: pymongo.MongoClient = None,
|
|
713
852
|
close_client: bool = False
|
|
714
853
|
) -> list:
|
|
@@ -735,8 +874,8 @@ def distinct(
|
|
|
735
874
|
pymongo.database.Database - the database object that will be used instead of creating a new one.
|
|
736
875
|
:param collection_name: str, the name of the collection.
|
|
737
876
|
:param field_name: str, the name of the field.
|
|
738
|
-
:param
|
|
739
|
-
If None, the query will not be executed.
|
|
877
|
+
:param filter_query: dict, the filter query to search for.
|
|
878
|
+
If None, the filter query will not be executed.
|
|
740
879
|
:param mongo_client: pymongo.MongoClient, the connection object.
|
|
741
880
|
If None, a new connection will be created to default URI.
|
|
742
881
|
:param close_client: bool, if True, the connection will be closed after the operation.
|
|
@@ -751,7 +890,7 @@ def distinct(
|
|
|
751
890
|
db = _get_pymongo_db_from_string_or_pymongo_db(database, mongo_client)
|
|
752
891
|
collection = db[collection_name]
|
|
753
892
|
|
|
754
|
-
distinct_values = collection.distinct(field_name,
|
|
893
|
+
distinct_values = collection.distinct(field_name, filter_query)
|
|
755
894
|
|
|
756
895
|
if close_client:
|
|
757
896
|
mongo_client.close()
|
|
@@ -762,7 +901,7 @@ def distinct(
|
|
|
762
901
|
def update(
|
|
763
902
|
database: Union[str, pymongo.database.Database],
|
|
764
903
|
collection_name: str,
|
|
765
|
-
|
|
904
|
+
filter_query: dict,
|
|
766
905
|
update_instance: Union[dict, list[dict]],
|
|
767
906
|
add_timestamp: bool = False,
|
|
768
907
|
convert_mixed_lists_to_strings: bool = False,
|
|
@@ -770,16 +909,16 @@ def update(
|
|
|
770
909
|
close_client: bool = False
|
|
771
910
|
):
|
|
772
911
|
"""
|
|
773
|
-
Update one entry in a MongoDB collection by query.
|
|
912
|
+
Update one entry in a MongoDB collection by filter query.
|
|
774
913
|
:param database: String or the database object.
|
|
775
914
|
str - the name of the database. In this case the database object will be created.
|
|
776
915
|
pymongo.database.Database - the database object that will be used instead of creating a new one.
|
|
777
916
|
:param collection_name: str, the name of the collection.
|
|
778
|
-
:param
|
|
917
|
+
:param filter_query: dict, the filter query to search for.
|
|
779
918
|
Example, search for all entries with column name 'name' equal to 'John':
|
|
780
|
-
|
|
919
|
+
filter_query = {'name': 'John'}
|
|
781
920
|
Find by Object id:
|
|
782
|
-
|
|
921
|
+
filter_query = {'_id': ObjectId('5f3e3b3b4b9f3b3b4b9f3b3b')}
|
|
783
922
|
:param update_instance: dict or list of dicts, the update to apply.
|
|
784
923
|
If dict, the update will be applied to one entry using 'update_one'.
|
|
785
924
|
If list of dicts, the update will be applied to multiple entries using 'update_many'.
|
|
@@ -814,7 +953,7 @@ def update(
|
|
|
814
953
|
|
|
815
954
|
if convert_mixed_lists_to_strings:
|
|
816
955
|
if isinstance(update_instance, dict):
|
|
817
|
-
|
|
956
|
+
update_instance = dicts.convert_int_to_str_in_mixed_lists(update_instance)
|
|
818
957
|
elif isinstance(update_instance, list):
|
|
819
958
|
for doc_index, doc in enumerate(update_instance):
|
|
820
959
|
update_instance[doc_index] = dicts.convert_int_to_str_in_mixed_lists(doc)
|
|
@@ -829,9 +968,9 @@ def update(
|
|
|
829
968
|
|
|
830
969
|
result = None
|
|
831
970
|
if isinstance(update_instance, dict):
|
|
832
|
-
result = collection.update_one(
|
|
971
|
+
result = collection.update_one(filter_query, update_instance)
|
|
833
972
|
elif isinstance(update_instance, list):
|
|
834
|
-
result = collection.update_many(
|
|
973
|
+
result = collection.update_many(filter_query, update_instance)
|
|
835
974
|
|
|
836
975
|
if result.matched_count == 0:
|
|
837
976
|
raise MongoDBUpdateOneError("No document found to update.")
|
|
@@ -845,7 +984,7 @@ def update(
|
|
|
845
984
|
def replace(
|
|
846
985
|
database: Union[str, pymongo.database.Database],
|
|
847
986
|
collection_name: str,
|
|
848
|
-
|
|
987
|
+
filter_query: dict,
|
|
849
988
|
replacement: dict,
|
|
850
989
|
add_timestamp: bool = False,
|
|
851
990
|
convert_mixed_lists_to_strings: bool = False,
|
|
@@ -853,16 +992,16 @@ def replace(
|
|
|
853
992
|
close_client: bool = False
|
|
854
993
|
):
|
|
855
994
|
"""
|
|
856
|
-
Replace one entry in a MongoDB collection by query.
|
|
995
|
+
Replace one entry in a MongoDB collection by filter query.
|
|
857
996
|
:param database: String or the database object.
|
|
858
997
|
str - the name of the database. In this case the database object will be created.
|
|
859
998
|
pymongo.database.Database - the database object that will be used instead of creating a new one.
|
|
860
999
|
:param collection_name: str, the name of the collection.
|
|
861
|
-
:param
|
|
1000
|
+
:param filter_query: dict, the filter query to search for.
|
|
862
1001
|
Example, search for all entries with column name 'name' equal to 'John':
|
|
863
|
-
|
|
1002
|
+
filter_query = {'name': 'John'}
|
|
864
1003
|
Find by Object id:
|
|
865
|
-
|
|
1004
|
+
filter_query = {'_id': ObjectId('5f3e3b3b4b9f3b3b4b9f3b3b')}
|
|
866
1005
|
:param replacement: dict, the replacement to apply.
|
|
867
1006
|
:param add_timestamp: bool, if True, a current time timestamp will be added to the object.
|
|
868
1007
|
:param convert_mixed_lists_to_strings: bool, if True, mixed lists or tuples when entries are strings and integers,
|
|
@@ -888,7 +1027,7 @@ def replace(
|
|
|
888
1027
|
timestamp = datetime.datetime.now()
|
|
889
1028
|
replacement['timestamp'] = timestamp
|
|
890
1029
|
|
|
891
|
-
result = collection.replace_one(
|
|
1030
|
+
result = collection.replace_one(filter_query, replacement)
|
|
892
1031
|
if result.matched_count == 0:
|
|
893
1032
|
raise MongoDBReplaceOneError("No document found to replace.")
|
|
894
1033
|
|
|
@@ -898,10 +1037,73 @@ def replace(
|
|
|
898
1037
|
return result
|
|
899
1038
|
|
|
900
1039
|
|
|
1040
|
+
def get_all_indexes_in_collection(
|
|
1041
|
+
database: Union[str, pymongo.database.Database],
|
|
1042
|
+
collection_name: str,
|
|
1043
|
+
mongo_client: pymongo.MongoClient = None,
|
|
1044
|
+
close_client: bool = False
|
|
1045
|
+
) -> dict:
|
|
1046
|
+
"""
|
|
1047
|
+
Get all indexes in a MongoDB collection.
|
|
1048
|
+
:param database: String or the database object.
|
|
1049
|
+
str - the name of the database. In this case the database object will be created.
|
|
1050
|
+
pymongo.database.Database - the database object that will be used instead of creating a new one.
|
|
1051
|
+
:param collection_name: str, the name of the collection.
|
|
1052
|
+
:param mongo_client: pymongo.MongoClient, the connection object.
|
|
1053
|
+
If None, a new connection will be created to default URI.
|
|
1054
|
+
:param close_client: bool, if True, the connection will be closed after the operation.
|
|
1055
|
+
|
|
1056
|
+
:return: list, the list of indexes.
|
|
1057
|
+
"""
|
|
1058
|
+
|
|
1059
|
+
if not mongo_client:
|
|
1060
|
+
mongo_client = connect()
|
|
1061
|
+
close_client = True
|
|
1062
|
+
|
|
1063
|
+
db = _get_pymongo_db_from_string_or_pymongo_db(database, mongo_client)
|
|
1064
|
+
collection = db[collection_name]
|
|
1065
|
+
|
|
1066
|
+
# noinspection PyTypeChecker
|
|
1067
|
+
indexes: dict = collection.index_information()
|
|
1068
|
+
|
|
1069
|
+
if close_client:
|
|
1070
|
+
mongo_client.close()
|
|
1071
|
+
|
|
1072
|
+
return indexes
|
|
1073
|
+
|
|
1074
|
+
|
|
1075
|
+
def is_index_name_in_collection(
|
|
1076
|
+
database: Union[str, pymongo.database.Database],
|
|
1077
|
+
collection_name: str,
|
|
1078
|
+
index_name: str,
|
|
1079
|
+
mongo_client: pymongo.MongoClient = None,
|
|
1080
|
+
close_client: bool = False
|
|
1081
|
+
) -> bool:
|
|
1082
|
+
"""
|
|
1083
|
+
Check if an index name is in a MongoDB collection.
|
|
1084
|
+
:param database: String or the database object.
|
|
1085
|
+
str - the name of the database. In this case the database object will be created.
|
|
1086
|
+
pymongo.database.Database - the database object that will be used instead of creating a new one.
|
|
1087
|
+
:param collection_name: str, the name of the collection.
|
|
1088
|
+
:param index_name: str, the name of the index.
|
|
1089
|
+
:param mongo_client: pymongo.MongoClient, the connection object.
|
|
1090
|
+
If None, a new connection will be created to default URI.
|
|
1091
|
+
:param close_client: bool, if True, the connection will be closed after the operation.
|
|
1092
|
+
|
|
1093
|
+
:return: bool, if the index name is in the collection.
|
|
1094
|
+
"""
|
|
1095
|
+
|
|
1096
|
+
indexes = get_all_indexes_in_collection(
|
|
1097
|
+
database=database, collection_name=collection_name,
|
|
1098
|
+
mongo_client=mongo_client, close_client=close_client)
|
|
1099
|
+
|
|
1100
|
+
return index_name in indexes
|
|
1101
|
+
|
|
1102
|
+
|
|
901
1103
|
def count_entries_in_collection(
|
|
902
1104
|
database: Union[str, pymongo.database.Database],
|
|
903
1105
|
collection_name: str,
|
|
904
|
-
|
|
1106
|
+
filter_query: dict = None,
|
|
905
1107
|
mongo_client: pymongo.MongoClient = None,
|
|
906
1108
|
close_client: bool = False
|
|
907
1109
|
) -> int:
|
|
@@ -912,11 +1114,11 @@ def count_entries_in_collection(
|
|
|
912
1114
|
str - the name of the database. In this case the database object will be created.
|
|
913
1115
|
pymongo.database.Database - the database object that will be used instead of creating a new one.
|
|
914
1116
|
:param collection_name: str, the name of the collection.
|
|
915
|
-
:param
|
|
1117
|
+
:param filter_query: dict, the query to search for.
|
|
916
1118
|
Example, search for all entries with column name 'name' equal to 'John':
|
|
917
|
-
|
|
1119
|
+
filter_query = {'name': 'John'}
|
|
918
1120
|
Example, return all entries from collection:
|
|
919
|
-
|
|
1121
|
+
filter_query = None
|
|
920
1122
|
:param mongo_client: pymongo.MongoClient, the connection object.
|
|
921
1123
|
If None, a new connection will be created to default URI.
|
|
922
1124
|
:param close_client: bool, if True, the connection will be closed after the operation.
|
|
@@ -931,10 +1133,10 @@ def count_entries_in_collection(
|
|
|
931
1133
|
db = _get_pymongo_db_from_string_or_pymongo_db(database, mongo_client)
|
|
932
1134
|
collection = db[collection_name]
|
|
933
1135
|
|
|
934
|
-
if
|
|
935
|
-
|
|
1136
|
+
if filter_query is None:
|
|
1137
|
+
filter_query = {}
|
|
936
1138
|
|
|
937
|
-
count = collection.count_documents(
|
|
1139
|
+
count = collection.count_documents(filter_query)
|
|
938
1140
|
|
|
939
1141
|
if close_client:
|
|
940
1142
|
mongo_client.close()
|
|
@@ -1011,7 +1213,7 @@ def overwrite_collection(
|
|
|
1011
1213
|
mongo_client=mongo_client
|
|
1012
1214
|
)
|
|
1013
1215
|
|
|
1014
|
-
|
|
1216
|
+
insert(
|
|
1015
1217
|
object_instance=object_instance,
|
|
1016
1218
|
database=database, collection_name=collection_name,
|
|
1017
1219
|
add_timestamp=add_timestamp, convert_mixed_lists_to_strings=convert_mixed_lists_to_strings,
|