1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 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 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289
|
# Copyright 2011-present MongoDB, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Framework-agnostic core of Motor, an asynchronous driver for MongoDB."""
import functools
import time
import warnings
import pymongo
import pymongo.auth
import pymongo.common
import pymongo.database
import pymongo.errors
import pymongo.mongo_client
from pymongo.change_stream import ChangeStream
from pymongo.client_session import ClientSession
from pymongo.collection import Collection
from pymongo.command_cursor import CommandCursor, RawBatchCommandCursor
from pymongo.cursor import Cursor, RawBatchCursor
from pymongo.cursor_shared import _QUERY_OPTIONS
from pymongo.database import Database
from pymongo.driver_info import DriverInfo
from pymongo.encryption import ClientEncryption
from . import docstrings
from . import version as motor_version
from .metaprogramming import (
AsyncCommand,
AsyncRead,
AsyncWrite,
DelegateMethod,
MotorCursorChainingMethod,
ReadOnlyProperty,
coroutine_annotation,
create_class_with_framework,
unwrap_args_session,
unwrap_kwargs_session,
)
from .motor_common import callback_type_error
HAS_SSL = True
try:
import ssl
except ImportError:
ssl = None
HAS_SSL = False
# From the Convenient API for Transactions spec, with_transaction must
# halt retries after 120 seconds.
# This limit is non-configurable and was chosen to be twice the 60 second
# default value of MongoDB's `transactionLifetimeLimitSeconds` parameter.
_WITH_TRANSACTION_RETRY_TIME_LIMIT = 120
def _within_time_limit(start_time):
"""Are we within the with_transaction retry limit?"""
return time.monotonic() - start_time < _WITH_TRANSACTION_RETRY_TIME_LIMIT
def _max_time_expired_error(exc):
"""Return true if exc is a MaxTimeMSExpired error."""
return isinstance(exc, pymongo.errors.OperationFailure) and exc.code == 50
class AgnosticBase:
def __eq__(self, other):
if (
isinstance(other, self.__class__)
and hasattr(self, "delegate")
and hasattr(other, "delegate")
):
return self.delegate == other.delegate
return NotImplemented
def __init__(self, delegate):
self.delegate = delegate
def __repr__(self):
return f"{self.__class__.__name__}({self.delegate!r})"
class AgnosticBaseProperties(AgnosticBase):
# Allow the use of generics at runtime.
@classmethod
def __class_getitem__(cls, key: str) -> object:
return cls
codec_options = ReadOnlyProperty()
read_preference = ReadOnlyProperty()
read_concern = ReadOnlyProperty()
write_concern = ReadOnlyProperty()
class AgnosticClient(AgnosticBaseProperties):
__motor_class_name__ = "MotorClient"
__delegate_class__ = pymongo.mongo_client.MongoClient
address = ReadOnlyProperty()
arbiters = ReadOnlyProperty()
close = DelegateMethod()
__hash__ = DelegateMethod()
bulk_write = AsyncRead()
drop_database = AsyncCommand().unwrap("MotorDatabase")
options = ReadOnlyProperty()
get_database = DelegateMethod(doc=docstrings.get_database_doc).wrap(Database)
get_default_database = DelegateMethod(doc=docstrings.get_default_database_doc).wrap(Database)
HOST = ReadOnlyProperty()
is_mongos = ReadOnlyProperty()
is_primary = ReadOnlyProperty()
list_databases = AsyncRead().wrap(CommandCursor)
list_database_names = AsyncRead()
nodes = ReadOnlyProperty()
PORT = ReadOnlyProperty()
primary = ReadOnlyProperty()
read_concern = ReadOnlyProperty()
secondaries = ReadOnlyProperty()
server_info = AsyncRead()
topology_description = ReadOnlyProperty()
start_session = AsyncCommand(doc=docstrings.start_session_doc).wrap(ClientSession)
_connect = AsyncRead()
def __init__(self, *args, **kwargs):
"""Create a new connection to a single MongoDB instance at *host:port*.
Takes the same constructor arguments as
:class:`~pymongo.mongo_client.MongoClient`, as well as:
:Parameters:
- `io_loop` (optional): Special event loop
instance to use instead of default.
"""
if "io_loop" in kwargs:
io_loop = kwargs.pop("io_loop")
self._framework.check_event_loop(io_loop)
else:
io_loop = None
self._io_loop = io_loop
kwargs.setdefault("connect", False)
driver_info = DriverInfo("Motor", motor_version, self._framework.platform_info())
if kwargs.get("driver"):
provided_info = kwargs.get("driver")
if not isinstance(provided_info, DriverInfo):
raise TypeError(
f"Incorrect type for `driver` {type(provided_info)};"
" expected value of type pymongo.driver_info.DriverInfo"
)
added_version = f"|{provided_info.version}" if provided_info.version else ""
added_platform = f"|{provided_info.platform}" if provided_info.platform else ""
driver_info = DriverInfo(
f"{driver_info.name}|{provided_info.name}",
f"{driver_info.version}{added_version}",
f"{driver_info.platform}{added_platform}",
)
kwargs["driver"] = driver_info
delegate = self.__delegate_class__(*args, **kwargs)
super().__init__(delegate)
@property
def io_loop(self):
if self._io_loop is None:
self._io_loop = self._framework.get_event_loop()
return self._io_loop
def get_io_loop(self):
return self.io_loop
def watch(
self,
pipeline=None,
full_document=None,
resume_after=None,
max_await_time_ms=None,
batch_size=None,
collation=None,
start_at_operation_time=None,
session=None,
start_after=None,
comment=None,
full_document_before_change=None,
show_expanded_events=None,
):
"""Watch changes on this cluster.
Returns a :class:`~MotorChangeStream` cursor which iterates over changes
on all databases in this cluster. Introduced in MongoDB 4.0.
See the documentation for :meth:`MotorCollection.watch` for more
details and examples.
:Parameters:
- `pipeline` (optional): A list of aggregation pipeline stages to
append to an initial ``$changeStream`` stage. Not all
pipeline stages are valid after a ``$changeStream`` stage, see the
MongoDB documentation on change streams for the supported stages.
- `full_document` (optional): The fullDocument option to pass
to the ``$changeStream`` stage. Allowed values: 'updateLookup'.
When set to 'updateLookup', the change notification for partial
updates will include both a delta describing the changes to the
document, as well as a copy of the entire document that was
changed from some time after the change occurred.
- `resume_after` (optional): A resume token. If provided, the
change stream will start returning changes that occur directly
after the operation specified in the resume token. A resume token
is the _id value of a change document.
- `max_await_time_ms` (optional): The maximum time in milliseconds
for the server to wait for changes before responding to a getMore
operation.
- `batch_size` (optional): The maximum number of documents to return
per batch.
- `collation` (optional): The :class:`~pymongo.collation.Collation`
to use for the aggregation.
- `start_at_operation_time` (optional): If provided, the resulting
change stream will only return changes that occurred at or after
the specified :class:`~bson.timestamp.Timestamp`. Requires
MongoDB >= 4.0.
- `session` (optional): a
:class:`~pymongo.client_session.ClientSession`.
- `start_after` (optional): The same as `resume_after` except that
`start_after` can resume notifications after an invalidate event.
This option and `resume_after` are mutually exclusive.
- `comment` (optional): A user-provided comment to attach to this
command.
- `full_document_before_change`: Allowed values: `whenAvailable` and `required`. Change
events may now result in a `fullDocumentBeforeChange` response field.
- `show_expanded_events` (optional): Include expanded events such as DDL events like
`dropIndexes`.
:Returns:
A :class:`~MotorChangeStream`.
.. versionchanged:: 3.2
Added ``show_expanded_events`` parameter.
.. versionchanged:: 3.1
Added ``full_document_before_change`` parameter.
.. versionchanged:: 3.0
Added ``comment`` parameter.
.. versionchanged:: 2.1
Added the ``start_after`` parameter.
.. versionadded:: 2.0
.. mongodoc:: changeStreams
"""
cursor_class = create_class_with_framework(
AgnosticChangeStream, self._framework, self.__module__
)
# Latent cursor that will send initial command on first "async for".
return cursor_class(
self,
pipeline,
full_document,
resume_after,
max_await_time_ms,
batch_size,
collation,
start_at_operation_time,
session,
start_after,
comment,
full_document_before_change,
show_expanded_events,
)
def __getattr__(self, name):
if name.startswith("_"):
raise AttributeError(
f"{self.__class__.__name__} has no attribute {name!r}. To access the {name}"
f" database, use client['{name}']."
)
return self[name]
def __getitem__(self, name):
db_class = create_class_with_framework(AgnosticDatabase, self._framework, self.__module__)
return db_class(self, name)
def wrap(self, obj):
if obj.__class__ == Database:
db_class = create_class_with_framework(
AgnosticDatabase, self._framework, self.__module__
)
return db_class(self, obj.name, _delegate=obj)
elif obj.__class__ == CommandCursor:
command_cursor_class = create_class_with_framework(
AgnosticCommandCursor, self._framework, self.__module__
)
return command_cursor_class(obj, self)
elif obj.__class__ == ClientSession:
session_class = create_class_with_framework(
AgnosticClientSession, self._framework, self.__module__
)
return session_class(obj, self)
class _MotorTransactionContext:
"""Internal transaction context manager for start_transaction."""
def __init__(self, session):
self._session = session
async def __aenter__(self):
return self
async def __aexit__(self, exc_type, exc_val, exc_tb):
if self._session.in_transaction:
if exc_val is None:
await self._session.commit_transaction()
else:
await self._session.abort_transaction()
class AgnosticClientSession(AgnosticBase):
"""A session for ordering sequential operations.
Do not create an instance of :class:`MotorClientSession` directly; use
:meth:`MotorClient.start_session`:
.. code-block:: python3
collection = client.db.collection
async with await client.start_session() as s:
async with s.start_transaction():
await collection.delete_one({"x": 1}, session=s)
await collection.insert_one({"x": 2}, session=s)
.. versionadded:: 2.0
"""
__motor_class_name__ = "MotorClientSession"
__delegate_class__ = ClientSession
commit_transaction = AsyncCommand()
abort_transaction = AsyncCommand()
end_session = AsyncCommand()
cluster_time = ReadOnlyProperty()
has_ended = ReadOnlyProperty()
in_transaction = ReadOnlyProperty()
options = ReadOnlyProperty()
operation_time = ReadOnlyProperty()
session_id = ReadOnlyProperty()
advance_cluster_time = DelegateMethod()
advance_operation_time = DelegateMethod()
def __init__(self, delegate, motor_client):
AgnosticBase.__init__(self, delegate=delegate)
self._client = motor_client
def get_io_loop(self):
return self._client.get_io_loop()
async def with_transaction(
self,
coro,
read_concern=None,
write_concern=None,
read_preference=None,
max_commit_time_ms=None,
):
"""Executes an awaitable in a transaction.
This method starts a transaction on this session, awaits ``coro``
once, and then commits the transaction. For example::
async def coro(session):
orders = session.client.db.orders
inventory = session.client.db.inventory
inserted_id = await orders.insert_one(
{"sku": "abc123", "qty": 100}, session=session)
await inventory.update_one(
{"sku": "abc123", "qty": {"$gte": 100}},
{"$inc": {"qty": -100}}, session=session)
return inserted_id
async with await client.start_session() as session:
inserted_id = await session.with_transaction(coro)
To pass arbitrary arguments to the ``coro``, wrap it with a
``lambda`` like this::
async def coro(session, custom_arg, custom_kwarg=None):
# Transaction operations...
async with await client.start_session() as session:
await session.with_transaction(
lambda s: coro(s, "custom_arg", custom_kwarg=1))
In the event of an exception, ``with_transaction`` may retry the commit
or the entire transaction, therefore ``coro`` may be awaited
multiple times by a single call to ``with_transaction``. Developers
should be mindful of this possibility when writing a ``coro`` that
modifies application state or has any other side-effects.
Note that even when the ``coro`` is invoked multiple times,
``with_transaction`` ensures that the transaction will be committed
at-most-once on the server.
The ``coro`` should not attempt to start new transactions, but
should simply run operations meant to be contained within a
transaction. The ``coro`` should also not commit the transaction;
this is handled automatically by ``with_transaction``. If the
``coro`` does commit or abort the transaction without error,
however, ``with_transaction`` will return without taking further
action.
When ``coro`` raises an exception, ``with_transaction``
automatically aborts the current transaction. When ``coro`` or
:meth:`~ClientSession.commit_transaction` raises an exception that
includes the ``"TransientTransactionError"`` error label,
``with_transaction`` starts a new transaction and re-executes
the ``coro``.
When :meth:`~ClientSession.commit_transaction` raises an exception with
the ``"UnknownTransactionCommitResult"`` error label,
``with_transaction`` retries the commit until the result of the
transaction is known.
This method will cease retrying after 120 seconds has elapsed. This
timeout is not configurable and any exception raised by the
``coro`` or by :meth:`ClientSession.commit_transaction` after the
timeout is reached will be re-raised. Applications that desire a
different timeout duration should not use this method.
:Parameters:
- `coro`: The coroutine to run inside a transaction. The coroutine must
accept a single argument, this session. Note, under certain error
conditions the coroutine may be run multiple times.
- `read_concern` (optional): The
:class:`~pymongo.read_concern.ReadConcern` to use for this
transaction.
- `write_concern` (optional): The
:class:`~pymongo.write_concern.WriteConcern` to use for this
transaction.
- `read_preference` (optional): The read preference to use for this
transaction. If ``None`` (the default) the :attr:`read_preference`
of this :class:`Database` is used. See
:mod:`~pymongo.read_preferences` for options.
:Returns:
The return value of the ``coro``.
.. versionadded:: 2.1
"""
start_time = time.monotonic()
while True:
async with self.start_transaction(
read_concern, write_concern, read_preference, max_commit_time_ms
):
try:
ret = await coro(self)
except Exception as exc:
if self.in_transaction:
await self.abort_transaction()
if (
isinstance(exc, pymongo.errors.PyMongoError)
and exc.has_error_label("TransientTransactionError")
and _within_time_limit(start_time)
):
# Retry the entire transaction.
continue
raise
if not self.in_transaction:
# Assume callback intentionally ended the transaction.
return ret
while True:
try:
await self.commit_transaction()
except pymongo.errors.PyMongoError as exc:
if (
exc.has_error_label("UnknownTransactionCommitResult")
and _within_time_limit(start_time)
and not _max_time_expired_error(exc)
):
# Retry the commit.
continue
if exc.has_error_label("TransientTransactionError") and _within_time_limit(
start_time
):
# Retry the entire transaction.
break
raise
# Commit succeeded.
return ret
def start_transaction(
self, read_concern=None, write_concern=None, read_preference=None, max_commit_time_ms=None
):
"""Start a multi-statement transaction.
Takes the same arguments as
:class:`~pymongo.client_session.TransactionOptions`.
Best used in a context manager block:
.. code-block:: python3
# Use "await" for start_session, but not for start_transaction.
async with await client.start_session() as s:
async with s.start_transaction():
await collection.delete_one({"x": 1}, session=s)
await collection.insert_one({"x": 2}, session=s)
"""
self.delegate.start_transaction(
read_concern=read_concern,
write_concern=write_concern,
read_preference=read_preference,
max_commit_time_ms=max_commit_time_ms,
)
return _MotorTransactionContext(self)
@property
def client(self):
"""The :class:`~MotorClient` this session was created from."""
return self._client
async def __aenter__(self):
return self
async def __aexit__(self, exc_type, exc_val, exc_tb):
self.delegate.__exit__(exc_type, exc_val, exc_tb)
def __enter__(self):
raise AttributeError(
"Use Motor sessions like 'async with await client.start_session()', not 'with'"
)
def __exit__(self, exc_type, exc_val, exc_tb):
pass
class AgnosticDatabase(AgnosticBaseProperties):
__motor_class_name__ = "MotorDatabase"
__delegate_class__ = Database
__hash__ = DelegateMethod()
__bool__ = DelegateMethod()
command = AsyncCommand(doc=docstrings.cmd_doc)
create_collection = AsyncCommand().wrap(Collection)
dereference = AsyncRead()
drop_collection = AsyncCommand().unwrap("MotorCollection")
get_collection = DelegateMethod().wrap(Collection)
list_collection_names = AsyncRead(doc=docstrings.list_collection_names_doc)
list_collections = AsyncRead().wrap(CommandCursor)
name = ReadOnlyProperty()
validate_collection = AsyncRead().unwrap("MotorCollection")
with_options = DelegateMethod().wrap(Database)
_async_aggregate = AsyncRead(attr_name="aggregate")
def __init__(self, client, name, **kwargs):
self._client = client
_delegate = kwargs.get("_delegate")
delegate = _delegate if _delegate is not None else Database(client.delegate, name, **kwargs)
super().__init__(delegate)
def aggregate(self, pipeline, *args, **kwargs):
"""Execute an aggregation pipeline on this database.
Introduced in MongoDB 3.6.
The aggregation can be run on a secondary if the client is connected
to a replica set and its ``read_preference`` is not :attr:`PRIMARY`.
The :meth:`aggregate` method obeys the :attr:`read_preference` of this
:class:`MotorDatabase`, except when ``$out`` or ``$merge`` are used, in
which case :attr:`PRIMARY` is used.
All optional `aggregate command`_ parameters should be passed as
keyword arguments to this method. Valid options include, but are not
limited to:
- `allowDiskUse` (bool): Enables writing to temporary files. When set
to True, aggregation stages can write data to the _tmp subdirectory
of the --dbpath directory. The default is False.
- `maxTimeMS` (int): The maximum amount of time to allow the operation
to run in milliseconds.
- `batchSize` (int): The maximum number of documents to return per
batch. Ignored if the connected mongod or mongos does not support
returning aggregate results using a cursor.
- `collation` (optional): An instance of
:class:`~pymongo.collation.Collation`.
- `let` (dict): A dict of parameter names and values. Values must be
constant or closed expressions that do not reference document
fields. Parameters can then be accessed as variables in an
aggregate expression context (e.g. ``"$$var"``). This option is
only supported on MongoDB >= 5.0.
Returns a :class:`MotorCommandCursor` that can be iterated like a
cursor from :meth:`find`::
async def f():
# Lists all operations currently running on the server.
pipeline = [{"$currentOp": {}}]
async for operation in client.admin.aggregate(pipeline):
print(operation)
.. note:: This method does not support the 'explain' option. Please
use :meth:`MotorDatabase.command` instead.
.. note:: The :attr:`MotorDatabase.write_concern` of this database is
automatically applied to this operation.
.. versionadded:: 2.1
.. _aggregate command:
https://www.mongodb.com/docs/manual/reference/command/aggregate/
"""
cursor_class = create_class_with_framework(
AgnosticLatentCommandCursor, self._framework, self.__module__
)
# Latent cursor that will send initial command on first "async for".
return cursor_class(
self["$cmd.aggregate"],
self._async_aggregate,
pipeline,
*unwrap_args_session(args),
**unwrap_kwargs_session(kwargs),
)
def watch(
self,
pipeline=None,
full_document=None,
resume_after=None,
max_await_time_ms=None,
batch_size=None,
collation=None,
start_at_operation_time=None,
session=None,
start_after=None,
comment=None,
full_document_before_change=None,
show_expanded_events=None,
):
"""Watch changes on this database.
Returns a :class:`~MotorChangeStream` cursor which iterates over changes
on this database. Introduced in MongoDB 4.0.
See the documentation for :meth:`MotorCollection.watch` for more
details and examples.
:Parameters:
- `pipeline` (optional): A list of aggregation pipeline stages to
append to an initial ``$changeStream`` stage. Not all
pipeline stages are valid after a ``$changeStream`` stage, see the
MongoDB documentation on change streams for the supported stages.
- `full_document` (optional): The fullDocument option to pass
to the ``$changeStream`` stage. Allowed values: 'updateLookup'.
When set to 'updateLookup', the change notification for partial
updates will include both a delta describing the changes to the
document, as well as a copy of the entire document that was
changed from some time after the change occurred.
- `resume_after` (optional): A resume token. If provided, the
change stream will start returning changes that occur directly
after the operation specified in the resume token. A resume token
is the _id value of a change document.
- `max_await_time_ms` (optional): The maximum time in milliseconds
for the server to wait for changes before responding to a getMore
operation.
- `batch_size` (optional): The maximum number of documents to return
per batch.
- `collation` (optional): The :class:`~pymongo.collation.Collation`
to use for the aggregation.
- `start_at_operation_time` (optional): If provided, the resulting
change stream will only return changes that occurred at or after
the specified :class:`~bson.timestamp.Timestamp`. Requires
MongoDB >= 4.0.
- `session` (optional): a
:class:`~pymongo.client_session.ClientSession`.
- `start_after` (optional): The same as `resume_after` except that
`start_after` can resume notifications after an invalidate event.
This option and `resume_after` are mutually exclusive.
- `comment` (optional): A user-provided comment to attach to this
command.
- `full_document_before_change`: Allowed values: `whenAvailable` and `required`. Change
events may now result in a `fullDocumentBeforeChange` response field.
- `show_expanded_events` (optional): Include expanded events such as DDL events like
`dropIndexes`.
:Returns:
A :class:`~MotorChangeStream`.
.. versionchanged:: 3.2
Added ``show_expanded_events`` parameter.
.. versionchanged:: 3.1
Added ``full_document_before_change`` parameter.
.. versionchanged:: 3.0
Added ``comment`` parameter.
.. versionchanged:: 2.1
Added the ``start_after`` parameter.
.. versionadded:: 2.0
.. mongodoc:: changeStreams
"""
cursor_class = create_class_with_framework(
AgnosticChangeStream, self._framework, self.__module__
)
# Latent cursor that will send initial command on first "async for".
return cursor_class(
self,
pipeline,
full_document,
resume_after,
max_await_time_ms,
batch_size,
collation,
start_at_operation_time,
session,
start_after,
comment,
full_document_before_change,
show_expanded_events,
)
async def cursor_command(
self,
command,
value=1,
read_preference=None,
codec_options=None,
session=None,
comment=None,
max_await_time_ms=None,
**kwargs,
):
"""Issue a MongoDB command and parse the response as a cursor.
If the response from the server does not include a cursor field, an error will be thrown.
Otherwise, behaves identically to issuing a normal MongoDB command.
:Parameters:
- `command`: document representing the command to be issued,
or the name of the command (for simple commands only).
.. note:: the order of keys in the `command` document is
significant (the "verb" must come first), so commands
which require multiple keys (e.g. `findandmodify`)
should use an instance of :class:`~bson.son.SON` or
a string and kwargs instead of a Python `dict`.
- `value` (optional): value to use for the command verb when
`command` is passed as a string
- `read_preference` (optional): The read preference for this
operation. See :mod:`~pymongo.read_preferences` for options.
If the provided `session` is in a transaction, defaults to the
read preference configured for the transaction.
Otherwise, defaults to
:attr:`~pymongo.read_preferences.ReadPreference.PRIMARY`.
- `codec_options`: A :class:`~bson.codec_options.CodecOptions`
instance.
- `session` (optional): A
:class:`MotorClientSession`.
- `comment` (optional): A user-provided comment to attach to future getMores for this
command.
- `max_await_time_ms` (optional): The number of ms to wait for more data on future
getMores for this command.
- `**kwargs` (optional): additional keyword arguments will
be added to the command document before it is sent
.. note:: :meth:`command` does **not** obey this Database's
:attr:`read_preference` or :attr:`codec_options`. You must use the
``read_preference`` and ``codec_options`` parameters instead.
.. note:: :meth:`command` does **not** apply any custom TypeDecoders
when decoding the command response.
.. note:: If this client has been configured to use MongoDB Stable
API (see :ref:`versioned-api-ref`), then :meth:`command` will
automatically add API versioning options to the given command.
Explicitly adding API versioning options in the command and
declaring an API version on the client is not supported.
.. seealso:: The MongoDB documentation on `commands <https://dochub.mongodb.org/core/commands>`_.
"""
args = (command,)
kwargs["value"] = value
kwargs["read_preference"] = read_preference
kwargs["codec_options"] = codec_options
kwargs["session"] = session
kwargs["comment"] = comment
kwargs["max_await_time_ms"] = max_await_time_ms
def inner():
return self.delegate.cursor_command(
*unwrap_args_session(args), **unwrap_kwargs_session(kwargs)
)
loop = self.get_io_loop()
cursor = await self._framework.run_on_executor(loop, inner)
cursor_class = create_class_with_framework(
AgnosticCommandCursor, self._framework, self.__module__
)
return cursor_class(cursor, self)
@property
def client(self):
"""This MotorDatabase's :class:`MotorClient`."""
return self._client
def __getattr__(self, name):
if name.startswith("_"):
raise AttributeError(
f"{self.__class__.__name__} has no attribute {name!r}. To access the {name}"
" collection, use database['{name}']."
)
return self[name]
def __getitem__(self, name):
collection_class = create_class_with_framework(
AgnosticCollection, self._framework, self.__module__
)
return collection_class(self, name)
def __call__(self, *args, **kwargs):
database_name = self.delegate.name
client_class_name = self._client.__class__.__name__
if database_name == "open_sync":
raise TypeError(
f"{client_class_name}.open_sync() is unnecessary Motor 0.2, "
"see changelog for details."
)
raise TypeError(
"MotorDatabase object is not callable. If you meant to "
f"call the '{database_name}' method on a {client_class_name} object it is "
"failing because no such method exists."
)
def wrap(self, obj):
if obj.__class__ is Collection:
# Replace pymongo.collection.Collection with MotorCollection.
klass = create_class_with_framework(
AgnosticCollection, self._framework, self.__module__
)
return klass(self, obj.name, _delegate=obj)
elif obj.__class__ is Database:
return self.__class__(self._client, obj.name, _delegate=obj)
elif obj.__class__ is CommandCursor:
command_cursor_class = create_class_with_framework(
AgnosticCommandCursor, self._framework, self.__module__
)
return command_cursor_class(obj, self)
else:
return obj
def get_io_loop(self):
return self._client.get_io_loop()
class AgnosticCollection(AgnosticBaseProperties):
__motor_class_name__ = "MotorCollection"
__delegate_class__ = Collection
__hash__ = DelegateMethod()
__bool__ = DelegateMethod()
bulk_write = AsyncCommand(doc=docstrings.bulk_write_doc)
count_documents = AsyncRead()
create_index = AsyncCommand(doc=docstrings.create_index_doc)
create_indexes = AsyncCommand(doc=docstrings.create_indexes_doc)
create_search_index = AsyncCommand()
create_search_indexes = AsyncCommand()
delete_many = AsyncCommand(doc=docstrings.delete_many_doc)
delete_one = AsyncCommand(doc=docstrings.delete_one_doc)
distinct = AsyncRead()
drop = AsyncCommand(doc=docstrings.drop_doc)
drop_index = AsyncCommand()
drop_search_index = AsyncCommand()
drop_indexes = AsyncCommand()
estimated_document_count = AsyncCommand()
find_one = AsyncRead(doc=docstrings.find_one_doc)
find_one_and_delete = AsyncCommand(doc=docstrings.find_one_and_delete_doc)
find_one_and_replace = AsyncCommand(doc=docstrings.find_one_and_replace_doc)
find_one_and_update = AsyncCommand(doc=docstrings.find_one_and_update_doc)
full_name = ReadOnlyProperty()
index_information = AsyncRead(doc=docstrings.index_information_doc)
insert_many = AsyncWrite(doc=docstrings.insert_many_doc)
insert_one = AsyncCommand(doc=docstrings.insert_one_doc)
name = ReadOnlyProperty()
options = AsyncRead()
rename = AsyncCommand()
replace_one = AsyncCommand(doc=docstrings.replace_one_doc)
update_many = AsyncCommand(doc=docstrings.update_many_doc)
update_one = AsyncCommand(doc=docstrings.update_one_doc)
update_search_index = AsyncCommand()
with_options = DelegateMethod().wrap(Collection)
_async_aggregate = AsyncRead(attr_name="aggregate")
_async_aggregate_raw_batches = AsyncRead(attr_name="aggregate_raw_batches")
_async_list_indexes = AsyncRead(attr_name="list_indexes")
_async_list_search_indexes = AsyncRead(attr_name="list_search_indexes")
def __init__(
self,
database,
name,
codec_options=None,
read_preference=None,
write_concern=None,
read_concern=None,
_delegate=None,
):
db_class = create_class_with_framework(AgnosticDatabase, self._framework, self.__module__)
if not isinstance(database, db_class):
raise TypeError(
"First argument to MotorCollection must be MotorDatabase, not %r" % database
)
delegate = (
_delegate
if _delegate is not None
else Collection(
database.delegate,
name,
codec_options=codec_options,
read_preference=read_preference,
write_concern=write_concern,
read_concern=read_concern,
)
)
super().__init__(delegate)
self.database = database
def __getattr__(self, name):
# Dotted collection name, like "foo.bar".
if name.startswith("_"):
full_name = f"{self.name}.{name}"
raise AttributeError(
f"{self.__class__.__name__} has no attribute {name!r}. To access the {full_name}"
f" collection, use database['{full_name}']."
)
return self[name]
def __getitem__(self, name):
collection_class = create_class_with_framework(
AgnosticCollection, self._framework, self.__module__
)
return collection_class(
self.database, self.name + "." + name, _delegate=self.delegate[name]
)
def __call__(self, *args, **kwargs):
raise TypeError(
"MotorCollection object is not callable. If you meant to "
"call the '%s' method on a MotorCollection object it is "
"failing because no such method exists." % self.delegate.name
)
def find(self, *args, **kwargs):
"""Create a :class:`MotorCursor`. Same parameters as for
PyMongo's :meth:`~pymongo.collection.Collection.find`.
Note that ``find`` does not require an ``await`` expression, because
``find`` merely creates a
:class:`MotorCursor` without performing any operations on the server.
``MotorCursor`` methods such as :meth:`~MotorCursor.to_list`
perform actual operations.
"""
cursor = self.delegate.find(*unwrap_args_session(args), **unwrap_kwargs_session(kwargs))
cursor_class = create_class_with_framework(AgnosticCursor, self._framework, self.__module__)
return cursor_class(cursor, self)
def find_raw_batches(self, *args, **kwargs):
"""Query the database and retrieve batches of raw BSON.
Similar to the :meth:`find` method but returns each batch as bytes.
This example demonstrates how to work with raw batches, but in practice
raw batches should be passed to an external library that can decode
BSON into another data type, rather than used with PyMongo's
:mod:`bson` module.
.. code-block:: python3
async def get_raw():
cursor = db.test.find_raw_batches()
async for batch in cursor:
print(bson.decode_all(batch))
Note that ``find_raw_batches`` does not support sessions.
.. versionadded:: 2.0
"""
cursor = self.delegate.find_raw_batches(
*unwrap_args_session(args), **unwrap_kwargs_session(kwargs)
)
cursor_class = create_class_with_framework(
AgnosticRawBatchCursor, self._framework, self.__module__
)
return cursor_class(cursor, self)
def aggregate(self, pipeline, *args, **kwargs):
"""Execute an aggregation pipeline on this collection.
The aggregation can be run on a secondary if the client is connected
to a replica set and its ``read_preference`` is not :attr:`PRIMARY`.
:Parameters:
- `pipeline`: a single command or list of aggregation commands
- `session` (optional): a
:class:`~pymongo.client_session.ClientSession`, created with
:meth:`~MotorClient.start_session`.
- `**kwargs`: send arbitrary parameters to the aggregate command
All optional `aggregate command`_ parameters should be passed as
keyword arguments to this method. Valid options include, but are not
limited to:
- `allowDiskUse` (bool): Enables writing to temporary files. When set
to True, aggregation stages can write data to the _tmp subdirectory
of the --dbpath directory. The default is False.
- `maxTimeMS` (int): The maximum amount of time to allow the operation
to run in milliseconds.
- `batchSize` (int): The maximum number of documents to return per
batch. Ignored if the connected mongod or mongos does not support
returning aggregate results using a cursor.
- `collation` (optional): An instance of
:class:`~pymongo.collation.Collation`.
- `let` (dict): A dict of parameter names and values. Values must be
constant or closed expressions that do not reference document
fields. Parameters can then be accessed as variables in an
aggregate expression context (e.g. ``"$$var"``). This option is
only supported on MongoDB >= 5.0.
Returns a :class:`MotorCommandCursor` that can be iterated like a
cursor from :meth:`find`::
async def f():
pipeline = [{'$project': {'name': {'$toUpper': '$name'}}}]
async for doc in collection.aggregate(pipeline):
print(doc)
Note that this method returns a :class:`MotorCommandCursor` which
lazily runs the aggregate command when first iterated. In order to run
an aggregation with ``$out`` or ``$merge`` the application needs to
iterate the cursor, for example::
cursor = motor_coll.aggregate([{'$out': 'out'}])
# Iterate the cursor to run the $out (or $merge) operation.
await cursor.to_list(length=None)
# Or more succinctly:
await motor_coll.aggregate([{'$out': 'out'}]).to_list(length=None)
# Or:
async for _ in motor_coll.aggregate([{'$out': 'out'}]):
pass
:class:`MotorCommandCursor` does not allow the ``explain`` option. To
explain MongoDB's query plan for the aggregation, use
:meth:`MotorDatabase.command`::
async def f():
plan = await db.command(
'aggregate', 'COLLECTION-NAME',
pipeline=[{'$project': {'x': 1}}],
explain=True)
print(plan)
.. versionchanged:: 2.1
This collection's read concern is now applied to pipelines
containing the `$out` stage when connected to MongoDB >= 4.2.
.. versionchanged:: 1.0
:meth:`aggregate` now **always** returns a cursor.
.. versionchanged:: 0.5
:meth:`aggregate` now returns a cursor by default,
and the cursor is returned immediately without an ``await``.
See :ref:`aggregation changes in Motor 0.5 <aggregate_changes_0_5>`.
.. versionchanged:: 0.2
Added cursor support.
.. _aggregate command:
https://mongodb.com/docs/manual/applications/aggregation
"""
cursor_class = create_class_with_framework(
AgnosticLatentCommandCursor, self._framework, self.__module__
)
# Latent cursor that will send initial command on first "async for".
return cursor_class(
self,
self._async_aggregate,
pipeline,
*unwrap_args_session(args),
**unwrap_kwargs_session(kwargs),
)
def aggregate_raw_batches(self, pipeline, **kwargs):
"""Perform an aggregation and retrieve batches of raw BSON.
Similar to the :meth:`aggregate` method but returns each batch as bytes.
This example demonstrates how to work with raw batches, but in practice
raw batches should be passed to an external library that can decode
BSON into another data type, rather than used with PyMongo's
:mod:`bson` module.
.. code-block:: python3
async def get_raw():
cursor = db.test.aggregate_raw_batches()
async for batch in cursor:
print(bson.decode_all(batch))
Note that ``aggregate_raw_batches`` does not support sessions.
.. versionadded:: 2.0
"""
cursor_class = create_class_with_framework(
AgnosticLatentCommandCursor, self._framework, self.__module__
)
# Latent cursor that will send initial command on first "async for".
return cursor_class(
self, self._async_aggregate_raw_batches, pipeline, **unwrap_kwargs_session(kwargs)
)
def watch(
self,
pipeline=None,
full_document=None,
resume_after=None,
max_await_time_ms=None,
batch_size=None,
collation=None,
start_at_operation_time=None,
session=None,
start_after=None,
comment=None,
full_document_before_change=None,
show_expanded_events=None,
):
"""Watch changes on this collection.
Performs an aggregation with an implicit initial ``$changeStream``
stage and returns a :class:`~MotorChangeStream` cursor which
iterates over changes on this collection.
Introduced in MongoDB 3.6.
A change stream continues waiting indefinitely for matching change
events. Code like the following allows a program to cancel the change
stream and exit.
.. code-block:: python3
change_stream = None
async def watch_collection():
global change_stream
# Using the change stream in an "async with" block
# ensures it is canceled promptly if your code breaks
# from the loop or throws an exception.
async with db.collection.watch() as change_stream:
async for change in change_stream:
print(change)
# Tornado
from tornado.ioloop import IOLoop
def main():
loop = IOLoop.current()
# Start watching collection for changes.
try:
loop.run_sync(watch_collection)
except KeyboardInterrupt:
if change_stream:
loop.run_sync(change_stream.close)
# asyncio
try:
asyncio.run(watch_collection())
except KeyboardInterrupt:
pass
The :class:`~MotorChangeStream` async iterable blocks
until the next change document is returned or an error is raised. If
the :meth:`~MotorChangeStream.next` method encounters
a network error when retrieving a batch from the server, it will
automatically attempt to recreate the cursor such that no change
events are missed. Any error encountered during the resume attempt
indicates there may be an outage and will be raised.
.. code-block:: python3
try:
pipeline = [{"$match": {"operationType": "insert"}}]
async with db.collection.watch(pipeline) as stream:
async for change in stream:
print(change)
except pymongo.errors.PyMongoError:
# The ChangeStream encountered an unrecoverable error or the
# resume attempt failed to recreate the cursor.
logging.error("...")
For a precise description of the resume process see the
`change streams specification`_.
:Parameters:
- `pipeline` (optional): A list of aggregation pipeline stages to
append to an initial ``$changeStream`` stage. Not all
pipeline stages are valid after a ``$changeStream`` stage, see the
MongoDB documentation on change streams for the supported stages.
- `full_document` (optional): The fullDocument option to pass
to the ``$changeStream`` stage. Allowed values: 'updateLookup'.
When set to 'updateLookup', the change notification for partial
updates will include both a delta describing the changes to the
document, as well as a copy of the entire document that was
changed from some time after the change occurred.
- `resume_after` (optional): A resume token. If provided, the
change stream will start returning changes that occur directly
after the operation specified in the resume token. A resume token
is the _id value of a change document.
- `max_await_time_ms` (optional): The maximum time in milliseconds
for the server to wait for changes before responding to a getMore
operation.
- `batch_size` (optional): The maximum number of documents to return
per batch.
- `collation` (optional): The :class:`~pymongo.collation.Collation`
to use for the aggregation.
- `session` (optional): a
:class:`~pymongo.client_session.ClientSession`.
- `start_after` (optional): The same as `resume_after` except that
`start_after` can resume notifications after an invalidate event.
This option and `resume_after` are mutually exclusive.
- `comment` (optional): A user-provided comment to attach to this
command.
- `full_document_before_change`: Allowed values: `whenAvailable` and `required`.
Change events may now result in a `fullDocumentBeforeChange` response field.
- `show_expanded_events` (optional): Include expanded events such as DDL events
like `dropIndexes`.
:Returns:
A :class:`~MotorChangeStream`.
See the :ref:`tornado_change_stream_example`.
.. versionchanged:: 3.2
Added ``show_expanded_events`` parameter.
.. versionchanged:: 3.1
Added ``full_document_before_change`` parameter.
.. versionchanged:: 3.0
Added ``comment`` parameter.
.. versionchanged:: 2.1
Added the ``start_after`` parameter.
.. versionadded:: 1.2
.. mongodoc:: changeStreams
.. _change streams specification:
https://github.com/mongodb/specifications/blob/master/source/change-streams/change-streams.rst
"""
cursor_class = create_class_with_framework(
AgnosticChangeStream, self._framework, self.__module__
)
# Latent cursor that will send initial command on first "async for".
return cursor_class(
self,
pipeline,
full_document,
resume_after,
max_await_time_ms,
batch_size,
collation,
start_at_operation_time,
session,
start_after,
comment,
full_document_before_change,
show_expanded_events,
)
def list_indexes(self, session=None, **kwargs):
"""Get a cursor over the index documents for this collection. ::
async def print_indexes():
async for index in db.test.list_indexes():
print(index)
If the only index is the default index on ``_id``, this might print::
SON([('v', 1), ('key', SON([('_id', 1)])), ('name', '_id_')])
"""
cursor_class = create_class_with_framework(
AgnosticLatentCommandCursor, self._framework, self.__module__
)
# Latent cursor that will send initial command on first "async for".
return cursor_class(self, self._async_list_indexes, session=session, **kwargs)
def list_search_indexes(self, *args, **kwargs):
"""Return a cursor over search indexes for the current collection."""
cursor_class = create_class_with_framework(
AgnosticLatentCommandCursor, self._framework, self.__module__
)
# Latent cursor that will send initial command on first "async for".
return cursor_class(self, self._async_list_search_indexes, *args, **kwargs)
def wrap(self, obj):
if obj.__class__ is Collection:
# Replace pymongo.collection.Collection with MotorCollection.
return self.__class__(self.database, obj.name, _delegate=obj)
elif obj.__class__ is Cursor:
return AgnosticCursor(obj, self)
elif obj.__class__ is CommandCursor:
command_cursor_class = create_class_with_framework(
AgnosticCommandCursor, self._framework, self.__module__
)
return command_cursor_class(obj, self)
elif obj.__class__ is ChangeStream:
change_stream_class = create_class_with_framework(
AgnosticChangeStream, self._framework, self.__module__
)
return change_stream_class(obj, self)
else:
return obj
def get_io_loop(self):
return self.database.get_io_loop()
class AgnosticBaseCursor(AgnosticBase):
"""Base class for AgnosticCursor and AgnosticCommandCursor"""
# Allow the use of generics at runtime.
@classmethod
def __class_getitem__(cls, key: str) -> object:
return cls
_async_close = AsyncRead(attr_name="close")
_refresh = AsyncRead()
address = ReadOnlyProperty()
cursor_id = ReadOnlyProperty()
alive = ReadOnlyProperty()
session = ReadOnlyProperty()
def __init__(self, cursor, collection):
"""Don't construct a cursor yourself, but acquire one from methods like
:meth:`MotorCollection.find` or :meth:`MotorCollection.aggregate`.
.. note::
There is no need to manually close cursors; they are closed
by the server after being fully iterated
with :meth:`to_list`, :meth:`each`, or `async for`, or
automatically closed by the client when the :class:`MotorCursor` is
cleaned up by the garbage collector.
"""
# 'cursor' is a PyMongo Cursor, CommandCursor, or a _LatentCursor.
super().__init__(delegate=cursor)
self.collection = collection
self.started = False
self.closed = False
# python.org/dev/peps/pep-0492/#api-design-and-implementation-revisions
def __aiter__(self):
return self
async def next(self):
"""Advance the cursor.
.. versionadded:: 2.2
"""
if self.alive and (self._buffer_size() or await self._get_more()):
return next(self.delegate)
raise StopAsyncIteration
__anext__ = next
async def __aenter__(self):
return self
async def __aexit__(self, exc_type, exc_val, exc_tb):
if self.delegate:
await self.close()
def _get_more(self):
"""Initial query or getMore. Returns a Future."""
if not self.alive:
raise pymongo.errors.InvalidOperation(
"Can't call get_more() on a MotorCursor that has been exhausted or killed."
)
self.started = True
return self._refresh()
@property
@coroutine_annotation
def fetch_next(self):
"""**DEPRECATED** - A Future used with `gen.coroutine`_ to
asynchronously retrieve the next document in the result set,
fetching a batch of documents from the server if necessary.
Resolves to ``False`` if there are no more documents, otherwise
:meth:`next_object` is guaranteed to return a document:
.. doctest:: fetch_next
:hide:
>>> _ = MongoClient().test.test_collection.delete_many({})
>>> collection = MotorClient().test.test_collection
.. attention:: The :attr:`fetch_next` property is deprecated and will
be removed in Motor 3.0. Use `async for` to iterate elegantly and
efficiently over :class:`MotorCursor` objects instead.:
.. doctest:: fetch_next
>>> async def f():
... await collection.drop()
... await collection.insert_many([{"_id": i} for i in range(5)])
... async for doc in collection.find():
... sys.stdout.write(str(doc["_id"]) + ", ")
... print("done")
...
>>> IOLoop.current().run_sync(f)
0, 1, 2, 3, 4, done
While it appears that fetch_next retrieves each document from
the server individually, the cursor actually fetches documents
efficiently in `large batches`_. Example usage:
.. doctest:: fetch_next
>>> async def f():
... await collection.drop()
... await collection.insert_many([{"_id": i} for i in range(5)])
... cursor = collection.find().sort([("_id", 1)])
... while await cursor.fetch_next:
... doc = cursor.next_object()
... sys.stdout.write(str(doc["_id"]) + ", ")
... print("done")
...
>>> IOLoop.current().run_sync(f)
0, 1, 2, 3, 4, done
.. versionchanged:: 2.2
Deprecated.
.. _`large batches`: https://www.mongodb.com/docs/manual/tutorial/iterate-a-cursor/#cursor-batches
.. _`gen.coroutine`: http://tornadoweb.org/en/stable/gen.html
"""
warnings.warn(
"The fetch_next property is deprecated and may be "
"removed in a future major release. Use `async for` to iterate "
"over Cursor objects instead.",
DeprecationWarning,
stacklevel=2,
)
if not self._buffer_size() and self.alive:
# Return the Future, which resolves to number of docs fetched or 0.
return self._get_more()
elif self._buffer_size():
future = self._framework.get_future(self.get_io_loop())
future.set_result(True)
return future
else:
# Dead
future = self._framework.get_future(self.get_io_loop())
future.set_result(False)
return future
def next_object(self):
"""**DEPRECATED** - Get a document from the most recently fetched
batch, or ``None``. See :attr:`fetch_next`.
The :meth:`next_object` method is deprecated and may be removed
in a future major release. Use `async for` to elegantly iterate over
:class:`MotorCursor` objects instead.
.. versionchanged:: 2.2
Deprecated.
"""
warnings.warn(
"The next_object method is deprecated and may be "
"removed in a future major release. Use `async for` to iterate "
"over Cursor objects instead.",
DeprecationWarning,
stacklevel=2,
)
if not self._buffer_size():
return None
return next(self.delegate)
def each(self, callback):
"""Iterates over all the documents for this cursor.
:meth:`each` returns immediately, and `callback` is executed asynchronously
for each document. `callback` is passed ``(None, None)`` when iteration
is complete.
Cancel iteration early by returning ``False`` from the callback. (Only
``False`` cancels iteration: returning ``None`` or 0 does not.)
.. testsetup:: each
from tornado.ioloop import IOLoop
MongoClient().test.test_collection.delete_many({})
MongoClient().test.test_collection.insert_many([{"_id": i} for i in range(5)])
collection = MotorClient().test.test_collection
.. doctest:: each
>>> def each(result, error):
... if error:
... raise error
... elif result:
... sys.stdout.write(str(result["_id"]) + ", ")
... else:
... # Iteration complete
... IOLoop.current().stop()
... print("done")
...
>>> cursor = collection.find().sort([("_id", 1)])
>>> cursor.each(callback=each)
>>> IOLoop.current().start()
0, 1, 2, 3, 4, done
.. note:: Unlike other Motor methods, ``each`` requires a callback and
does not return a Future, so it cannot be used in a coroutine.
``async for`` and :meth:`to_list` are much easier to use.
:Parameters:
- `callback`: function taking (document, error)
"""
if not callable(callback):
raise callback_type_error
self._each_got_more(callback, None)
def _each_got_more(self, callback, future):
if future:
try:
future.result()
except Exception as error:
callback(None, error)
return
while self._buffer_size() > 0:
doc = next(self.delegate) # decrements self.buffer_size
# Quit if callback returns exactly False (not None). Note we
# don't close the cursor: user may want to resume iteration.
if callback(doc, None) is False:
return
# The callback closed this cursor?
if self.closed:
return
if self.alive and (self.cursor_id or not self.started):
self._framework.add_future(
self.get_io_loop(), self._get_more(), self._each_got_more, callback
)
else:
# Complete
self._framework.call_soon(self.get_io_loop(), functools.partial(callback, None, None))
@coroutine_annotation
def to_list(self, length=None):
"""Get a list of documents.
.. testsetup:: to_list
MongoClient().test.test_collection.delete_many({})
MongoClient().test.test_collection.insert_many([{"_id": i} for i in range(4)])
from tornado import ioloop
.. doctest:: to_list
>>> from motor.motor_tornado import MotorClient
>>> collection = MotorClient().test.test_collection
>>>
>>> async def f():
... cursor = collection.find().sort([("_id", 1)])
... docs = await cursor.to_list(length=2)
... while docs:
... print(docs)
... docs = await cursor.to_list(length=2)
... print("done")
...
>>> ioloop.IOLoop.current().run_sync(f)
[{'_id': 0}, {'_id': 1}]
[{'_id': 2}, {'_id': 3}]
done
:Parameters:
- `length`: maximum number of documents to return for this call, or
None
Returns a Future.
.. versionchanged:: 2.0
No longer accepts a callback argument.
.. versionchanged:: 0.2
`callback` must be passed as a keyword argument, like
``to_list(10, callback=callback)``, and the
`length` parameter is no longer optional.
"""
if length is not None:
if not isinstance(length, int):
raise TypeError("length must be an int, not %r" % length)
elif length < 0:
raise ValueError("length must be non-negative")
if self._query_flags() & _QUERY_OPTIONS["tailable_cursor"]:
raise pymongo.errors.InvalidOperation("Can't call to_list on tailable cursor")
future = self._framework.get_future(self.get_io_loop())
if not self.alive:
future.set_result([])
else:
the_list = []
self._framework.add_future(
self.get_io_loop(),
self._get_more(),
self._to_list,
length,
the_list,
future,
)
return future
def _to_list(self, length, the_list, future, get_more_result):
# get_more_result is the result of self._get_more().
# to_list_future will be the result of the user's to_list() call.
try:
result = get_more_result.result()
# Return early if the task was cancelled.
if future.done():
return
if length is None:
n = result
else:
n = min(length - len(the_list), result)
for _ in range(n):
the_list.append(self._data().popleft())
reached_length = length is not None and len(the_list) >= length
if reached_length or not self.alive:
future.set_result(the_list)
else:
self._framework.add_future(
self.get_io_loop(), self._get_more(), self._to_list, length, the_list, future
)
except Exception as exc:
if not future.done():
future.set_exception(exc)
def get_io_loop(self):
return self.collection.get_io_loop()
async def close(self):
"""Explicitly kill this cursor on the server.
Call like::
await cursor.close()
"""
if not self.closed:
self.closed = True
await self._async_close()
def batch_size(self, batch_size):
self.delegate.batch_size(batch_size)
return self
def _buffer_size(self):
return len(self._data())
# Paper over some differences between PyMongo Cursor and CommandCursor.
def _query_flags(self):
raise NotImplementedError
def _data(self):
raise NotImplementedError
def _killed(self):
raise NotImplementedError
class AgnosticCursor(AgnosticBaseCursor):
__motor_class_name__ = "MotorCursor"
__delegate_class__ = Cursor
address = ReadOnlyProperty()
collation = MotorCursorChainingMethod()
distinct = AsyncRead()
explain = AsyncRead()
add_option = MotorCursorChainingMethod()
remove_option = MotorCursorChainingMethod()
limit = MotorCursorChainingMethod()
skip = MotorCursorChainingMethod()
max_scan = MotorCursorChainingMethod()
sort = MotorCursorChainingMethod(doc=docstrings.cursor_sort_doc)
hint = MotorCursorChainingMethod()
where = MotorCursorChainingMethod(doc=docstrings.where_doc)
max_await_time_ms = MotorCursorChainingMethod()
max_time_ms = MotorCursorChainingMethod()
min = MotorCursorChainingMethod()
max = MotorCursorChainingMethod()
comment = MotorCursorChainingMethod()
allow_disk_use = MotorCursorChainingMethod()
def rewind(self):
"""Rewind this cursor to its unevaluated state."""
self.delegate.rewind()
self.started = False
return self
def clone(self):
"""Get a clone of this cursor."""
return self.__class__(self.delegate.clone(), self.collection)
def __copy__(self):
return self.__class__(self.delegate.__copy__(), self.collection)
def __deepcopy__(self, memo):
return self.__class__(self.delegate.__deepcopy__(memo), self.collection)
def _query_flags(self):
return self.delegate._query_flags
def _data(self):
return self.delegate._data
def _killed(self):
return self.delegate._killed
class AgnosticRawBatchCursor(AgnosticCursor):
__motor_class_name__ = "MotorRawBatchCursor"
__delegate_class__ = RawBatchCursor
class AgnosticCommandCursor(AgnosticBaseCursor):
__motor_class_name__ = "MotorCommandCursor"
__delegate_class__ = CommandCursor
async def try_next(self):
"""Advance the cursor without blocking indefinitely.
This method returns the next document without waiting
indefinitely for data.
If no document is cached locally then this method runs a single
getMore command. If the getMore yields any documents, the next
document is returned, otherwise, if the getMore returns no documents
(because there is no additional data) then ``None`` is returned.
:Returns:
The next document or ``None`` when no document is available
after running a single getMore or when the cursor is closed.
"""
def inner():
return self.delegate.try_next()
loop = self.get_io_loop()
return await self._framework.run_on_executor(loop, inner)
def _query_flags(self):
return 0
def _data(self):
return self.delegate._data
def _killed(self):
return self.delegate._killed
class AgnosticRawBatchCommandCursor(AgnosticCommandCursor):
__motor_class_name__ = "MotorRawBatchCommandCursor"
__delegate_class__ = RawBatchCommandCursor
class _LatentCursor:
"""Take the place of a PyMongo CommandCursor until aggregate() begins."""
alive = True
_data = []
_id = None
_killed = False
_sock_mgr = None
_session = None
_explicit_session = None
cursor_id = None
def __init__(self, collection):
self._collection = collection.delegate
def _end_session(self, *args, **kwargs):
pass
def _die_lock(self, *args, **kwargs):
pass
def clone(self):
return _LatentCursor(self._collection)
def rewind(self):
pass
class AgnosticLatentCommandCursor(AgnosticCommandCursor):
__motor_class_name__ = "MotorLatentCommandCursor"
def __init__(self, collection, start, *args, **kwargs):
# We're being constructed without await, like:
#
# cursor = collection.aggregate(pipeline)
#
# ... so we can't send the "aggregate" command to the server and get
# a PyMongo CommandCursor back yet. Set self.delegate to a latent
# cursor until the first await triggers _get_more(), which
# will execute the callback "start", which gets a PyMongo CommandCursor.
super().__init__(_LatentCursor(collection), collection)
self.start = start
self.args = args
self.kwargs = kwargs
def batch_size(self, batch_size):
self.kwargs["batchSize"] = batch_size
return self
def _get_more(self):
if not self.started:
self.started = True
original_future = self._framework.get_future(self.get_io_loop())
future = self.start(*self.args, **self.kwargs)
self.start = self.args = self.kwargs = None
self._framework.add_future(
self.get_io_loop(), future, self._on_started, original_future
)
return original_future
return super()._get_more()
def _on_started(self, original_future, future):
try:
# "result" is a PyMongo command cursor from PyMongo's aggregate() or
# aggregate_raw_batches(). Set its batch size from our latent
# cursor's batch size.
pymongo_cursor = future.result()
self.delegate = pymongo_cursor
except Exception as exc:
if not original_future.done():
original_future.set_exception(exc)
else:
# Return early if the task was cancelled.
if original_future.done():
return
if self.delegate._data or not self.delegate.alive:
# _get_more is complete.
original_future.set_result(len(self.delegate._data))
else:
# Send a getMore.
future = super()._get_more()
self._framework.chain_future(future, original_future)
class AgnosticChangeStream(AgnosticBase):
"""A change stream cursor.
Should not be called directly by application developers. See
:meth:`~MotorCollection.watch` for example usage.
.. versionadded: 1.2
.. mongodoc:: changeStreams
"""
__delegate_class__ = ChangeStream
__motor_class_name__ = "MotorChangeStream"
_close = AsyncCommand(attr_name="close")
resume_token = ReadOnlyProperty()
# Allow the use of generics at runtime.
@classmethod
def __class_getitem__(cls, key: str) -> object:
return cls
def __init__(
self,
target,
pipeline,
full_document,
resume_after,
max_await_time_ms,
batch_size,
collation,
start_at_operation_time,
session,
start_after,
comment,
full_document_before_change,
show_expanded_events,
):
super().__init__(delegate=None)
# The "target" object is a client, database, or collection.
self._target = target
self._kwargs = {
"pipeline": pipeline,
"full_document": full_document,
"resume_after": resume_after,
"max_await_time_ms": max_await_time_ms,
"batch_size": batch_size,
"collation": collation,
"start_at_operation_time": start_at_operation_time,
"session": session,
"start_after": start_after,
"comment": comment,
"full_document_before_change": full_document_before_change,
"show_expanded_events": show_expanded_events,
}
def _lazy_init(self):
if not self.delegate:
self.delegate = self._target.delegate.watch(**unwrap_kwargs_session(self._kwargs))
def _try_next(self):
# This method is run on a thread.
self._lazy_init()
return self.delegate.try_next()
@property
def alive(self):
"""Does this cursor have the potential to return more data?
.. note:: Even if :attr:`alive` is ``True``, :meth:`next` can raise
:exc:`StopAsyncIteration` and :meth:`try_next` can return ``None``.
"""
if not self.delegate:
# Not yet fully initialized, so we may return data.
return True
return self.delegate.alive
async def next(self):
"""Advance the cursor.
This method blocks until the next change document is returned or an
unrecoverable error is raised. This method is used when iterating over
all changes in the cursor. For example::
async def watch_collection():
resume_token = None
pipeline = [{'$match': {'operationType': 'insert'}}]
try:
async with db.collection.watch(pipeline) as stream:
async for insert_change in stream:
print(insert_change)
resume_token = stream.resume_token
except pymongo.errors.PyMongoError:
# The ChangeStream encountered an unrecoverable error or the
# resume attempt failed to recreate the cursor.
if resume_token is None:
# There is no usable resume token because there was a
# failure during ChangeStream initialization.
logging.error('...')
else:
# Use the interrupted ChangeStream's resume token to
# create a new ChangeStream. The new stream will
# continue from the last seen insert change without
# missing any events.
async with db.collection.watch(
pipeline, resume_after=resume_token) as stream:
async for insert_change in stream:
print(insert_change)
Raises :exc:`StopAsyncIteration` if this change stream is closed.
In addition to using an "async for" loop as shown in the code
example above, you can also iterate the change stream by calling
``await change_stream.next()`` repeatedly.
"""
while self.alive:
doc = await self.try_next()
if doc is not None:
return doc
raise StopAsyncIteration()
async def try_next(self):
"""Advance the cursor without blocking indefinitely.
This method returns the next change document without waiting
indefinitely for the next change. If no changes are available,
it returns None. For example:
.. code-block:: python3
while change_stream.alive:
change = await change_stream.try_next()
# Note that the ChangeStream's resume token may be updated
# even when no changes are returned.
print("Current resume token: %r" % (change_stream.resume_token,))
if change is not None:
print("Change document: %r" % (change,))
continue
# We end up here when there are no recent changes.
# Sleep for a while before trying again to avoid flooding
# the server with getMore requests when no changes are
# available.
await asyncio.sleep(10)
If no change document is cached locally then this method runs a single
getMore command. If the getMore yields any documents, the next
document is returned, otherwise, if the getMore returns no documents
(because there have been no changes) then ``None`` is returned.
:Returns:
The next change document or ``None`` when no document is available
after running a single getMore or when the cursor is closed.
.. versionadded:: 2.1
"""
loop = self.get_io_loop()
return await self._framework.run_on_executor(loop, self._try_next)
async def close(self):
"""Close this change stream.
Stops any "async for" loops using this change stream.
"""
if self.delegate:
await self._close()
def __aiter__(self):
return self
__anext__ = next
async def __aenter__(self):
if not self.delegate:
loop = self.get_io_loop()
await self._framework.run_on_executor(loop, self._lazy_init)
return self
async def __aexit__(self, exc_type, exc_val, exc_tb):
if self.delegate:
await self.close()
def get_io_loop(self):
return self._target.get_io_loop()
def __enter__(self):
raise RuntimeError('Use a change stream in "async with", not "with"')
def __exit__(self, exc_type, exc_val, exc_tb):
pass
class AgnosticClientEncryption(AgnosticBase):
"""Explicit client-side field level encryption."""
__motor_class_name__ = "MotorClientEncryption"
__delegate_class__ = ClientEncryption
create_data_key = AsyncCommand(doc=docstrings.create_data_key_doc)
encrypt = AsyncCommand()
encrypt_expression = AsyncCommand()
decrypt = AsyncCommand()
close = AsyncCommand(doc=docstrings.close_doc)
# Key Management API
rewrap_many_data_key = AsyncCommand()
delete_key = AsyncCommand()
get_key = AsyncCommand()
add_key_alt_name = AsyncCommand()
get_key_by_alt_name = AsyncCommand()
remove_key_alt_name = AsyncCommand()
# Allow the use of generics at runtime.
@classmethod
def __class_getitem__(cls, key: str) -> object:
return cls
def __init__(
self,
kms_providers,
key_vault_namespace,
key_vault_client,
codec_options,
io_loop=None,
kms_tls_options=None,
):
"""Explicit client-side field level encryption.
Takes the same constructor arguments as
:class:`pymongo.encryption.ClientEncryption`, as well as:
:Parameters:
- `io_loop` (optional): Special event loop
instance to use instead of default.
"""
if io_loop:
self._framework.check_event_loop(io_loop)
else:
io_loop = None
sync_client = key_vault_client.delegate
delegate = self.__delegate_class__(
kms_providers, key_vault_namespace, sync_client, codec_options, kms_tls_options
)
super().__init__(delegate)
self._io_loop = io_loop
@property
def io_loop(self):
if self._io_loop is None:
self._io_loop = self._framework.get_event_loop()
return self._io_loop
def get_io_loop(self):
return self.io_loop
async def __aenter__(self):
return self
async def __aexit__(self, exc_type, exc_val, exc_tb):
if self.delegate:
await self.close()
def __enter__(self):
raise RuntimeError(f'Use {self.__class__.__name__} in "async with", not "with"')
def __exit__(self, exc_type, exc_val, exc_tb):
pass
async def get_keys(self):
cursor_class = create_class_with_framework(AgnosticCursor, self._framework, self.__module__)
return cursor_class(self.delegate.get_keys(), self)
async def create_encrypted_collection(
self,
database,
name,
encrypted_fields,
kms_provider=None,
master_key=None,
**kwargs,
):
"""Create a collection with encryptedFields.
.. warning::
This function does not update the encryptedFieldsMap in the client's
AutoEncryptionOpts, thus the user must create a new client after calling
this function with the encryptedFields returned.
Normally collection creation is automatic. This method should
only be used to specify options on
creation. :class:`~pymongo.errors.EncryptionError` will be
raised if the collection already exists.
:Parameters:
- `database`: the database in which to create a collection
- `name`: the name of the collection to create
- `encrypted_fields` (dict): Document that describes the encrypted fields for
Queryable Encryption. For example::
{
"escCollection": "enxcol_.encryptedCollection.esc",
"ecocCollection": "enxcol_.encryptedCollection.ecoc",
"fields": [
{
"path": "firstName",
"keyId": Binary.from_uuid(UUID('00000000-0000-0000-0000-000000000000')),
"bsonType": "string",
"queries": {"queryType": "equality"}
},
{
"path": "ssn",
"keyId": Binary.from_uuid(UUID('04104104-1041-0410-4104-104104104104')),
"bsonType": "string"
}
]
}
The "keyId" may be set to ``None`` to auto-generate the data keys.
- `kms_provider` (optional): the KMS provider to be used
- `master_key` (optional): Identifies a KMS-specific key used to encrypt the
new data key. If the kmsProvider is "local" the `master_key` is
not applicable and may be omitted.
- `**kwargs` (optional): additional keyword arguments are the same as "create_collection".
All optional `create collection command`_ parameters should be passed
as keyword arguments to this method.
See the documentation for :meth:`~pymongo.database.Database.create_collection`
for all valid options.
:Raises:
- :class:`~pymongo.errors.EncryptedCollectionError`: When either data-key creation or
creating the collection fails.
.. versionadded:: 3.2
.. _create collection command:
https://mongodb.com/docs/manual/reference/command/create
"""
collection_class = create_class_with_framework(
AgnosticCollection, self._framework, self.__module__
)
loop = self.get_io_loop()
coll, ef = await self._framework.run_on_executor(
loop,
self.delegate.create_encrypted_collection,
database=database.delegate,
name=name,
encrypted_fields=encrypted_fields,
kms_provider=kms_provider,
master_key=master_key,
**kwargs,
)
return collection_class(database, coll.name, _delegate=coll), ef
|