/usr/share/doc/python-pymongo-doc/html/api/pymongo/collection.html is in python-pymongo-doc 3.4.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>collection – Collection level operations — PyMongo 3.4.0 documentation</title>
<link rel="stylesheet" href="../../_static/pydoctheme.css" type="text/css" />
<link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: '../../',
VERSION: '3.4.0',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="../../_static/jquery.js"></script>
<script type="text/javascript" src="../../_static/underscore.js"></script>
<script type="text/javascript" src="../../_static/doctools.js"></script>
<script type="text/javascript" src="../../_static/sidebar.js"></script>
<link rel="index" title="Index" href="../../genindex.html" />
<link rel="search" title="Search" href="../../search.html" />
<link rel="top" title="PyMongo 3.4.0 documentation" href="../../index.html" />
<link rel="up" title="pymongo – Python driver for MongoDB" href="index.html" />
<link rel="next" title="command_cursor – Tools for iterating over MongoDB command results" href="command_cursor.html" />
<link rel="prev" title="collation – Tools for working with collations." href="collation.html" />
</head>
<body role="document">
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="command_cursor.html" title="command_cursor – Tools for iterating over MongoDB command results"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="collation.html" title="collation – Tools for working with collations."
accesskey="P">previous</a> |</li>
<li class="nav-item nav-item-0"><a href="../../index.html">PyMongo 3.4.0 documentation</a> »</li>
<li class="nav-item nav-item-1"><a href="../index.html" >API Documentation</a> »</li>
<li class="nav-item nav-item-2"><a href="index.html" accesskey="U"><code class="docutils literal"><span class="pre">pymongo</span></code> – Python driver for MongoDB</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="module-pymongo.collection">
<span id="collection-collection-level-operations"></span><h1><code class="xref py py-mod docutils literal"><span class="pre">collection</span></code> – Collection level operations<a class="headerlink" href="#module-pymongo.collection" title="Permalink to this headline">¶</a></h1>
<p>Collection level utilities for Mongo.</p>
<dl class="data">
<dt id="pymongo.ASCENDING">
<code class="descclassname">pymongo.</code><code class="descname">ASCENDING</code><em class="property"> = 1</em><a class="headerlink" href="#pymongo.ASCENDING" title="Permalink to this definition">¶</a></dt>
<dd><p>Ascending sort order.</p>
</dd></dl>
<dl class="data">
<dt id="pymongo.DESCENDING">
<code class="descclassname">pymongo.</code><code class="descname">DESCENDING</code><em class="property"> = -1</em><a class="headerlink" href="#pymongo.DESCENDING" title="Permalink to this definition">¶</a></dt>
<dd><p>Descending sort order.</p>
</dd></dl>
<dl class="data">
<dt id="pymongo.GEO2D">
<code class="descclassname">pymongo.</code><code class="descname">GEO2D</code><em class="property"> = '2d'</em><a class="headerlink" href="#pymongo.GEO2D" title="Permalink to this definition">¶</a></dt>
<dd><p>Index specifier for a 2-dimensional <a class="reference external" href="http://docs.mongodb.org/manual/core/2d/">geospatial index</a>.</p>
</dd></dl>
<dl class="data">
<dt id="pymongo.GEOHAYSTACK">
<code class="descclassname">pymongo.</code><code class="descname">GEOHAYSTACK</code><em class="property"> = 'geoHaystack'</em><a class="headerlink" href="#pymongo.GEOHAYSTACK" title="Permalink to this definition">¶</a></dt>
<dd><p>Index specifier for a 2-dimensional <a class="reference external" href="http://docs.mongodb.org/manual/core/geohaystack/">haystack index</a>.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.1.</span></p>
</div>
</dd></dl>
<dl class="data">
<dt id="pymongo.GEOSPHERE">
<code class="descclassname">pymongo.</code><code class="descname">GEOSPHERE</code><em class="property"> = '2dsphere'</em><a class="headerlink" href="#pymongo.GEOSPHERE" title="Permalink to this definition">¶</a></dt>
<dd><p>Index specifier for a <a class="reference external" href="http://docs.mongodb.org/manual/core/2dsphere/">spherical geospatial index</a>.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.5.</span></p>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">2dsphere indexing requires server version <strong>>= 2.4.0</strong>.</p>
</div>
</dd></dl>
<dl class="data">
<dt id="pymongo.HASHED">
<code class="descclassname">pymongo.</code><code class="descname">HASHED</code><em class="property"> = 'hashed'</em><a class="headerlink" href="#pymongo.HASHED" title="Permalink to this definition">¶</a></dt>
<dd><p>Index specifier for a <a class="reference external" href="http://docs.mongodb.org/manual/core/index-hashed/">hashed index</a>.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.5.</span></p>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">hashed indexing requires server version <strong>>= 2.4.0</strong>.</p>
</div>
</dd></dl>
<dl class="data">
<dt id="pymongo.TEXT">
<code class="descclassname">pymongo.</code><code class="descname">TEXT</code><em class="property"> = 'text'</em><a class="headerlink" href="#pymongo.TEXT" title="Permalink to this definition">¶</a></dt>
<dd><p>Index specifier for a <a class="reference external" href="http://docs.mongodb.org/manual/core/index-text/">text index</a>.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.7.1.</span></p>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">text search requires server version <strong>>= 2.4.0</strong>.</p>
</div>
</dd></dl>
<dl class="class">
<dt id="pymongo.collection.ReturnDocument">
<em class="property">class </em><code class="descclassname">pymongo.collection.</code><code class="descname">ReturnDocument</code><a class="headerlink" href="#pymongo.collection.ReturnDocument" title="Permalink to this definition">¶</a></dt>
<dd><p>An enum used with
<a class="reference internal" href="#pymongo.collection.Collection.find_one_and_replace" title="pymongo.collection.Collection.find_one_and_replace"><code class="xref py py-meth docutils literal"><span class="pre">find_one_and_replace()</span></code></a> and
<a class="reference internal" href="#pymongo.collection.Collection.find_one_and_update" title="pymongo.collection.Collection.find_one_and_update"><code class="xref py py-meth docutils literal"><span class="pre">find_one_and_update()</span></code></a>.</p>
<dl class="attribute">
<dt id="pymongo.collection.ReturnDocument.BEFORE">
<code class="descname">BEFORE</code><a class="headerlink" href="#pymongo.collection.ReturnDocument.BEFORE" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the original document before it was updated/replaced, or
<code class="docutils literal"><span class="pre">None</span></code> if no document matches the query.</p>
</dd></dl>
<dl class="attribute">
<dt id="pymongo.collection.ReturnDocument.AFTER">
<code class="descname">AFTER</code><a class="headerlink" href="#pymongo.collection.ReturnDocument.AFTER" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the updated/replaced or inserted document.</p>
</dd></dl>
</dd></dl>
<dl class="class">
<dt id="pymongo.collection.Collection">
<em class="property">class </em><code class="descclassname">pymongo.collection.</code><code class="descname">Collection</code><span class="sig-paren">(</span><em>database</em>, <em>name</em>, <em>create=False</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection" title="Permalink to this definition">¶</a></dt>
<dd><p>Get / create a Mongo collection.</p>
<p>Raises <code class="xref py py-class docutils literal"><span class="pre">TypeError</span></code> if <cite>name</cite> is not an instance of
<code class="xref py py-class docutils literal"><span class="pre">basestring</span></code> (<code class="xref py py-class docutils literal"><span class="pre">str</span></code> in python 3). Raises
<a class="reference internal" href="errors.html#pymongo.errors.InvalidName" title="pymongo.errors.InvalidName"><code class="xref py py-class docutils literal"><span class="pre">InvalidName</span></code></a> if <cite>name</cite> is not a valid
collection name. Any additional keyword arguments will be used
as options passed to the create command. See
<a class="reference internal" href="database.html#pymongo.database.Database.create_collection" title="pymongo.database.Database.create_collection"><code class="xref py py-meth docutils literal"><span class="pre">create_collection()</span></code></a> for valid
options.</p>
<p>If <cite>create</cite> is <code class="docutils literal"><span class="pre">True</span></code>, <cite>collation</cite> is specified, or any additional
keyword arguments are present, a <code class="docutils literal"><span class="pre">create</span></code> command will be
sent. Otherwise, a <code class="docutils literal"><span class="pre">create</span></code> command will not be sent and the
collection will be created implicitly on first use.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><cite>database</cite>: the database to get a collection from</li>
<li><cite>name</cite>: the name of the collection to get</li>
<li><cite>create</cite> (optional): if <code class="docutils literal"><span class="pre">True</span></code>, force collection
creation even without options being set</li>
<li><cite>codec_options</cite> (optional): An instance of
<a class="reference internal" href="../bson/codec_options.html#bson.codec_options.CodecOptions" title="bson.codec_options.CodecOptions"><code class="xref py py-class docutils literal"><span class="pre">CodecOptions</span></code></a>. If <code class="docutils literal"><span class="pre">None</span></code> (the
default) database.codec_options is used.</li>
<li><cite>read_preference</cite> (optional): The read preference to use. If
<code class="docutils literal"><span class="pre">None</span></code> (the default) database.read_preference is used.</li>
<li><cite>write_concern</cite> (optional): An instance of
<a class="reference internal" href="write_concern.html#pymongo.write_concern.WriteConcern" title="pymongo.write_concern.WriteConcern"><code class="xref py py-class docutils literal"><span class="pre">WriteConcern</span></code></a>. If <code class="docutils literal"><span class="pre">None</span></code> (the
default) database.write_concern is used.</li>
<li><cite>read_concern</cite> (optional): An instance of
<a class="reference internal" href="read_concern.html#pymongo.read_concern.ReadConcern" title="pymongo.read_concern.ReadConcern"><code class="xref py py-class docutils literal"><span class="pre">ReadConcern</span></code></a>. If <code class="docutils literal"><span class="pre">None</span></code> (the
default) database.read_concern is used.</li>
<li><cite>collation</cite> (optional): An instance of
<a class="reference internal" href="collation.html#pymongo.collation.Collation" title="pymongo.collation.Collation"><code class="xref py py-class docutils literal"><span class="pre">Collation</span></code></a>. If a collation is provided,
it will be passed to the create collection command. This option is
only supported on MongoDB 3.4 and above.</li>
<li><cite>**kwargs</cite> (optional): additional keyword arguments will
be passed as options for the create collection command</li>
</ul>
</td>
</tr>
</tbody>
</table>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.4: </span>Support the <cite>collation</cite> option.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.2: </span>Added the read_concern option.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.0: </span>Added the codec_options, read_preference, and write_concern options.
Removed the uuid_subtype attribute.
<a class="reference internal" href="#pymongo.collection.Collection" title="pymongo.collection.Collection"><code class="xref py py-class docutils literal"><span class="pre">Collection</span></code></a> no longer returns an
instance of <a class="reference internal" href="#pymongo.collection.Collection" title="pymongo.collection.Collection"><code class="xref py py-class docutils literal"><span class="pre">Collection</span></code></a> for attribute
names with leading underscores. You must use dict-style lookups
instead::</p>
<blockquote>
<div>collection[‘__my_collection__’]</div></blockquote>
<p>Not:</p>
<blockquote>
<div>collection.__my_collection__</div></blockquote>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 2.2: </span>Removed deprecated argument: options</p>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.1: </span>uuid_subtype attribute</p>
</div>
<div class="admonition-see-general-mongodb-documentation admonition seealso">
<p class="first admonition-title">See also</p>
<p class="admonition-title">See general MongoDB documentation</p>
<p class="last"><a class="reference external" href="http://dochub.mongodb.org/core/collections" name="pymongo.collection.Collection"><em>collections</em></a></p>
</div>
<dl class="describe">
<dt>
<code class="descname">c[name] || c.name</code></dt>
<dd><p>Get the <cite>name</cite> sub-collection of <a class="reference internal" href="#pymongo.collection.Collection" title="pymongo.collection.Collection"><code class="xref py py-class docutils literal"><span class="pre">Collection</span></code></a> <cite>c</cite>.</p>
<p>Raises <a class="reference internal" href="errors.html#pymongo.errors.InvalidName" title="pymongo.errors.InvalidName"><code class="xref py py-class docutils literal"><span class="pre">InvalidName</span></code></a> if an invalid
collection name is used.</p>
</dd></dl>
<dl class="attribute">
<dt id="pymongo.collection.Collection.full_name">
<code class="descname">full_name</code><a class="headerlink" href="#pymongo.collection.Collection.full_name" title="Permalink to this definition">¶</a></dt>
<dd><p>The full name of this <a class="reference internal" href="#pymongo.collection.Collection" title="pymongo.collection.Collection"><code class="xref py py-class docutils literal"><span class="pre">Collection</span></code></a>.</p>
<p>The full name is of the form <cite>database_name.collection_name</cite>.</p>
</dd></dl>
<dl class="attribute">
<dt id="pymongo.collection.Collection.name">
<code class="descname">name</code><a class="headerlink" href="#pymongo.collection.Collection.name" title="Permalink to this definition">¶</a></dt>
<dd><p>The name of this <a class="reference internal" href="#pymongo.collection.Collection" title="pymongo.collection.Collection"><code class="xref py py-class docutils literal"><span class="pre">Collection</span></code></a>.</p>
</dd></dl>
<dl class="attribute">
<dt id="pymongo.collection.Collection.database">
<code class="descname">database</code><a class="headerlink" href="#pymongo.collection.Collection.database" title="Permalink to this definition">¶</a></dt>
<dd><p>The <a class="reference internal" href="database.html#pymongo.database.Database" title="pymongo.database.Database"><code class="xref py py-class docutils literal"><span class="pre">Database</span></code></a> that this
<a class="reference internal" href="#pymongo.collection.Collection" title="pymongo.collection.Collection"><code class="xref py py-class docutils literal"><span class="pre">Collection</span></code></a> is a part of.</p>
</dd></dl>
<dl class="attribute">
<dt id="pymongo.collection.Collection.codec_options">
<code class="descname">codec_options</code><a class="headerlink" href="#pymongo.collection.Collection.codec_options" title="Permalink to this definition">¶</a></dt>
<dd><p>Read only access to the <a class="reference internal" href="../bson/codec_options.html#bson.codec_options.CodecOptions" title="bson.codec_options.CodecOptions"><code class="xref py py-class docutils literal"><span class="pre">CodecOptions</span></code></a>
of this instance.</p>
</dd></dl>
<dl class="attribute">
<dt id="pymongo.collection.Collection.read_preference">
<code class="descname">read_preference</code><a class="headerlink" href="#pymongo.collection.Collection.read_preference" title="Permalink to this definition">¶</a></dt>
<dd><p>Read only access to the read preference of this instance.</p>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.0: </span>The <a class="reference internal" href="#pymongo.collection.Collection.read_preference" title="pymongo.collection.Collection.read_preference"><code class="xref py py-attr docutils literal"><span class="pre">read_preference</span></code></a> attribute is now read only.</p>
</div>
</dd></dl>
<dl class="attribute">
<dt id="pymongo.collection.Collection.write_concern">
<code class="descname">write_concern</code><a class="headerlink" href="#pymongo.collection.Collection.write_concern" title="Permalink to this definition">¶</a></dt>
<dd><p>Read only access to the <a class="reference internal" href="write_concern.html#pymongo.write_concern.WriteConcern" title="pymongo.write_concern.WriteConcern"><code class="xref py py-class docutils literal"><span class="pre">WriteConcern</span></code></a>
of this instance.</p>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.0: </span>The <a class="reference internal" href="#pymongo.collection.Collection.write_concern" title="pymongo.collection.Collection.write_concern"><code class="xref py py-attr docutils literal"><span class="pre">write_concern</span></code></a> attribute is now read only.</p>
</div>
</dd></dl>
<dl class="method">
<dt id="pymongo.collection.Collection.with_options">
<code class="descname">with_options</code><span class="sig-paren">(</span><em>codec_options=None</em>, <em>read_preference=None</em>, <em>write_concern=None</em>, <em>read_concern=None</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.with_options" title="Permalink to this definition">¶</a></dt>
<dd><p>Get a clone of this collection changing the specified settings.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">coll1</span><span class="o">.</span><span class="n">read_preference</span>
<span class="go">Primary()</span>
<span class="gp">>>> </span><span class="kn">from</span> <span class="nn">pymongo</span> <span class="k">import</span> <span class="n">ReadPreference</span>
<span class="gp">>>> </span><span class="n">coll2</span> <span class="o">=</span> <span class="n">coll1</span><span class="o">.</span><span class="n">with_options</span><span class="p">(</span><span class="n">read_preference</span><span class="o">=</span><span class="n">ReadPreference</span><span class="o">.</span><span class="n">SECONDARY</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">coll1</span><span class="o">.</span><span class="n">read_preference</span>
<span class="go">Primary()</span>
<span class="gp">>>> </span><span class="n">coll2</span><span class="o">.</span><span class="n">read_preference</span>
<span class="go">Secondary(tag_sets=None)</span>
</pre></div>
</div>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><cite>codec_options</cite> (optional): An instance of
<a class="reference internal" href="../bson/codec_options.html#bson.codec_options.CodecOptions" title="bson.codec_options.CodecOptions"><code class="xref py py-class docutils literal"><span class="pre">CodecOptions</span></code></a>. If <code class="docutils literal"><span class="pre">None</span></code> (the
default) the <a class="reference internal" href="#pymongo.collection.Collection.codec_options" title="pymongo.collection.Collection.codec_options"><code class="xref py py-attr docutils literal"><span class="pre">codec_options</span></code></a> of this <a class="reference internal" href="#pymongo.collection.Collection" title="pymongo.collection.Collection"><code class="xref py py-class docutils literal"><span class="pre">Collection</span></code></a>
is used.</li>
<li><cite>read_preference</cite> (optional): The read preference to use. If
<code class="docutils literal"><span class="pre">None</span></code> (the default) the <a class="reference internal" href="#pymongo.collection.Collection.read_preference" title="pymongo.collection.Collection.read_preference"><code class="xref py py-attr docutils literal"><span class="pre">read_preference</span></code></a> of this
<a class="reference internal" href="#pymongo.collection.Collection" title="pymongo.collection.Collection"><code class="xref py py-class docutils literal"><span class="pre">Collection</span></code></a> is used. See <a class="reference internal" href="read_preferences.html#module-pymongo.read_preferences" title="pymongo.read_preferences: Utilities for choosing which member of a replica set to read from."><code class="xref py py-mod docutils literal"><span class="pre">read_preferences</span></code></a>
for options.</li>
<li><cite>write_concern</cite> (optional): An instance of
<a class="reference internal" href="write_concern.html#pymongo.write_concern.WriteConcern" title="pymongo.write_concern.WriteConcern"><code class="xref py py-class docutils literal"><span class="pre">WriteConcern</span></code></a>. If <code class="docutils literal"><span class="pre">None</span></code> (the
default) the <a class="reference internal" href="#pymongo.collection.Collection.write_concern" title="pymongo.collection.Collection.write_concern"><code class="xref py py-attr docutils literal"><span class="pre">write_concern</span></code></a> of this <a class="reference internal" href="#pymongo.collection.Collection" title="pymongo.collection.Collection"><code class="xref py py-class docutils literal"><span class="pre">Collection</span></code></a>
is used.</li>
<li><cite>read_concern</cite> (optional): An instance of
<a class="reference internal" href="read_concern.html#pymongo.read_concern.ReadConcern" title="pymongo.read_concern.ReadConcern"><code class="xref py py-class docutils literal"><span class="pre">ReadConcern</span></code></a>. If <code class="docutils literal"><span class="pre">None</span></code> (the
default) the <code class="xref py py-attr docutils literal"><span class="pre">read_concern</span></code> of this <a class="reference internal" href="#pymongo.collection.Collection" title="pymongo.collection.Collection"><code class="xref py py-class docutils literal"><span class="pre">Collection</span></code></a>
is used.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="method">
<dt id="pymongo.collection.Collection.bulk_write">
<code class="descname">bulk_write</code><span class="sig-paren">(</span><em>requests</em>, <em>ordered=True</em>, <em>bypass_document_validation=False</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.bulk_write" title="Permalink to this definition">¶</a></dt>
<dd><p>Send a batch of write operations to the server.</p>
<p>Requests are passed as a list of write operation instances (
<a class="reference internal" href="operations.html#pymongo.operations.InsertOne" title="pymongo.operations.InsertOne"><code class="xref py py-class docutils literal"><span class="pre">InsertOne</span></code></a>,
<a class="reference internal" href="operations.html#pymongo.operations.UpdateOne" title="pymongo.operations.UpdateOne"><code class="xref py py-class docutils literal"><span class="pre">UpdateOne</span></code></a>,
<a class="reference internal" href="operations.html#pymongo.operations.UpdateMany" title="pymongo.operations.UpdateMany"><code class="xref py py-class docutils literal"><span class="pre">UpdateMany</span></code></a>,
<a class="reference internal" href="operations.html#pymongo.operations.ReplaceOne" title="pymongo.operations.ReplaceOne"><code class="xref py py-class docutils literal"><span class="pre">ReplaceOne</span></code></a>,
<a class="reference internal" href="operations.html#pymongo.operations.DeleteOne" title="pymongo.operations.DeleteOne"><code class="xref py py-class docutils literal"><span class="pre">DeleteOne</span></code></a>, or
<a class="reference internal" href="operations.html#pymongo.operations.DeleteMany" title="pymongo.operations.DeleteMany"><code class="xref py py-class docutils literal"><span class="pre">DeleteMany</span></code></a>).</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">for</span> <span class="n">doc</span> <span class="ow">in</span> <span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">find</span><span class="p">({}):</span>
<span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">doc</span><span class="p">)</span>
<span class="gp">...</span>
<span class="go">{u'x': 1, u'_id': ObjectId('54f62e60fba5226811f634ef')}</span>
<span class="go">{u'x': 1, u'_id': ObjectId('54f62e60fba5226811f634f0')}</span>
<span class="gp">>>> </span><span class="c1"># DeleteMany, UpdateOne, and UpdateMany are also available.</span>
<span class="gp">...</span>
<span class="gp">>>> </span><span class="kn">from</span> <span class="nn">pymongo</span> <span class="k">import</span> <span class="n">InsertOne</span><span class="p">,</span> <span class="n">DeleteOne</span><span class="p">,</span> <span class="n">ReplaceOne</span>
<span class="gp">>>> </span><span class="n">requests</span> <span class="o">=</span> <span class="p">[</span><span class="n">InsertOne</span><span class="p">({</span><span class="s1">'y'</span><span class="p">:</span> <span class="mi">1</span><span class="p">}),</span> <span class="n">DeleteOne</span><span class="p">({</span><span class="s1">'x'</span><span class="p">:</span> <span class="mi">1</span><span class="p">}),</span>
<span class="gp">... </span> <span class="n">ReplaceOne</span><span class="p">({</span><span class="s1">'w'</span><span class="p">:</span> <span class="mi">1</span><span class="p">},</span> <span class="p">{</span><span class="s1">'z'</span><span class="p">:</span> <span class="mi">1</span><span class="p">},</span> <span class="n">upsert</span><span class="o">=</span><span class="kc">True</span><span class="p">)]</span>
<span class="gp">>>> </span><span class="n">result</span> <span class="o">=</span> <span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">bulk_write</span><span class="p">(</span><span class="n">requests</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">result</span><span class="o">.</span><span class="n">inserted_count</span>
<span class="go">1</span>
<span class="gp">>>> </span><span class="n">result</span><span class="o">.</span><span class="n">deleted_count</span>
<span class="go">1</span>
<span class="gp">>>> </span><span class="n">result</span><span class="o">.</span><span class="n">modified_count</span>
<span class="go">0</span>
<span class="gp">>>> </span><span class="n">result</span><span class="o">.</span><span class="n">upserted_ids</span>
<span class="go">{2: ObjectId('54f62ee28891e756a6e1abd5')}</span>
<span class="gp">>>> </span><span class="k">for</span> <span class="n">doc</span> <span class="ow">in</span> <span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">find</span><span class="p">({}):</span>
<span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">doc</span><span class="p">)</span>
<span class="gp">...</span>
<span class="go">{u'x': 1, u'_id': ObjectId('54f62e60fba5226811f634f0')}</span>
<span class="go">{u'y': 1, u'_id': ObjectId('54f62ee2fba5226811f634f1')}</span>
<span class="go">{u'z': 1, u'_id': ObjectId('54f62ee28891e756a6e1abd5')}</span>
</pre></div>
</div>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><cite>requests</cite>: A list of write operations (see examples above).</li>
<li><cite>ordered</cite> (optional): If <code class="docutils literal"><span class="pre">True</span></code> (the default) requests will be
performed on the server serially, in the order provided. If an error
occurs all remaining operations are aborted. If <code class="docutils literal"><span class="pre">False</span></code> requests
will be performed on the server in arbitrary order, possibly in
parallel, and all operations will be attempted.</li>
<li><cite>bypass_document_validation</cite>: (optional) If <code class="docutils literal"><span class="pre">True</span></code>, allows the
write to opt-out of document level validation. Default is
<code class="docutils literal"><span class="pre">False</span></code>.</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first last">An instance of <a class="reference internal" href="results.html#pymongo.results.BulkWriteResult" title="pymongo.results.BulkWriteResult"><code class="xref py py-class docutils literal"><span class="pre">BulkWriteResult</span></code></a>.</p>
</td>
</tr>
</tbody>
</table>
<div class="admonition seealso">
<p class="first admonition-title">See also</p>
<p class="last"><a class="reference internal" href="../../faq.html#writes-and-ids"><span class="std std-ref">Why does PyMongo add an _id field to all of my documents?</span></a></p>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last"><cite>bypass_document_validation</cite> requires server version
<strong>>= 3.2</strong></p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.2: </span>Added bypass_document_validation support</p>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 3.0.</span></p>
</div>
</dd></dl>
<dl class="method">
<dt id="pymongo.collection.Collection.insert_one">
<code class="descname">insert_one</code><span class="sig-paren">(</span><em>document</em>, <em>bypass_document_validation=False</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.insert_one" title="Permalink to this definition">¶</a></dt>
<dd><p>Insert a single document.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">count</span><span class="p">({</span><span class="s1">'x'</span><span class="p">:</span> <span class="mi">1</span><span class="p">})</span>
<span class="go">0</span>
<span class="gp">>>> </span><span class="n">result</span> <span class="o">=</span> <span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">insert_one</span><span class="p">({</span><span class="s1">'x'</span><span class="p">:</span> <span class="mi">1</span><span class="p">})</span>
<span class="gp">>>> </span><span class="n">result</span><span class="o">.</span><span class="n">inserted_id</span>
<span class="go">ObjectId('54f112defba522406c9cc208')</span>
<span class="gp">>>> </span><span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">find_one</span><span class="p">({</span><span class="s1">'x'</span><span class="p">:</span> <span class="mi">1</span><span class="p">})</span>
<span class="go">{u'x': 1, u'_id': ObjectId('54f112defba522406c9cc208')}</span>
</pre></div>
</div>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><cite>document</cite>: The document to insert. Must be a mutable mapping
type. If the document does not have an _id field one will be
added automatically.</li>
<li><cite>bypass_document_validation</cite>: (optional) If <code class="docutils literal"><span class="pre">True</span></code>, allows the
write to opt-out of document level validation. Default is
<code class="docutils literal"><span class="pre">False</span></code>.</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><ul class="first last simple">
<li>An instance of <a class="reference internal" href="results.html#pymongo.results.InsertOneResult" title="pymongo.results.InsertOneResult"><code class="xref py py-class docutils literal"><span class="pre">InsertOneResult</span></code></a>.</li>
</ul>
</td>
</tr>
</tbody>
</table>
<div class="admonition seealso">
<p class="first admonition-title">See also</p>
<p class="last"><a class="reference internal" href="../../faq.html#writes-and-ids"><span class="std std-ref">Why does PyMongo add an _id field to all of my documents?</span></a></p>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last"><cite>bypass_document_validation</cite> requires server version
<strong>>= 3.2</strong></p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.2: </span>Added bypass_document_validation support</p>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 3.0.</span></p>
</div>
</dd></dl>
<dl class="method">
<dt id="pymongo.collection.Collection.insert_many">
<code class="descname">insert_many</code><span class="sig-paren">(</span><em>documents</em>, <em>ordered=True</em>, <em>bypass_document_validation=False</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.insert_many" title="Permalink to this definition">¶</a></dt>
<dd><p>Insert an iterable of documents.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">count</span><span class="p">()</span>
<span class="go">0</span>
<span class="gp">>>> </span><span class="n">result</span> <span class="o">=</span> <span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">insert_many</span><span class="p">([{</span><span class="s1">'x'</span><span class="p">:</span> <span class="n">i</span><span class="p">}</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">2</span><span class="p">)])</span>
<span class="gp">>>> </span><span class="n">result</span><span class="o">.</span><span class="n">inserted_ids</span>
<span class="go">[ObjectId('54f113fffba522406c9cc20e'), ObjectId('54f113fffba522406c9cc20f')]</span>
<span class="gp">>>> </span><span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">count</span><span class="p">()</span>
<span class="go">2</span>
</pre></div>
</div>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><cite>documents</cite>: A iterable of documents to insert.</li>
<li><cite>ordered</cite> (optional): If <code class="docutils literal"><span class="pre">True</span></code> (the default) documents will be
inserted on the server serially, in the order provided. If an error
occurs all remaining inserts are aborted. If <code class="docutils literal"><span class="pre">False</span></code>, documents
will be inserted on the server in arbitrary order, possibly in
parallel, and all document inserts will be attempted.</li>
<li><cite>bypass_document_validation</cite>: (optional) If <code class="docutils literal"><span class="pre">True</span></code>, allows the
write to opt-out of document level validation. Default is
<code class="docutils literal"><span class="pre">False</span></code>.</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first last">An instance of <a class="reference internal" href="results.html#pymongo.results.InsertManyResult" title="pymongo.results.InsertManyResult"><code class="xref py py-class docutils literal"><span class="pre">InsertManyResult</span></code></a>.</p>
</td>
</tr>
</tbody>
</table>
<div class="admonition seealso">
<p class="first admonition-title">See also</p>
<p class="last"><a class="reference internal" href="../../faq.html#writes-and-ids"><span class="std std-ref">Why does PyMongo add an _id field to all of my documents?</span></a></p>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last"><cite>bypass_document_validation</cite> requires server version
<strong>>= 3.2</strong></p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.2: </span>Added bypass_document_validation support</p>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 3.0.</span></p>
</div>
</dd></dl>
<dl class="method">
<dt id="pymongo.collection.Collection.replace_one">
<code class="descname">replace_one</code><span class="sig-paren">(</span><em>filter</em>, <em>replacement</em>, <em>upsert=False</em>, <em>bypass_document_validation=False</em>, <em>collation=None</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.replace_one" title="Permalink to this definition">¶</a></dt>
<dd><p>Replace a single document matching the filter.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">for</span> <span class="n">doc</span> <span class="ow">in</span> <span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">find</span><span class="p">({}):</span>
<span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">doc</span><span class="p">)</span>
<span class="gp">...</span>
<span class="go">{u'x': 1, u'_id': ObjectId('54f4c5befba5220aa4d6dee7')}</span>
<span class="gp">>>> </span><span class="n">result</span> <span class="o">=</span> <span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">replace_one</span><span class="p">({</span><span class="s1">'x'</span><span class="p">:</span> <span class="mi">1</span><span class="p">},</span> <span class="p">{</span><span class="s1">'y'</span><span class="p">:</span> <span class="mi">1</span><span class="p">})</span>
<span class="gp">>>> </span><span class="n">result</span><span class="o">.</span><span class="n">matched_count</span>
<span class="go">1</span>
<span class="gp">>>> </span><span class="n">result</span><span class="o">.</span><span class="n">modified_count</span>
<span class="go">1</span>
<span class="gp">>>> </span><span class="k">for</span> <span class="n">doc</span> <span class="ow">in</span> <span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">find</span><span class="p">({}):</span>
<span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">doc</span><span class="p">)</span>
<span class="gp">...</span>
<span class="go">{u'y': 1, u'_id': ObjectId('54f4c5befba5220aa4d6dee7')}</span>
</pre></div>
</div>
<p>The <em>upsert</em> option can be used to insert a new document if a matching
document does not exist.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">result</span> <span class="o">=</span> <span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">replace_one</span><span class="p">({</span><span class="s1">'x'</span><span class="p">:</span> <span class="mi">1</span><span class="p">},</span> <span class="p">{</span><span class="s1">'x'</span><span class="p">:</span> <span class="mi">1</span><span class="p">},</span> <span class="kc">True</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">result</span><span class="o">.</span><span class="n">matched_count</span>
<span class="go">0</span>
<span class="gp">>>> </span><span class="n">result</span><span class="o">.</span><span class="n">modified_count</span>
<span class="go">0</span>
<span class="gp">>>> </span><span class="n">result</span><span class="o">.</span><span class="n">upserted_id</span>
<span class="go">ObjectId('54f11e5c8891e756a6e1abd4')</span>
<span class="gp">>>> </span><span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">find_one</span><span class="p">({</span><span class="s1">'x'</span><span class="p">:</span> <span class="mi">1</span><span class="p">})</span>
<span class="go">{u'x': 1, u'_id': ObjectId('54f11e5c8891e756a6e1abd4')}</span>
</pre></div>
</div>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><cite>filter</cite>: A query that matches the document to replace.</li>
<li><cite>replacement</cite>: The new document.</li>
<li><cite>upsert</cite> (optional): If <code class="docutils literal"><span class="pre">True</span></code>, perform an insert if no documents
match the filter.</li>
<li><cite>bypass_document_validation</cite>: (optional) If <code class="docutils literal"><span class="pre">True</span></code>, allows the
write to opt-out of document level validation. Default is
<code class="docutils literal"><span class="pre">False</span></code>.</li>
<li><cite>collation</cite> (optional): An instance of
<a class="reference internal" href="collation.html#pymongo.collation.Collation" title="pymongo.collation.Collation"><code class="xref py py-class docutils literal"><span class="pre">Collation</span></code></a>. This option is only supported
on MongoDB 3.4 and above.</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><ul class="first last simple">
<li>An instance of <a class="reference internal" href="results.html#pymongo.results.UpdateResult" title="pymongo.results.UpdateResult"><code class="xref py py-class docutils literal"><span class="pre">UpdateResult</span></code></a>.</li>
</ul>
</td>
</tr>
</tbody>
</table>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last"><cite>bypass_document_validation</cite> requires server version
<strong>>= 3.2</strong></p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.4: </span>Added the <cite>collation</cite> option.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.2: </span>Added bypass_document_validation support</p>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 3.0.</span></p>
</div>
</dd></dl>
<dl class="method">
<dt id="pymongo.collection.Collection.update_one">
<code class="descname">update_one</code><span class="sig-paren">(</span><em>filter</em>, <em>update</em>, <em>upsert=False</em>, <em>bypass_document_validation=False</em>, <em>collation=None</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.update_one" title="Permalink to this definition">¶</a></dt>
<dd><p>Update a single document matching the filter.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">for</span> <span class="n">doc</span> <span class="ow">in</span> <span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">find</span><span class="p">():</span>
<span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">doc</span><span class="p">)</span>
<span class="gp">...</span>
<span class="go">{u'x': 1, u'_id': 0}</span>
<span class="go">{u'x': 1, u'_id': 1}</span>
<span class="go">{u'x': 1, u'_id': 2}</span>
<span class="gp">>>> </span><span class="n">result</span> <span class="o">=</span> <span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">update_one</span><span class="p">({</span><span class="s1">'x'</span><span class="p">:</span> <span class="mi">1</span><span class="p">},</span> <span class="p">{</span><span class="s1">'$inc'</span><span class="p">:</span> <span class="p">{</span><span class="s1">'x'</span><span class="p">:</span> <span class="mi">3</span><span class="p">}})</span>
<span class="gp">>>> </span><span class="n">result</span><span class="o">.</span><span class="n">matched_count</span>
<span class="go">1</span>
<span class="gp">>>> </span><span class="n">result</span><span class="o">.</span><span class="n">modified_count</span>
<span class="go">1</span>
<span class="gp">>>> </span><span class="k">for</span> <span class="n">doc</span> <span class="ow">in</span> <span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">find</span><span class="p">():</span>
<span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">doc</span><span class="p">)</span>
<span class="gp">...</span>
<span class="go">{u'x': 4, u'_id': 0}</span>
<span class="go">{u'x': 1, u'_id': 1}</span>
<span class="go">{u'x': 1, u'_id': 2}</span>
</pre></div>
</div>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><cite>filter</cite>: A query that matches the document to update.</li>
<li><cite>update</cite>: The modifications to apply.</li>
<li><cite>upsert</cite> (optional): If <code class="docutils literal"><span class="pre">True</span></code>, perform an insert if no documents
match the filter.</li>
<li><cite>bypass_document_validation</cite>: (optional) If <code class="docutils literal"><span class="pre">True</span></code>, allows the
write to opt-out of document level validation. Default is
<code class="docutils literal"><span class="pre">False</span></code>.</li>
<li><cite>collation</cite> (optional): An instance of
<a class="reference internal" href="collation.html#pymongo.collation.Collation" title="pymongo.collation.Collation"><code class="xref py py-class docutils literal"><span class="pre">Collation</span></code></a>. This option is only supported
on MongoDB 3.4 and above.</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><ul class="first last simple">
<li>An instance of <a class="reference internal" href="results.html#pymongo.results.UpdateResult" title="pymongo.results.UpdateResult"><code class="xref py py-class docutils literal"><span class="pre">UpdateResult</span></code></a>.</li>
</ul>
</td>
</tr>
</tbody>
</table>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last"><cite>bypass_document_validation</cite> requires server version
<strong>>= 3.2</strong></p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.4: </span>Added the <cite>collation</cite> option.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.2: </span>Added bypass_document_validation support</p>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 3.0.</span></p>
</div>
</dd></dl>
<dl class="method">
<dt id="pymongo.collection.Collection.update_many">
<code class="descname">update_many</code><span class="sig-paren">(</span><em>filter</em>, <em>update</em>, <em>upsert=False</em>, <em>bypass_document_validation=False</em>, <em>collation=None</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.update_many" title="Permalink to this definition">¶</a></dt>
<dd><p>Update one or more documents that match the filter.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">for</span> <span class="n">doc</span> <span class="ow">in</span> <span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">find</span><span class="p">():</span>
<span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">doc</span><span class="p">)</span>
<span class="gp">...</span>
<span class="go">{u'x': 1, u'_id': 0}</span>
<span class="go">{u'x': 1, u'_id': 1}</span>
<span class="go">{u'x': 1, u'_id': 2}</span>
<span class="gp">>>> </span><span class="n">result</span> <span class="o">=</span> <span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">update_many</span><span class="p">({</span><span class="s1">'x'</span><span class="p">:</span> <span class="mi">1</span><span class="p">},</span> <span class="p">{</span><span class="s1">'$inc'</span><span class="p">:</span> <span class="p">{</span><span class="s1">'x'</span><span class="p">:</span> <span class="mi">3</span><span class="p">}})</span>
<span class="gp">>>> </span><span class="n">result</span><span class="o">.</span><span class="n">matched_count</span>
<span class="go">3</span>
<span class="gp">>>> </span><span class="n">result</span><span class="o">.</span><span class="n">modified_count</span>
<span class="go">3</span>
<span class="gp">>>> </span><span class="k">for</span> <span class="n">doc</span> <span class="ow">in</span> <span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">find</span><span class="p">():</span>
<span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">doc</span><span class="p">)</span>
<span class="gp">...</span>
<span class="go">{u'x': 4, u'_id': 0}</span>
<span class="go">{u'x': 4, u'_id': 1}</span>
<span class="go">{u'x': 4, u'_id': 2}</span>
</pre></div>
</div>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><cite>filter</cite>: A query that matches the documents to update.</li>
<li><cite>update</cite>: The modifications to apply.</li>
<li><cite>upsert</cite> (optional): If <code class="docutils literal"><span class="pre">True</span></code>, perform an insert if no documents
match the filter.</li>
<li><cite>bypass_document_validation</cite> (optional): If <code class="docutils literal"><span class="pre">True</span></code>, allows the
write to opt-out of document level validation. Default is
<code class="docutils literal"><span class="pre">False</span></code>.</li>
<li><cite>collation</cite> (optional): An instance of
<a class="reference internal" href="collation.html#pymongo.collation.Collation" title="pymongo.collation.Collation"><code class="xref py py-class docutils literal"><span class="pre">Collation</span></code></a>. This option is only supported
on MongoDB 3.4 and above.</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><ul class="first last simple">
<li>An instance of <a class="reference internal" href="results.html#pymongo.results.UpdateResult" title="pymongo.results.UpdateResult"><code class="xref py py-class docutils literal"><span class="pre">UpdateResult</span></code></a>.</li>
</ul>
</td>
</tr>
</tbody>
</table>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last"><cite>bypass_document_validation</cite> requires server version
<strong>>= 3.2</strong></p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.4: </span>Added the <cite>collation</cite> option.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.2: </span>Added bypass_document_validation support</p>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 3.0.</span></p>
</div>
</dd></dl>
<dl class="method">
<dt id="pymongo.collection.Collection.delete_one">
<code class="descname">delete_one</code><span class="sig-paren">(</span><em>filter</em>, <em>collation=None</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.delete_one" title="Permalink to this definition">¶</a></dt>
<dd><p>Delete a single document matching the filter.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">count</span><span class="p">({</span><span class="s1">'x'</span><span class="p">:</span> <span class="mi">1</span><span class="p">})</span>
<span class="go">3</span>
<span class="gp">>>> </span><span class="n">result</span> <span class="o">=</span> <span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">delete_one</span><span class="p">({</span><span class="s1">'x'</span><span class="p">:</span> <span class="mi">1</span><span class="p">})</span>
<span class="gp">>>> </span><span class="n">result</span><span class="o">.</span><span class="n">deleted_count</span>
<span class="go">1</span>
<span class="gp">>>> </span><span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">count</span><span class="p">({</span><span class="s1">'x'</span><span class="p">:</span> <span class="mi">1</span><span class="p">})</span>
<span class="go">2</span>
</pre></div>
</div>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><cite>filter</cite>: A query that matches the document to delete.</li>
<li><cite>collation</cite> (optional): An instance of
<a class="reference internal" href="collation.html#pymongo.collation.Collation" title="pymongo.collation.Collation"><code class="xref py py-class docutils literal"><span class="pre">Collation</span></code></a>. This option is only supported
on MongoDB 3.4 and above.</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><ul class="first last simple">
<li>An instance of <a class="reference internal" href="results.html#pymongo.results.DeleteResult" title="pymongo.results.DeleteResult"><code class="xref py py-class docutils literal"><span class="pre">DeleteResult</span></code></a>.</li>
</ul>
</td>
</tr>
</tbody>
</table>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.4: </span>Added the <cite>collation</cite> option.</p>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 3.0.</span></p>
</div>
</dd></dl>
<dl class="method">
<dt id="pymongo.collection.Collection.delete_many">
<code class="descname">delete_many</code><span class="sig-paren">(</span><em>filter</em>, <em>collation=None</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.delete_many" title="Permalink to this definition">¶</a></dt>
<dd><p>Delete one or more documents matching the filter.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">count</span><span class="p">({</span><span class="s1">'x'</span><span class="p">:</span> <span class="mi">1</span><span class="p">})</span>
<span class="go">3</span>
<span class="gp">>>> </span><span class="n">result</span> <span class="o">=</span> <span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">delete_many</span><span class="p">({</span><span class="s1">'x'</span><span class="p">:</span> <span class="mi">1</span><span class="p">})</span>
<span class="gp">>>> </span><span class="n">result</span><span class="o">.</span><span class="n">deleted_count</span>
<span class="go">3</span>
<span class="gp">>>> </span><span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">count</span><span class="p">({</span><span class="s1">'x'</span><span class="p">:</span> <span class="mi">1</span><span class="p">})</span>
<span class="go">0</span>
</pre></div>
</div>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><cite>filter</cite>: A query that matches the documents to delete.</li>
<li><cite>collation</cite> (optional): An instance of
<a class="reference internal" href="collation.html#pymongo.collation.Collation" title="pymongo.collation.Collation"><code class="xref py py-class docutils literal"><span class="pre">Collation</span></code></a>. This option is only supported
on MongoDB 3.4 and above.</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><ul class="first last simple">
<li>An instance of <a class="reference internal" href="results.html#pymongo.results.DeleteResult" title="pymongo.results.DeleteResult"><code class="xref py py-class docutils literal"><span class="pre">DeleteResult</span></code></a>.</li>
</ul>
</td>
</tr>
</tbody>
</table>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.4: </span>Added the <cite>collation</cite> option.</p>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 3.0.</span></p>
</div>
</dd></dl>
<dl class="method">
<dt id="pymongo.collection.Collection.aggregate">
<code class="descname">aggregate</code><span class="sig-paren">(</span><em>pipeline</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.aggregate" title="Permalink to this definition">¶</a></dt>
<dd><p>Perform an aggregation using the aggregation framework on this
collection.</p>
<p>All optional aggregate parameters should be passed as keyword arguments
to this method. Valid options include, but are not limited to:</p>
<blockquote>
<div><ul class="simple">
<li><cite>allowDiskUse</cite> (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.</li>
<li><cite>maxTimeMS</cite> (int): The maximum amount of time to allow the operation
to run in milliseconds.</li>
<li><cite>batchSize</cite> (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, or <cite>useCursor</cite> is
<code class="docutils literal"><span class="pre">False</span></code>.</li>
<li><cite>useCursor</cite> (bool): Requests that the <cite>server</cite> provide results
using a cursor, if possible. Ignored if the connected mongod or
mongos does not support returning aggregate results using a cursor.
The default is <code class="docutils literal"><span class="pre">True</span></code>. Set this to <code class="docutils literal"><span class="pre">False</span></code> when upgrading a 2.4
or older sharded cluster to 2.6 or newer (see the warning below).</li>
<li><cite>collation</cite> (optional): An instance of
<a class="reference internal" href="collation.html#pymongo.collation.Collation" title="pymongo.collation.Collation"><code class="xref py py-class docutils literal"><span class="pre">Collation</span></code></a>. This option is only supported
on MongoDB 3.4 and above.</li>
</ul>
</div></blockquote>
<p>The <a class="reference internal" href="#pymongo.collection.Collection.aggregate" title="pymongo.collection.Collection.aggregate"><code class="xref py py-meth docutils literal"><span class="pre">aggregate()</span></code></a> method obeys the <a class="reference internal" href="#pymongo.collection.Collection.read_preference" title="pymongo.collection.Collection.read_preference"><code class="xref py py-attr docutils literal"><span class="pre">read_preference</span></code></a> of this
<a class="reference internal" href="#pymongo.collection.Collection" title="pymongo.collection.Collection"><code class="xref py py-class docutils literal"><span class="pre">Collection</span></code></a>. Please note that using the <code class="docutils literal"><span class="pre">$out</span></code> pipeline stage
requires a read preference of
<a class="reference internal" href="read_preferences.html#pymongo.read_preferences.ReadPreference.PRIMARY" title="pymongo.read_preferences.ReadPreference.PRIMARY"><code class="xref py py-attr docutils literal"><span class="pre">PRIMARY</span></code></a> (the default).
The server will raise an error if the <code class="docutils literal"><span class="pre">$out</span></code> pipeline stage is used
with any other read preference.</p>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">When upgrading a 2.4 or older sharded cluster to 2.6 or
newer the <cite>useCursor</cite> option <strong>must</strong> be set to <code class="docutils literal"><span class="pre">False</span></code>
until all shards have been upgraded to 2.6 or newer.</p>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">This method does not support the ‘explain’ option. Please
use <a class="reference internal" href="database.html#pymongo.database.Database.command" title="pymongo.database.Database.command"><code class="xref py py-meth docutils literal"><span class="pre">command()</span></code></a> instead. An
example is included in the <a class="reference internal" href="../../examples/aggregation.html#aggregate-examples"><span class="std std-ref">Aggregation Framework</span></a> documentation.</p>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The <a class="reference internal" href="#pymongo.collection.Collection.write_concern" title="pymongo.collection.Collection.write_concern"><code class="xref py py-attr docutils literal"><span class="pre">write_concern</span></code></a> of
this collection is automatically applied to this operation when using
MongoDB >= 3.4.</p>
</div>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><cite>pipeline</cite>: a list of aggregation pipeline stages</li>
<li><cite>**kwargs</cite> (optional): See list of options above.</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first last">A <a class="reference internal" href="command_cursor.html#pymongo.command_cursor.CommandCursor" title="pymongo.command_cursor.CommandCursor"><code class="xref py py-class docutils literal"><span class="pre">CommandCursor</span></code></a> over the result
set.</p>
</td>
</tr>
</tbody>
</table>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.4: </span>Apply this collection’s write concern automatically to this operation
when connected to MongoDB >= 3.4. Support the <cite>collation</cite> option.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.0: </span>The <a class="reference internal" href="#pymongo.collection.Collection.aggregate" title="pymongo.collection.Collection.aggregate"><code class="xref py py-meth docutils literal"><span class="pre">aggregate()</span></code></a> method always returns a CommandCursor. The
pipeline argument must be a list.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 2.7: </span>When the cursor option is used, return
<a class="reference internal" href="command_cursor.html#pymongo.command_cursor.CommandCursor" title="pymongo.command_cursor.CommandCursor"><code class="xref py py-class docutils literal"><span class="pre">CommandCursor</span></code></a> instead of
<a class="reference internal" href="cursor.html#pymongo.cursor.Cursor" title="pymongo.cursor.Cursor"><code class="xref py py-class docutils literal"><span class="pre">Cursor</span></code></a>.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 2.6: </span>Added cursor support.</p>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.3.</span></p>
</div>
<div class="admonition seealso">
<p class="first admonition-title">See also</p>
<p class="last"><a class="reference internal" href="../../examples/aggregation.html"><span class="doc">Aggregation Examples</span></a></p>
</div>
</dd></dl>
<dl class="method">
<dt id="pymongo.collection.Collection.find">
<code class="descname">find</code><span class="sig-paren">(</span><em>filter=None</em>, <em>projection=None</em>, <em>skip=0</em>, <em>limit=0</em>, <em>no_cursor_timeout=False</em>, <em>cursor_type=CursorType.NON_TAILABLE</em>, <em>sort=None</em>, <em>allow_partial_results=False</em>, <em>oplog_replay=False</em>, <em>modifiers=None</em>, <em>manipulate=True</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.find" title="Permalink to this definition">¶</a></dt>
<dd><p>Query the database.</p>
<p>The <cite>filter</cite> argument is a prototype document that all results
must match. For example:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">find</span><span class="p">({</span><span class="s2">"hello"</span><span class="p">:</span> <span class="s2">"world"</span><span class="p">})</span>
</pre></div>
</div>
<p>only matches documents that have a key “hello” with value
“world”. Matches can have other keys <em>in addition</em> to
“hello”. The <cite>projection</cite> argument is used to specify a subset
of fields that should be included in the result documents. By
limiting results to a certain subset of fields you can cut
down on network traffic and decoding time.</p>
<p>Raises <code class="xref py py-class docutils literal"><span class="pre">TypeError</span></code> if any of the arguments are of
improper type. Returns an instance of
<a class="reference internal" href="cursor.html#pymongo.cursor.Cursor" title="pymongo.cursor.Cursor"><code class="xref py py-class docutils literal"><span class="pre">Cursor</span></code></a> corresponding to this query.</p>
<p>The <a class="reference internal" href="#pymongo.collection.Collection.find" title="pymongo.collection.Collection.find"><code class="xref py py-meth docutils literal"><span class="pre">find()</span></code></a> method obeys the <a class="reference internal" href="#pymongo.collection.Collection.read_preference" title="pymongo.collection.Collection.read_preference"><code class="xref py py-attr docutils literal"><span class="pre">read_preference</span></code></a> of
this <a class="reference internal" href="#pymongo.collection.Collection" title="pymongo.collection.Collection"><code class="xref py py-class docutils literal"><span class="pre">Collection</span></code></a>.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last">
<li><p class="first"><cite>filter</cite> (optional): a SON object specifying elements which
must be present for a document to be included in the
result set</p>
</li>
<li><p class="first"><cite>projection</cite> (optional): a list of field names that should be
returned in the result set or a dict specifying the fields
to include or exclude. If <cite>projection</cite> is a list “_id” will
always be returned. Use a dict to exclude fields from
the result (e.g. projection={‘_id’: False}).</p>
</li>
<li><p class="first"><cite>skip</cite> (optional): the number of documents to omit (from
the start of the result set) when returning the results</p>
</li>
<li><p class="first"><cite>limit</cite> (optional): the maximum number of results to
return</p>
</li>
<li><p class="first"><cite>no_cursor_timeout</cite> (optional): if False (the default), any
returned cursor is closed by the server after 10 minutes of
inactivity. If set to True, the returned cursor will never
time out on the server. Care should be taken to ensure that
cursors with no_cursor_timeout turned on are properly closed.</p>
</li>
<li><p class="first"><cite>cursor_type</cite> (optional): the type of cursor to return. The valid
options are defined by <a class="reference internal" href="cursor.html#pymongo.cursor.CursorType" title="pymongo.cursor.CursorType"><code class="xref py py-class docutils literal"><span class="pre">CursorType</span></code></a>:</p>
<ul class="simple">
<li><a class="reference internal" href="cursor.html#pymongo.cursor.CursorType.NON_TAILABLE" title="pymongo.cursor.CursorType.NON_TAILABLE"><code class="xref py py-attr docutils literal"><span class="pre">NON_TAILABLE</span></code></a> - the result of
this find call will return a standard cursor over the result set.</li>
<li><a class="reference internal" href="cursor.html#pymongo.cursor.CursorType.TAILABLE" title="pymongo.cursor.CursorType.TAILABLE"><code class="xref py py-attr docutils literal"><span class="pre">TAILABLE</span></code></a> - the result of this
find call will be a tailable cursor - tailable cursors are only
for use with capped collections. They are not closed when the
last data is retrieved but are kept open and the cursor location
marks the final document position. If more data is received
iteration of the cursor will continue from the last document
received. For details, see the <a class="reference external" href="http://www.mongodb.org/display/DOCS/Tailable+Cursors">tailable cursor documentation</a>.</li>
<li><a class="reference internal" href="cursor.html#pymongo.cursor.CursorType.TAILABLE_AWAIT" title="pymongo.cursor.CursorType.TAILABLE_AWAIT"><code class="xref py py-attr docutils literal"><span class="pre">TAILABLE_AWAIT</span></code></a> - the result
of this find call will be a tailable cursor with the await flag
set. The server will wait for a few seconds after returning the
full result set so that it can capture and return additional data
added during the query.</li>
<li><a class="reference internal" href="cursor.html#pymongo.cursor.CursorType.EXHAUST" title="pymongo.cursor.CursorType.EXHAUST"><code class="xref py py-attr docutils literal"><span class="pre">EXHAUST</span></code></a> - the result of this
find call will be an exhaust cursor. MongoDB will stream batched
results to the client without waiting for the client to request
each batch, reducing latency. See notes on compatibility below.</li>
</ul>
</li>
<li><p class="first"><cite>sort</cite> (optional): a list of (key, direction) pairs
specifying the sort order for this query. See
<a class="reference internal" href="cursor.html#pymongo.cursor.Cursor.sort" title="pymongo.cursor.Cursor.sort"><code class="xref py py-meth docutils literal"><span class="pre">sort()</span></code></a> for details.</p>
</li>
<li><p class="first"><cite>allow_partial_results</cite> (optional): if True, mongos will return
partial results if some shards are down instead of returning an
error.</p>
</li>
<li><p class="first"><cite>oplog_replay</cite> (optional): If True, set the oplogReplay query
flag.</p>
</li>
<li><p class="first"><cite>modifiers</cite> (optional): A dict specifying the MongoDB <a class="reference external" href="http://docs.mongodb.org/manual/reference/operator/query-modifier/">query
modifiers</a> that should be used for this query. For example:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="n">modifiers</span><span class="o">=</span><span class="p">{</span><span class="s2">"$maxTimeMS"</span><span class="p">:</span> <span class="mi">500</span><span class="p">})</span>
</pre></div>
</div>
</li>
<li><p class="first"><cite>batch_size</cite> (optional): Limits the number of documents returned in
a single batch.</p>
</li>
<li><p class="first"><cite>manipulate</cite> (optional): <strong>DEPRECATED</strong> - If True (the default),
apply any outgoing SON manipulators before returning.</p>
</li>
<li><p class="first"><cite>collation</cite> (optional): An instance of
<a class="reference internal" href="collation.html#pymongo.collation.Collation" title="pymongo.collation.Collation"><code class="xref py py-class docutils literal"><span class="pre">Collation</span></code></a>. This option is only supported
on MongoDB 3.4 and above.</p>
</li>
</ul>
</td>
</tr>
</tbody>
</table>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p>There are a number of caveats to using
<a class="reference internal" href="cursor.html#pymongo.cursor.CursorType.EXHAUST" title="pymongo.cursor.CursorType.EXHAUST"><code class="xref py py-attr docutils literal"><span class="pre">EXHAUST</span></code></a> as cursor_type:</p>
<ul class="last simple">
<li>The <cite>limit</cite> option can not be used with an exhaust cursor.</li>
<li>Exhaust cursors are not supported by mongos and can not be
used with a sharded cluster.</li>
<li>A <a class="reference internal" href="cursor.html#pymongo.cursor.Cursor" title="pymongo.cursor.Cursor"><code class="xref py py-class docutils literal"><span class="pre">Cursor</span></code></a> instance created with the
<a class="reference internal" href="cursor.html#pymongo.cursor.CursorType.EXHAUST" title="pymongo.cursor.CursorType.EXHAUST"><code class="xref py py-attr docutils literal"><span class="pre">EXHAUST</span></code></a> cursor_type requires an
exclusive <code class="xref py py-class docutils literal"><span class="pre">socket</span></code> connection to MongoDB. If the
<a class="reference internal" href="cursor.html#pymongo.cursor.Cursor" title="pymongo.cursor.Cursor"><code class="xref py py-class docutils literal"><span class="pre">Cursor</span></code></a> is discarded without being
completely iterated the underlying <code class="xref py py-class docutils literal"><span class="pre">socket</span></code>
connection will be closed and discarded without being returned to
the connection pool.</li>
</ul>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.4: </span>Support the <cite>collation</cite> option.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.0: </span>Changed the parameter names <cite>spec</cite>, <cite>fields</cite>, <cite>timeout</cite>, and
<cite>partial</cite> to <cite>filter</cite>, <cite>projection</cite>, <cite>no_cursor_timeout</cite>, and
<cite>allow_partial_results</cite> respectively.
Added the <cite>cursor_type</cite>, <cite>oplog_replay</cite>, and <cite>modifiers</cite> options.
Removed the <cite>network_timeout</cite>, <cite>read_preference</cite>, <cite>tag_sets</cite>,
<cite>secondary_acceptable_latency_ms</cite>, <cite>max_scan</cite>, <cite>snapshot</cite>,
<cite>tailable</cite>, <cite>await_data</cite>, <cite>exhaust</cite>, <cite>as_class</cite>, and slave_okay
parameters. Removed <cite>compile_re</cite> option: PyMongo now always
represents BSON regular expressions as <a class="reference internal" href="../bson/regex.html#bson.regex.Regex" title="bson.regex.Regex"><code class="xref py py-class docutils literal"><span class="pre">Regex</span></code></a>
objects. Use <a class="reference internal" href="../bson/regex.html#bson.regex.Regex.try_compile" title="bson.regex.Regex.try_compile"><code class="xref py py-meth docutils literal"><span class="pre">try_compile()</span></code></a> to attempt to
convert from a BSON regular expression to a Python regular
expression object. Soft deprecated the <cite>manipulate</cite> option.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 2.7: </span>Added <cite>compile_re</cite> option. If set to False, PyMongo represented BSON
regular expressions as <a class="reference internal" href="../bson/regex.html#bson.regex.Regex" title="bson.regex.Regex"><code class="xref py py-class docutils literal"><span class="pre">Regex</span></code></a> objects instead of
attempting to compile BSON regular expressions as Python native
regular expressions, thus preventing errors for some incompatible
patterns, see <a class="reference external" href="https://jira.mongodb.org/browse/PYTHON-500">PYTHON-500</a>.</p>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.3: </span>The <cite>tag_sets</cite> and <cite>secondary_acceptable_latency_ms</cite> parameters.</p>
</div>
<div class="admonition-see-general-mongodb-documentation admonition seealso">
<p class="first admonition-title">See also</p>
<p class="admonition-title">See general MongoDB documentation</p>
<p class="last"><a class="reference external" href="http://dochub.mongodb.org/core/find" name="pymongo.collection.Collection.find"><em>find</em></a></p>
</div>
</dd></dl>
<dl class="method">
<dt id="pymongo.collection.Collection.find_one">
<code class="descname">find_one</code><span class="sig-paren">(</span><em>filter=None</em>, <em>*args</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.find_one" title="Permalink to this definition">¶</a></dt>
<dd><p>Get a single document from the database.</p>
<p>All arguments to <a class="reference internal" href="#pymongo.collection.Collection.find" title="pymongo.collection.Collection.find"><code class="xref py py-meth docutils literal"><span class="pre">find()</span></code></a> are also valid arguments for
<a class="reference internal" href="#pymongo.collection.Collection.find_one" title="pymongo.collection.Collection.find_one"><code class="xref py py-meth docutils literal"><span class="pre">find_one()</span></code></a>, although any <cite>limit</cite> argument will be
ignored. Returns a single document, or <code class="docutils literal"><span class="pre">None</span></code> if no matching
document is found.</p>
<p>The <a class="reference internal" href="#pymongo.collection.Collection.find_one" title="pymongo.collection.Collection.find_one"><code class="xref py py-meth docutils literal"><span class="pre">find_one()</span></code></a> method obeys the <a class="reference internal" href="#pymongo.collection.Collection.read_preference" title="pymongo.collection.Collection.read_preference"><code class="xref py py-attr docutils literal"><span class="pre">read_preference</span></code></a> of
this <a class="reference internal" href="#pymongo.collection.Collection" title="pymongo.collection.Collection"><code class="xref py py-class docutils literal"><span class="pre">Collection</span></code></a>.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last">
<li><p class="first"><cite>filter</cite> (optional): a dictionary specifying
the query to be performed OR any other type to be used as
the value for a query for <code class="docutils literal"><span class="pre">"_id"</span></code>.</p>
</li>
<li><p class="first"><cite>*args</cite> (optional): any additional positional arguments
are the same as the arguments to <a class="reference internal" href="#pymongo.collection.Collection.find" title="pymongo.collection.Collection.find"><code class="xref py py-meth docutils literal"><span class="pre">find()</span></code></a>.</p>
</li>
<li><p class="first"><cite>**kwargs</cite> (optional): any additional keyword arguments
are the same as the arguments to <a class="reference internal" href="#pymongo.collection.Collection.find" title="pymongo.collection.Collection.find"><code class="xref py py-meth docutils literal"><span class="pre">find()</span></code></a>.</p>
</li>
<li><p class="first"><cite>max_time_ms</cite> (optional): a value for max_time_ms may be
specified as part of <cite>**kwargs</cite>, e.g.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">find_one</span><span class="p">(</span><span class="n">max_time_ms</span><span class="o">=</span><span class="mi">100</span><span class="p">)</span>
</pre></div>
</div>
</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="method">
<dt id="pymongo.collection.Collection.find_one_and_delete">
<code class="descname">find_one_and_delete</code><span class="sig-paren">(</span><em>filter</em>, <em>projection=None</em>, <em>sort=None</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.find_one_and_delete" title="Permalink to this definition">¶</a></dt>
<dd><p>Finds a single document and deletes it, returning the document.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">count</span><span class="p">({</span><span class="s1">'x'</span><span class="p">:</span> <span class="mi">1</span><span class="p">})</span>
<span class="go">2</span>
<span class="gp">>>> </span><span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">find_one_and_delete</span><span class="p">({</span><span class="s1">'x'</span><span class="p">:</span> <span class="mi">1</span><span class="p">})</span>
<span class="go">{u'x': 1, u'_id': ObjectId('54f4e12bfba5220aa4d6dee8')}</span>
<span class="gp">>>> </span><span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">count</span><span class="p">({</span><span class="s1">'x'</span><span class="p">:</span> <span class="mi">1</span><span class="p">})</span>
<span class="go">1</span>
</pre></div>
</div>
<p>If multiple documents match <em>filter</em>, a <em>sort</em> can be applied.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">for</span> <span class="n">doc</span> <span class="ow">in</span> <span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">find</span><span class="p">({</span><span class="s1">'x'</span><span class="p">:</span> <span class="mi">1</span><span class="p">}):</span>
<span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">doc</span><span class="p">)</span>
<span class="gp">...</span>
<span class="go">{u'x': 1, u'_id': 0}</span>
<span class="go">{u'x': 1, u'_id': 1}</span>
<span class="go">{u'x': 1, u'_id': 2}</span>
<span class="gp">>>> </span><span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">find_one_and_delete</span><span class="p">(</span>
<span class="gp">... </span> <span class="p">{</span><span class="s1">'x'</span><span class="p">:</span> <span class="mi">1</span><span class="p">},</span> <span class="n">sort</span><span class="o">=</span><span class="p">[(</span><span class="s1">'_id'</span><span class="p">,</span> <span class="n">pymongo</span><span class="o">.</span><span class="n">DESCENDING</span><span class="p">)])</span>
<span class="go">{u'x': 1, u'_id': 2}</span>
</pre></div>
</div>
<p>The <em>projection</em> option can be used to limit the fields returned.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">find_one_and_delete</span><span class="p">({</span><span class="s1">'x'</span><span class="p">:</span> <span class="mi">1</span><span class="p">},</span> <span class="n">projection</span><span class="o">=</span><span class="p">{</span><span class="s1">'_id'</span><span class="p">:</span> <span class="kc">False</span><span class="p">})</span>
<span class="go">{u'x': 1}</span>
</pre></div>
</div>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><cite>filter</cite>: A query that matches the document to delete.</li>
<li><cite>projection</cite> (optional): a list of field names that should be
returned in the result document or a mapping specifying the fields
to include or exclude. If <cite>projection</cite> is a list “_id” will
always be returned. Use a mapping to exclude fields from
the result (e.g. projection={‘_id’: False}).</li>
<li><cite>sort</cite> (optional): a list of (key, direction) pairs
specifying the sort order for the query. If multiple documents
match the query, they are sorted and the first is deleted.</li>
<li><cite>**kwargs</cite> (optional): additional command arguments can be passed
as keyword arguments (for example maxTimeMS can be used with
recent server versions).</li>
</ul>
</td>
</tr>
</tbody>
</table>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.2: </span>Respects write concern.</p>
</div>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">Starting in PyMongo 3.2, this command uses the
<a class="reference internal" href="write_concern.html#pymongo.write_concern.WriteConcern" title="pymongo.write_concern.WriteConcern"><code class="xref py py-class docutils literal"><span class="pre">WriteConcern</span></code></a> of this
<a class="reference internal" href="#pymongo.collection.Collection" title="pymongo.collection.Collection"><code class="xref py py-class docutils literal"><span class="pre">Collection</span></code></a> when connected to MongoDB >=
3.2. Note that using an elevated write concern with this command may
be slower compared to using the default write concern.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.4: </span>Added the <cite>collation</cite> option.</p>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 3.0.</span></p>
</div>
</dd></dl>
<dl class="method">
<dt id="pymongo.collection.Collection.find_one_and_replace">
<code class="descname">find_one_and_replace</code><span class="sig-paren">(</span><em>filter</em>, <em>replacement</em>, <em>projection=None</em>, <em>sort=None</em>, <em>return_document=ReturnDocument.BEFORE</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.find_one_and_replace" title="Permalink to this definition">¶</a></dt>
<dd><p>Finds a single document and replaces it, returning either the
original or the replaced document.</p>
<p>The <a class="reference internal" href="#pymongo.collection.Collection.find_one_and_replace" title="pymongo.collection.Collection.find_one_and_replace"><code class="xref py py-meth docutils literal"><span class="pre">find_one_and_replace()</span></code></a> method differs from
<a class="reference internal" href="#pymongo.collection.Collection.find_one_and_update" title="pymongo.collection.Collection.find_one_and_update"><code class="xref py py-meth docutils literal"><span class="pre">find_one_and_update()</span></code></a> by replacing the document matched by
<em>filter</em>, rather than modifying the existing document.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">for</span> <span class="n">doc</span> <span class="ow">in</span> <span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">find</span><span class="p">({}):</span>
<span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">doc</span><span class="p">)</span>
<span class="gp">...</span>
<span class="go">{u'x': 1, u'_id': 0}</span>
<span class="go">{u'x': 1, u'_id': 1}</span>
<span class="go">{u'x': 1, u'_id': 2}</span>
<span class="gp">>>> </span><span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">find_one_and_replace</span><span class="p">({</span><span class="s1">'x'</span><span class="p">:</span> <span class="mi">1</span><span class="p">},</span> <span class="p">{</span><span class="s1">'y'</span><span class="p">:</span> <span class="mi">1</span><span class="p">})</span>
<span class="go">{u'x': 1, u'_id': 0}</span>
<span class="gp">>>> </span><span class="k">for</span> <span class="n">doc</span> <span class="ow">in</span> <span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">find</span><span class="p">({}):</span>
<span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">doc</span><span class="p">)</span>
<span class="gp">...</span>
<span class="go">{u'y': 1, u'_id': 0}</span>
<span class="go">{u'x': 1, u'_id': 1}</span>
<span class="go">{u'x': 1, u'_id': 2}</span>
</pre></div>
</div>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><cite>filter</cite>: A query that matches the document to replace.</li>
<li><cite>replacement</cite>: The replacement document.</li>
<li><cite>projection</cite> (optional): A list of field names that should be
returned in the result document or a mapping specifying the fields
to include or exclude. If <cite>projection</cite> is a list “_id” will
always be returned. Use a mapping to exclude fields from
the result (e.g. projection={‘_id’: False}).</li>
<li><cite>sort</cite> (optional): a list of (key, direction) pairs
specifying the sort order for the query. If multiple documents
match the query, they are sorted and the first is replaced.</li>
<li><cite>upsert</cite> (optional): When <code class="docutils literal"><span class="pre">True</span></code>, inserts a new document if no
document matches the query. Defaults to <code class="docutils literal"><span class="pre">False</span></code>.</li>
<li><cite>return_document</cite>: If
<a class="reference internal" href="#pymongo.collection.ReturnDocument.BEFORE" title="pymongo.collection.ReturnDocument.BEFORE"><code class="xref py py-attr docutils literal"><span class="pre">ReturnDocument.BEFORE</span></code></a> (the default),
returns the original document before it was replaced, or <code class="docutils literal"><span class="pre">None</span></code>
if no document matches. If
<a class="reference internal" href="#pymongo.collection.ReturnDocument.AFTER" title="pymongo.collection.ReturnDocument.AFTER"><code class="xref py py-attr docutils literal"><span class="pre">ReturnDocument.AFTER</span></code></a>, returns the replaced
or inserted document.</li>
<li><cite>**kwargs</cite> (optional): additional command arguments can be passed
as keyword arguments (for example maxTimeMS can be used with
recent server versions).</li>
</ul>
</td>
</tr>
</tbody>
</table>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.4: </span>Added the <cite>collation</cite> option.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.2: </span>Respects write concern.</p>
</div>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">Starting in PyMongo 3.2, this command uses the
<a class="reference internal" href="write_concern.html#pymongo.write_concern.WriteConcern" title="pymongo.write_concern.WriteConcern"><code class="xref py py-class docutils literal"><span class="pre">WriteConcern</span></code></a> of this
<a class="reference internal" href="#pymongo.collection.Collection" title="pymongo.collection.Collection"><code class="xref py py-class docutils literal"><span class="pre">Collection</span></code></a> when connected to MongoDB >=
3.2. Note that using an elevated write concern with this command may
be slower compared to using the default write concern.</p>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 3.0.</span></p>
</div>
</dd></dl>
<dl class="method">
<dt id="pymongo.collection.Collection.find_one_and_update">
<code class="descname">find_one_and_update</code><span class="sig-paren">(</span><em>filter</em>, <em>update</em>, <em>projection=None</em>, <em>sort=None</em>, <em>return_document=ReturnDocument.BEFORE</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.find_one_and_update" title="Permalink to this definition">¶</a></dt>
<dd><p>Finds a single document and updates it, returning either the
original or the updated document.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">find_one_and_update</span><span class="p">(</span>
<span class="gp">... </span> <span class="p">{</span><span class="s1">'_id'</span><span class="p">:</span> <span class="mi">665</span><span class="p">},</span> <span class="p">{</span><span class="s1">'$inc'</span><span class="p">:</span> <span class="p">{</span><span class="s1">'count'</span><span class="p">:</span> <span class="mi">1</span><span class="p">},</span> <span class="s1">'$set'</span><span class="p">:</span> <span class="p">{</span><span class="s1">'done'</span><span class="p">:</span> <span class="kc">True</span><span class="p">}})</span>
<span class="go">{u'_id': 665, u'done': False, u'count': 25}}</span>
</pre></div>
</div>
<p>By default <a class="reference internal" href="#pymongo.collection.Collection.find_one_and_update" title="pymongo.collection.Collection.find_one_and_update"><code class="xref py py-meth docutils literal"><span class="pre">find_one_and_update()</span></code></a> returns the original version of
the document before the update was applied. To return the updated
version of the document instead, use the <em>return_document</em> option.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">pymongo</span> <span class="k">import</span> <span class="n">ReturnDocument</span>
<span class="gp">>>> </span><span class="n">db</span><span class="o">.</span><span class="n">example</span><span class="o">.</span><span class="n">find_one_and_update</span><span class="p">(</span>
<span class="gp">... </span> <span class="p">{</span><span class="s1">'_id'</span><span class="p">:</span> <span class="s1">'userid'</span><span class="p">},</span>
<span class="gp">... </span> <span class="p">{</span><span class="s1">'$inc'</span><span class="p">:</span> <span class="p">{</span><span class="s1">'seq'</span><span class="p">:</span> <span class="mi">1</span><span class="p">}},</span>
<span class="gp">... </span> <span class="n">return_document</span><span class="o">=</span><span class="n">ReturnDocument</span><span class="o">.</span><span class="n">AFTER</span><span class="p">)</span>
<span class="go">{u'_id': u'userid', u'seq': 1}</span>
</pre></div>
</div>
<p>You can limit the fields returned with the <em>projection</em> option.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">db</span><span class="o">.</span><span class="n">example</span><span class="o">.</span><span class="n">find_one_and_update</span><span class="p">(</span>
<span class="gp">... </span> <span class="p">{</span><span class="s1">'_id'</span><span class="p">:</span> <span class="s1">'userid'</span><span class="p">},</span>
<span class="gp">... </span> <span class="p">{</span><span class="s1">'$inc'</span><span class="p">:</span> <span class="p">{</span><span class="s1">'seq'</span><span class="p">:</span> <span class="mi">1</span><span class="p">}},</span>
<span class="gp">... </span> <span class="n">projection</span><span class="o">=</span><span class="p">{</span><span class="s1">'seq'</span><span class="p">:</span> <span class="kc">True</span><span class="p">,</span> <span class="s1">'_id'</span><span class="p">:</span> <span class="kc">False</span><span class="p">},</span>
<span class="gp">... </span> <span class="n">return_document</span><span class="o">=</span><span class="n">ReturnDocument</span><span class="o">.</span><span class="n">AFTER</span><span class="p">)</span>
<span class="go">{u'seq': 2}</span>
</pre></div>
</div>
<p>The <em>upsert</em> option can be used to create the document if it doesn’t
already exist.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">db</span><span class="o">.</span><span class="n">example</span><span class="o">.</span><span class="n">delete_many</span><span class="p">({})</span><span class="o">.</span><span class="n">deleted_count</span>
<span class="go">1</span>
<span class="gp">>>> </span><span class="n">db</span><span class="o">.</span><span class="n">example</span><span class="o">.</span><span class="n">find_one_and_update</span><span class="p">(</span>
<span class="gp">... </span> <span class="p">{</span><span class="s1">'_id'</span><span class="p">:</span> <span class="s1">'userid'</span><span class="p">},</span>
<span class="gp">... </span> <span class="p">{</span><span class="s1">'$inc'</span><span class="p">:</span> <span class="p">{</span><span class="s1">'seq'</span><span class="p">:</span> <span class="mi">1</span><span class="p">}},</span>
<span class="gp">... </span> <span class="n">projection</span><span class="o">=</span><span class="p">{</span><span class="s1">'seq'</span><span class="p">:</span> <span class="kc">True</span><span class="p">,</span> <span class="s1">'_id'</span><span class="p">:</span> <span class="kc">False</span><span class="p">},</span>
<span class="gp">... </span> <span class="n">upsert</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
<span class="gp">... </span> <span class="n">return_document</span><span class="o">=</span><span class="n">ReturnDocument</span><span class="o">.</span><span class="n">AFTER</span><span class="p">)</span>
<span class="go">{u'seq': 1}</span>
</pre></div>
</div>
<p>If multiple documents match <em>filter</em>, a <em>sort</em> can be applied.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">for</span> <span class="n">doc</span> <span class="ow">in</span> <span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">find</span><span class="p">({</span><span class="s1">'done'</span><span class="p">:</span> <span class="kc">True</span><span class="p">}):</span>
<span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">doc</span><span class="p">)</span>
<span class="gp">...</span>
<span class="go">{u'_id': 665, u'done': True, u'result': {u'count': 26}}</span>
<span class="go">{u'_id': 701, u'done': True, u'result': {u'count': 17}}</span>
<span class="gp">>>> </span><span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">find_one_and_update</span><span class="p">(</span>
<span class="gp">... </span> <span class="p">{</span><span class="s1">'done'</span><span class="p">:</span> <span class="kc">True</span><span class="p">},</span>
<span class="gp">... </span> <span class="p">{</span><span class="s1">'$set'</span><span class="p">:</span> <span class="p">{</span><span class="s1">'final'</span><span class="p">:</span> <span class="kc">True</span><span class="p">}},</span>
<span class="gp">... </span> <span class="n">sort</span><span class="o">=</span><span class="p">[(</span><span class="s1">'_id'</span><span class="p">,</span> <span class="n">pymongo</span><span class="o">.</span><span class="n">DESCENDING</span><span class="p">)])</span>
<span class="go">{u'_id': 701, u'done': True, u'result': {u'count': 17}}</span>
</pre></div>
</div>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><cite>filter</cite>: A query that matches the document to update.</li>
<li><cite>update</cite>: The update operations to apply.</li>
<li><cite>projection</cite> (optional): A list of field names that should be
returned in the result document or a mapping specifying the fields
to include or exclude. If <cite>projection</cite> is a list “_id” will
always be returned. Use a dict to exclude fields from
the result (e.g. projection={‘_id’: False}).</li>
<li><cite>sort</cite> (optional): a list of (key, direction) pairs
specifying the sort order for the query. If multiple documents
match the query, they are sorted and the first is updated.</li>
<li><cite>upsert</cite> (optional): When <code class="docutils literal"><span class="pre">True</span></code>, inserts a new document if no
document matches the query. Defaults to <code class="docutils literal"><span class="pre">False</span></code>.</li>
<li><cite>return_document</cite>: If
<a class="reference internal" href="#pymongo.collection.ReturnDocument.BEFORE" title="pymongo.collection.ReturnDocument.BEFORE"><code class="xref py py-attr docutils literal"><span class="pre">ReturnDocument.BEFORE</span></code></a> (the default),
returns the original document before it was updated, or <code class="docutils literal"><span class="pre">None</span></code>
if no document matches. If
<a class="reference internal" href="#pymongo.collection.ReturnDocument.AFTER" title="pymongo.collection.ReturnDocument.AFTER"><code class="xref py py-attr docutils literal"><span class="pre">ReturnDocument.AFTER</span></code></a>, returns the updated
or inserted document.</li>
<li><cite>**kwargs</cite> (optional): additional command arguments can be passed
as keyword arguments (for example maxTimeMS can be used with
recent server versions).</li>
</ul>
</td>
</tr>
</tbody>
</table>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.4: </span>Added the <cite>collation</cite> option.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.2: </span>Respects write concern.</p>
</div>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">Starting in PyMongo 3.2, this command uses the
<a class="reference internal" href="write_concern.html#pymongo.write_concern.WriteConcern" title="pymongo.write_concern.WriteConcern"><code class="xref py py-class docutils literal"><span class="pre">WriteConcern</span></code></a> of this
<a class="reference internal" href="#pymongo.collection.Collection" title="pymongo.collection.Collection"><code class="xref py py-class docutils literal"><span class="pre">Collection</span></code></a> when connected to MongoDB >=
3.2. Note that using an elevated write concern with this command may
be slower compared to using the default write concern.</p>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 3.0.</span></p>
</div>
</dd></dl>
<dl class="method">
<dt id="pymongo.collection.Collection.count">
<code class="descname">count</code><span class="sig-paren">(</span><em>filter=None</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.count" title="Permalink to this definition">¶</a></dt>
<dd><p>Get the number of documents in this collection.</p>
<p>All optional count parameters should be passed as keyword arguments
to this method. Valid options include:</p>
<blockquote>
<div><ul class="simple">
<li><cite>hint</cite> (string or list of tuples): The index to use. Specify either
the index name as a string or the index specification as a list of
tuples (e.g. [(‘a’, pymongo.ASCENDING), (‘b’, pymongo.ASCENDING)]).</li>
<li><cite>limit</cite> (int): The maximum number of documents to count.</li>
<li><cite>skip</cite> (int): The number of matching documents to skip before
returning results.</li>
<li><cite>maxTimeMS</cite> (int): The maximum amount of time to allow the count
command to run, in milliseconds.</li>
<li><cite>collation</cite> (optional): An instance of
<a class="reference internal" href="collation.html#pymongo.collation.Collation" title="pymongo.collation.Collation"><code class="xref py py-class docutils literal"><span class="pre">Collation</span></code></a>. This option is only supported
on MongoDB 3.4 and above.</li>
</ul>
</div></blockquote>
<p>The <a class="reference internal" href="#pymongo.collection.Collection.count" title="pymongo.collection.Collection.count"><code class="xref py py-meth docutils literal"><span class="pre">count()</span></code></a> method obeys the <a class="reference internal" href="#pymongo.collection.Collection.read_preference" title="pymongo.collection.Collection.read_preference"><code class="xref py py-attr docutils literal"><span class="pre">read_preference</span></code></a> of
this <a class="reference internal" href="#pymongo.collection.Collection" title="pymongo.collection.Collection"><code class="xref py py-class docutils literal"><span class="pre">Collection</span></code></a>.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><cite>filter</cite> (optional): A query document that selects which documents
to count in the collection.</li>
<li><cite>**kwargs</cite> (optional): See list of options above.</li>
</ul>
</td>
</tr>
</tbody>
</table>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.4: </span>Support the <cite>collation</cite> option.</p>
</div>
</dd></dl>
<dl class="method">
<dt id="pymongo.collection.Collection.distinct">
<code class="descname">distinct</code><span class="sig-paren">(</span><em>key</em>, <em>filter=None</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.distinct" title="Permalink to this definition">¶</a></dt>
<dd><p>Get a list of distinct values for <cite>key</cite> among all documents
in this collection.</p>
<p>Raises <code class="xref py py-class docutils literal"><span class="pre">TypeError</span></code> if <cite>key</cite> is not an instance of
<code class="xref py py-class docutils literal"><span class="pre">basestring</span></code> (<code class="xref py py-class docutils literal"><span class="pre">str</span></code> in python 3).</p>
<p>All optional distinct parameters should be passed as keyword arguments
to this method. Valid options include:</p>
<blockquote>
<div><ul class="simple">
<li><cite>maxTimeMS</cite> (int): The maximum amount of time to allow the count
command to run, in milliseconds.</li>
<li><cite>collation</cite> (optional): An instance of
<a class="reference internal" href="collation.html#pymongo.collation.Collation" title="pymongo.collation.Collation"><code class="xref py py-class docutils literal"><span class="pre">Collation</span></code></a>. This option is only supported
on MongoDB 3.4 and above.</li>
</ul>
</div></blockquote>
<p>The <a class="reference internal" href="#pymongo.collection.Collection.distinct" title="pymongo.collection.Collection.distinct"><code class="xref py py-meth docutils literal"><span class="pre">distinct()</span></code></a> method obeys the <a class="reference internal" href="#pymongo.collection.Collection.read_preference" title="pymongo.collection.Collection.read_preference"><code class="xref py py-attr docutils literal"><span class="pre">read_preference</span></code></a> of
this <a class="reference internal" href="#pymongo.collection.Collection" title="pymongo.collection.Collection"><code class="xref py py-class docutils literal"><span class="pre">Collection</span></code></a>.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><cite>key</cite>: name of the field for which we want to get the distinct
values</li>
<li><cite>filter</cite> (optional): A query document that specifies the documents
from which to retrieve the distinct values.</li>
<li><cite>**kwargs</cite> (optional): See list of options above.</li>
</ul>
</td>
</tr>
</tbody>
</table>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.4: </span>Support the <cite>collation</cite> option.</p>
</div>
</dd></dl>
<dl class="method">
<dt id="pymongo.collection.Collection.create_index">
<code class="descname">create_index</code><span class="sig-paren">(</span><em>keys</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.create_index" title="Permalink to this definition">¶</a></dt>
<dd><p>Creates an index on this collection.</p>
<p>Takes either a single key or a list of (key, direction) pairs.
The key(s) must be an instance of <code class="xref py py-class docutils literal"><span class="pre">basestring</span></code>
(<code class="xref py py-class docutils literal"><span class="pre">str</span></code> in python 3), and the direction(s) must be one of
(<a class="reference internal" href="#pymongo.ASCENDING" title="pymongo.ASCENDING"><code class="xref py py-data docutils literal"><span class="pre">ASCENDING</span></code></a>, <a class="reference internal" href="#pymongo.DESCENDING" title="pymongo.DESCENDING"><code class="xref py py-data docutils literal"><span class="pre">DESCENDING</span></code></a>,
<a class="reference internal" href="#pymongo.GEO2D" title="pymongo.GEO2D"><code class="xref py py-data docutils literal"><span class="pre">GEO2D</span></code></a>, <a class="reference internal" href="#pymongo.GEOHAYSTACK" title="pymongo.GEOHAYSTACK"><code class="xref py py-data docutils literal"><span class="pre">GEOHAYSTACK</span></code></a>,
<a class="reference internal" href="#pymongo.GEOSPHERE" title="pymongo.GEOSPHERE"><code class="xref py py-data docutils literal"><span class="pre">GEOSPHERE</span></code></a>, <a class="reference internal" href="#pymongo.HASHED" title="pymongo.HASHED"><code class="xref py py-data docutils literal"><span class="pre">HASHED</span></code></a>,
<a class="reference internal" href="#pymongo.TEXT" title="pymongo.TEXT"><code class="xref py py-data docutils literal"><span class="pre">TEXT</span></code></a>).</p>
<p>To create a single key ascending index on the key <code class="docutils literal"><span class="pre">'mike'</span></code> we just
use a string argument:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">my_collection</span><span class="o">.</span><span class="n">create_index</span><span class="p">(</span><span class="s2">"mike"</span><span class="p">)</span>
</pre></div>
</div>
<p>For a compound index on <code class="docutils literal"><span class="pre">'mike'</span></code> descending and <code class="docutils literal"><span class="pre">'eliot'</span></code>
ascending we need to use a list of tuples:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">my_collection</span><span class="o">.</span><span class="n">create_index</span><span class="p">([(</span><span class="s2">"mike"</span><span class="p">,</span> <span class="n">pymongo</span><span class="o">.</span><span class="n">DESCENDING</span><span class="p">),</span>
<span class="gp">... </span> <span class="p">(</span><span class="s2">"eliot"</span><span class="p">,</span> <span class="n">pymongo</span><span class="o">.</span><span class="n">ASCENDING</span><span class="p">)])</span>
</pre></div>
</div>
<p>All optional index creation parameters should be passed as
keyword arguments to this method. For example:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">my_collection</span><span class="o">.</span><span class="n">create_index</span><span class="p">([(</span><span class="s2">"mike"</span><span class="p">,</span> <span class="n">pymongo</span><span class="o">.</span><span class="n">DESCENDING</span><span class="p">)],</span>
<span class="gp">... </span> <span class="n">background</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
</pre></div>
</div>
<p>Valid options include, but are not limited to:</p>
<blockquote>
<div><ul class="simple">
<li><cite>name</cite>: custom name to use for this index - if none is
given, a name will be generated.</li>
<li><cite>unique</cite>: if <code class="docutils literal"><span class="pre">True</span></code> creates a uniqueness constraint on the index.</li>
<li><cite>background</cite>: if <code class="docutils literal"><span class="pre">True</span></code> this index should be created in the
background.</li>
<li><cite>sparse</cite>: if <code class="docutils literal"><span class="pre">True</span></code>, omit from the index any documents that lack
the indexed field.</li>
<li><cite>bucketSize</cite>: for use with geoHaystack indexes.
Number of documents to group together within a certain proximity
to a given longitude and latitude.</li>
<li><cite>min</cite>: minimum value for keys in a <a class="reference internal" href="#pymongo.GEO2D" title="pymongo.GEO2D"><code class="xref py py-data docutils literal"><span class="pre">GEO2D</span></code></a>
index.</li>
<li><cite>max</cite>: maximum value for keys in a <a class="reference internal" href="#pymongo.GEO2D" title="pymongo.GEO2D"><code class="xref py py-data docutils literal"><span class="pre">GEO2D</span></code></a>
index.</li>
<li><cite>expireAfterSeconds</cite>: <int> Used to create an expiring (TTL)
collection. MongoDB will automatically delete documents from
this collection after <int> seconds. The indexed field must
be a UTC datetime or the data will not expire.</li>
<li><cite>partialFilterExpression</cite>: A document that specifies a filter for
a partial index.</li>
<li><cite>collation</cite> (optional): An instance of
<a class="reference internal" href="collation.html#pymongo.collation.Collation" title="pymongo.collation.Collation"><code class="xref py py-class docutils literal"><span class="pre">Collation</span></code></a>. This option is only supported
on MongoDB 3.4 and above.</li>
</ul>
</div></blockquote>
<p>See the MongoDB documentation for a full list of supported options by
server version.</p>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last"><cite>dropDups</cite> is not supported by MongoDB 3.0 or newer. The
option is silently ignored by the server and unique index builds
using the option will fail if a duplicate value is detected.</p>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last"><cite>partialFilterExpression</cite> requires server version <strong>>= 3.2</strong></p>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The <a class="reference internal" href="#pymongo.collection.Collection.write_concern" title="pymongo.collection.Collection.write_concern"><code class="xref py py-attr docutils literal"><span class="pre">write_concern</span></code></a> of
this collection is automatically applied to this operation when using
MongoDB >= 3.4.</p>
</div>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><cite>keys</cite>: a single key or a list of (key, direction)
pairs specifying the index to create</li>
<li><cite>**kwargs</cite> (optional): any additional index creation
options (see the above list) should be passed as keyword
arguments</li>
</ul>
</td>
</tr>
</tbody>
</table>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.4: </span>Apply this collection’s write concern automatically to this operation
when connected to MongoDB >= 3.4. Support the <cite>collation</cite> option.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.2: </span>Added partialFilterExpression to support partial indexes.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.0: </span>Renamed <cite>key_or_list</cite> to <cite>keys</cite>. Removed the <cite>cache_for</cite> option.
<a class="reference internal" href="#pymongo.collection.Collection.create_index" title="pymongo.collection.Collection.create_index"><code class="xref py py-meth docutils literal"><span class="pre">create_index()</span></code></a> no longer caches index names. Removed support
for the drop_dups and bucket_size aliases.</p>
</div>
<div class="admonition-see-general-mongodb-documentation admonition seealso">
<p class="first admonition-title">See also</p>
<p class="admonition-title">See general MongoDB documentation</p>
<p class="last"><a class="reference external" href="http://dochub.mongodb.org/core/indexes" name="pymongo.collection.Collection.create_index"><em>indexes</em></a></p>
</div>
</dd></dl>
<dl class="method">
<dt id="pymongo.collection.Collection.create_indexes">
<code class="descname">create_indexes</code><span class="sig-paren">(</span><em>indexes</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.create_indexes" title="Permalink to this definition">¶</a></dt>
<dd><p>Create one or more indexes on this collection.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">pymongo</span> <span class="k">import</span> <span class="n">IndexModel</span><span class="p">,</span> <span class="n">ASCENDING</span><span class="p">,</span> <span class="n">DESCENDING</span>
<span class="gp">>>> </span><span class="n">index1</span> <span class="o">=</span> <span class="n">IndexModel</span><span class="p">([(</span><span class="s2">"hello"</span><span class="p">,</span> <span class="n">DESCENDING</span><span class="p">),</span>
<span class="gp">... </span> <span class="p">(</span><span class="s2">"world"</span><span class="p">,</span> <span class="n">ASCENDING</span><span class="p">)],</span> <span class="n">name</span><span class="o">=</span><span class="s2">"hello_world"</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">index2</span> <span class="o">=</span> <span class="n">IndexModel</span><span class="p">([(</span><span class="s2">"goodbye"</span><span class="p">,</span> <span class="n">DESCENDING</span><span class="p">)])</span>
<span class="gp">>>> </span><span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">create_indexes</span><span class="p">([</span><span class="n">index1</span><span class="p">,</span> <span class="n">index2</span><span class="p">])</span>
<span class="go">["hello_world"]</span>
</pre></div>
</div>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><cite>indexes</cite>: A list of <a class="reference internal" href="operations.html#pymongo.operations.IndexModel" title="pymongo.operations.IndexModel"><code class="xref py py-class docutils literal"><span class="pre">IndexModel</span></code></a>
instances.</li>
</ul>
</td>
</tr>
</tbody>
</table>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last"><cite>create_indexes</cite> uses the <code class="docutils literal"><span class="pre">createIndexes</span></code> command
introduced in MongoDB <strong>2.6</strong> and cannot be used with earlier
versions.</p>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The <a class="reference internal" href="#pymongo.collection.Collection.write_concern" title="pymongo.collection.Collection.write_concern"><code class="xref py py-attr docutils literal"><span class="pre">write_concern</span></code></a> of
this collection is automatically applied to this operation when using
MongoDB >= 3.4.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.4: </span>Apply this collection’s write concern automatically to this operation
when connected to MongoDB >= 3.4.</p>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 3.0.</span></p>
</div>
</dd></dl>
<dl class="method">
<dt id="pymongo.collection.Collection.drop_index">
<code class="descname">drop_index</code><span class="sig-paren">(</span><em>index_or_name</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.drop_index" title="Permalink to this definition">¶</a></dt>
<dd><p>Drops the specified index on this collection.</p>
<p>Can be used on non-existant collections or collections with no
indexes. Raises OperationFailure on an error (e.g. trying to
drop an index that does not exist). <cite>index_or_name</cite>
can be either an index name (as returned by <cite>create_index</cite>),
or an index specifier (as passed to <cite>create_index</cite>). An index
specifier should be a list of (key, direction) pairs. Raises
TypeError if index is not an instance of (str, unicode, list).</p>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">if a custom name was used on index creation (by
passing the <cite>name</cite> parameter to <a class="reference internal" href="#pymongo.collection.Collection.create_index" title="pymongo.collection.Collection.create_index"><code class="xref py py-meth docutils literal"><span class="pre">create_index()</span></code></a> or
<a class="reference internal" href="#pymongo.collection.Collection.ensure_index" title="pymongo.collection.Collection.ensure_index"><code class="xref py py-meth docutils literal"><span class="pre">ensure_index()</span></code></a>) the index <strong>must</strong> be dropped by name.</p>
</div>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><cite>index_or_name</cite>: index (or name of index) to drop</li>
</ul>
</td>
</tr>
</tbody>
</table>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The <a class="reference internal" href="#pymongo.collection.Collection.write_concern" title="pymongo.collection.Collection.write_concern"><code class="xref py py-attr docutils literal"><span class="pre">write_concern</span></code></a> of
this collection is automatically applied to this operation when using
MongoDB >= 3.4.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.4: </span>Apply this collection’s write concern automatically to this operation
when connected to MongoDB >= 3.4.</p>
</div>
</dd></dl>
<dl class="method">
<dt id="pymongo.collection.Collection.drop_indexes">
<code class="descname">drop_indexes</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.drop_indexes" title="Permalink to this definition">¶</a></dt>
<dd><p>Drops all indexes on this collection.</p>
<p>Can be used on non-existant collections or collections with no indexes.
Raises OperationFailure on an error.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The <a class="reference internal" href="#pymongo.collection.Collection.write_concern" title="pymongo.collection.Collection.write_concern"><code class="xref py py-attr docutils literal"><span class="pre">write_concern</span></code></a> of
this collection is automatically applied to this operation when using
MongoDB >= 3.4.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.4: </span>Apply this collection’s write concern automatically to this operation
when connected to MongoDB >= 3.4.</p>
</div>
</dd></dl>
<dl class="method">
<dt id="pymongo.collection.Collection.reindex">
<code class="descname">reindex</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.reindex" title="Permalink to this definition">¶</a></dt>
<dd><p>Rebuilds all indexes on this collection.</p>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">reindex blocks all other operations (indexes
are built in the foreground) and will be slow for large
collections.</p>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The <a class="reference internal" href="#pymongo.collection.Collection.write_concern" title="pymongo.collection.Collection.write_concern"><code class="xref py py-attr docutils literal"><span class="pre">write_concern</span></code></a> of
this collection is automatically applied to this operation when using
MongoDB >= 3.4.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.4: </span>Apply this collection’s write concern automatically to this operation
when connected to MongoDB >= 3.4.</p>
</div>
</dd></dl>
<dl class="method">
<dt id="pymongo.collection.Collection.list_indexes">
<code class="descname">list_indexes</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.list_indexes" title="Permalink to this definition">¶</a></dt>
<dd><p>Get a cursor over the index documents for this collection.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">for</span> <span class="n">index</span> <span class="ow">in</span> <span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">list_indexes</span><span class="p">():</span>
<span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">index</span><span class="p">)</span>
<span class="gp">...</span>
<span class="go">SON([(u'v', 1), (u'key', SON([(u'_id', 1)])),</span>
<span class="go"> (u'name', u'_id_'), (u'ns', u'test.test')])</span>
</pre></div>
</div>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Returns:</th><td class="field-body">An instance of <a class="reference internal" href="command_cursor.html#pymongo.command_cursor.CommandCursor" title="pymongo.command_cursor.CommandCursor"><code class="xref py py-class docutils literal"><span class="pre">CommandCursor</span></code></a>.</td>
</tr>
</tbody>
</table>
<div class="versionadded">
<p><span class="versionmodified">New in version 3.0.</span></p>
</div>
</dd></dl>
<dl class="method">
<dt id="pymongo.collection.Collection.index_information">
<code class="descname">index_information</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.index_information" title="Permalink to this definition">¶</a></dt>
<dd><p>Get information on this collection’s indexes.</p>
<p>Returns a dictionary where the keys are index names (as
returned by create_index()) and the values are dictionaries
containing information about each index. The dictionary is
guaranteed to contain at least a single key, <code class="docutils literal"><span class="pre">"key"</span></code> which
is a list of (key, direction) pairs specifying the index (as
passed to create_index()). It will also contain any other
metadata about the indexes, except for the <code class="docutils literal"><span class="pre">"ns"</span></code> and
<code class="docutils literal"><span class="pre">"name"</span></code> keys, which are cleaned. Example output might look
like this:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">ensure_index</span><span class="p">(</span><span class="s2">"x"</span><span class="p">,</span> <span class="n">unique</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="go">u'x_1'</span>
<span class="gp">>>> </span><span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">index_information</span><span class="p">()</span>
<span class="go">{u'_id_': {u'key': [(u'_id', 1)]},</span>
<span class="go"> u'x_1': {u'unique': True, u'key': [(u'x', 1)]}}</span>
</pre></div>
</div>
</dd></dl>
<dl class="method">
<dt id="pymongo.collection.Collection.drop">
<code class="descname">drop</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.drop" title="Permalink to this definition">¶</a></dt>
<dd><p>Alias for <a class="reference internal" href="database.html#pymongo.database.Database.drop_collection" title="pymongo.database.Database.drop_collection"><code class="xref py py-meth docutils literal"><span class="pre">drop_collection()</span></code></a>.</p>
<p>The following two calls are equivalent:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">db</span><span class="o">.</span><span class="n">foo</span><span class="o">.</span><span class="n">drop</span><span class="p">()</span>
<span class="gp">>>> </span><span class="n">db</span><span class="o">.</span><span class="n">drop_collection</span><span class="p">(</span><span class="s2">"foo"</span><span class="p">)</span>
</pre></div>
</div>
</dd></dl>
<dl class="method">
<dt id="pymongo.collection.Collection.rename">
<code class="descname">rename</code><span class="sig-paren">(</span><em>new_name</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.rename" title="Permalink to this definition">¶</a></dt>
<dd><p>Rename this collection.</p>
<p>If operating in auth mode, client must be authorized as an
admin to perform this operation. Raises <code class="xref py py-class docutils literal"><span class="pre">TypeError</span></code> if
<cite>new_name</cite> is not an instance of <code class="xref py py-class docutils literal"><span class="pre">basestring</span></code>
(<code class="xref py py-class docutils literal"><span class="pre">str</span></code> in python 3). Raises <a class="reference internal" href="errors.html#pymongo.errors.InvalidName" title="pymongo.errors.InvalidName"><code class="xref py py-class docutils literal"><span class="pre">InvalidName</span></code></a>
if <cite>new_name</cite> is not a valid collection name.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><cite>new_name</cite>: new name for this collection</li>
<li><cite>**kwargs</cite> (optional): additional arguments to the rename command
may be passed as keyword arguments to this helper method
(i.e. <code class="docutils literal"><span class="pre">dropTarget=True</span></code>)</li>
</ul>
</td>
</tr>
</tbody>
</table>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The <a class="reference internal" href="#pymongo.collection.Collection.write_concern" title="pymongo.collection.Collection.write_concern"><code class="xref py py-attr docutils literal"><span class="pre">write_concern</span></code></a> of
this collection is automatically applied to this operation when using
MongoDB >= 3.4.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.4: </span>Apply this collection’s write concern automatically to this operation
when connected to MongoDB >= 3.4.</p>
</div>
</dd></dl>
<dl class="method">
<dt id="pymongo.collection.Collection.options">
<code class="descname">options</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.options" title="Permalink to this definition">¶</a></dt>
<dd><p>Get the options set on this collection.</p>
<p>Returns a dictionary of options and their values - see
<a class="reference internal" href="database.html#pymongo.database.Database.create_collection" title="pymongo.database.Database.create_collection"><code class="xref py py-meth docutils literal"><span class="pre">create_collection()</span></code></a> for more
information on the possible options. Returns an empty
dictionary if the collection has not been created yet.</p>
</dd></dl>
<dl class="method">
<dt id="pymongo.collection.Collection.group">
<code class="descname">group</code><span class="sig-paren">(</span><em>key</em>, <em>condition</em>, <em>initial</em>, <em>reduce</em>, <em>finalize=None</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.group" title="Permalink to this definition">¶</a></dt>
<dd><p>Perform a query similar to an SQL <em>group by</em> operation.</p>
<p>Returns an array of grouped items.</p>
<p>The <cite>key</cite> parameter can be:</p>
<blockquote>
<div><ul class="simple">
<li><code class="docutils literal"><span class="pre">None</span></code> to use the entire document as a key.</li>
<li>A <code class="xref py py-class docutils literal"><span class="pre">list</span></code> of keys (each a <code class="xref py py-class docutils literal"><span class="pre">basestring</span></code>
(<code class="xref py py-class docutils literal"><span class="pre">str</span></code> in python 3)) to group by.</li>
<li>A <code class="xref py py-class docutils literal"><span class="pre">basestring</span></code> (<code class="xref py py-class docutils literal"><span class="pre">str</span></code> in python 3), or
<a class="reference internal" href="../bson/code.html#bson.code.Code" title="bson.code.Code"><code class="xref py py-class docutils literal"><span class="pre">Code</span></code></a> instance containing a JavaScript
function to be applied to each document, returning the key
to group by.</li>
</ul>
</div></blockquote>
<p>The <a class="reference internal" href="#pymongo.collection.Collection.group" title="pymongo.collection.Collection.group"><code class="xref py py-meth docutils literal"><span class="pre">group()</span></code></a> method obeys the <a class="reference internal" href="#pymongo.collection.Collection.read_preference" title="pymongo.collection.Collection.read_preference"><code class="xref py py-attr docutils literal"><span class="pre">read_preference</span></code></a> of this
<a class="reference internal" href="#pymongo.collection.Collection" title="pymongo.collection.Collection"><code class="xref py py-class docutils literal"><span class="pre">Collection</span></code></a>.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><cite>key</cite>: fields to group by (see above description)</li>
<li><cite>condition</cite>: specification of rows to be
considered (as a <a class="reference internal" href="#pymongo.collection.Collection.find" title="pymongo.collection.Collection.find"><code class="xref py py-meth docutils literal"><span class="pre">find()</span></code></a> query specification)</li>
<li><cite>initial</cite>: initial value of the aggregation counter object</li>
<li><cite>reduce</cite>: aggregation function as a JavaScript string</li>
<li><cite>finalize</cite>: function to be called on each object in output list.</li>
<li><cite>**kwargs</cite> (optional): additional arguments to the group command
may be passed as keyword arguments to this helper method</li>
</ul>
</td>
</tr>
</tbody>
</table>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.4: </span>Added the <cite>collation</cite> option.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 2.2: </span>Removed deprecated argument: command</p>
</div>
</dd></dl>
<dl class="method">
<dt id="pymongo.collection.Collection.map_reduce">
<code class="descname">map_reduce</code><span class="sig-paren">(</span><em>map</em>, <em>reduce</em>, <em>out</em>, <em>full_response=False</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.map_reduce" title="Permalink to this definition">¶</a></dt>
<dd><p>Perform a map/reduce operation on this collection.</p>
<p>If <cite>full_response</cite> is <code class="docutils literal"><span class="pre">False</span></code> (default) returns a
<a class="reference internal" href="#pymongo.collection.Collection" title="pymongo.collection.Collection"><code class="xref py py-class docutils literal"><span class="pre">Collection</span></code></a> instance containing
the results of the operation. Otherwise, returns the full
response from the server to the <a class="reference external" href="http://docs.mongodb.org/manual/reference/command/mapReduce/">map reduce command</a>.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last">
<li><p class="first"><cite>map</cite>: map function (as a JavaScript string)</p>
</li>
<li><p class="first"><cite>reduce</cite>: reduce function (as a JavaScript string)</p>
</li>
<li><p class="first"><cite>out</cite>: output collection name or <cite>out object</cite> (dict). See
the <a class="reference external" href="http://docs.mongodb.org/manual/reference/command/mapReduce/">map reduce command</a> documentation for available options.
Note: <cite>out</cite> options are order sensitive. <a class="reference internal" href="../bson/son.html#bson.son.SON" title="bson.son.SON"><code class="xref py py-class docutils literal"><span class="pre">SON</span></code></a>
can be used to specify multiple options.
e.g. SON([(‘replace’, <collection name>), (‘db’, <database name>)])</p>
</li>
<li><p class="first"><cite>full_response</cite> (optional): if <code class="docutils literal"><span class="pre">True</span></code>, return full response to
this command - otherwise just return the result collection</p>
</li>
<li><p class="first"><cite>**kwargs</cite> (optional): additional arguments to the
<a class="reference external" href="http://docs.mongodb.org/manual/reference/command/mapReduce/">map reduce command</a> may be passed as keyword arguments to this
helper method, e.g.:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">map_reduce</span><span class="p">(</span><span class="nb">map</span><span class="p">,</span> <span class="n">reduce</span><span class="p">,</span> <span class="s2">"myresults"</span><span class="p">,</span> <span class="n">limit</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span>
</pre></div>
</div>
</li>
</ul>
</td>
</tr>
</tbody>
</table>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The <a class="reference internal" href="#pymongo.collection.Collection.map_reduce" title="pymongo.collection.Collection.map_reduce"><code class="xref py py-meth docutils literal"><span class="pre">map_reduce()</span></code></a> method does <strong>not</strong> obey the
<a class="reference internal" href="#pymongo.collection.Collection.read_preference" title="pymongo.collection.Collection.read_preference"><code class="xref py py-attr docutils literal"><span class="pre">read_preference</span></code></a> of this <a class="reference internal" href="#pymongo.collection.Collection" title="pymongo.collection.Collection"><code class="xref py py-class docutils literal"><span class="pre">Collection</span></code></a>. To run
mapReduce on a secondary use the <a class="reference internal" href="#pymongo.collection.Collection.inline_map_reduce" title="pymongo.collection.Collection.inline_map_reduce"><code class="xref py py-meth docutils literal"><span class="pre">inline_map_reduce()</span></code></a> method
instead.</p>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The <a class="reference internal" href="#pymongo.collection.Collection.write_concern" title="pymongo.collection.Collection.write_concern"><code class="xref py py-attr docutils literal"><span class="pre">write_concern</span></code></a> of
this collection is automatically applied to this operation (if the
output is not inline) when using MongoDB >= 3.4.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.4: </span>Apply this collection’s write concern automatically to this operation
when connected to MongoDB >= 3.4.</p>
</div>
<div class="admonition seealso">
<p class="first admonition-title">See also</p>
<p class="last"><a class="reference internal" href="../../examples/aggregation.html"><span class="doc">Aggregation Examples</span></a></p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.4: </span>Added the <cite>collation</cite> option.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 2.2: </span>Removed deprecated arguments: merge_output and reduce_output</p>
</div>
<div class="admonition-see-general-mongodb-documentation admonition seealso">
<p class="first admonition-title">See also</p>
<p class="admonition-title">See general MongoDB documentation</p>
<p class="last"><a class="reference external" href="http://dochub.mongodb.org/core/mapreduce" name="pymongo.collection.Collection.map_reduce"><em>mapreduce</em></a></p>
</div>
</dd></dl>
<dl class="method">
<dt id="pymongo.collection.Collection.inline_map_reduce">
<code class="descname">inline_map_reduce</code><span class="sig-paren">(</span><em>map</em>, <em>reduce</em>, <em>full_response=False</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.inline_map_reduce" title="Permalink to this definition">¶</a></dt>
<dd><p>Perform an inline map/reduce operation on this collection.</p>
<p>Perform the map/reduce operation on the server in RAM. A result
collection is not created. The result set is returned as a list
of documents.</p>
<p>If <cite>full_response</cite> is <code class="docutils literal"><span class="pre">False</span></code> (default) returns the
result documents in a list. Otherwise, returns the full
response from the server to the <a class="reference external" href="http://docs.mongodb.org/manual/reference/command/mapReduce/">map reduce command</a>.</p>
<p>The <a class="reference internal" href="#pymongo.collection.Collection.inline_map_reduce" title="pymongo.collection.Collection.inline_map_reduce"><code class="xref py py-meth docutils literal"><span class="pre">inline_map_reduce()</span></code></a> method obeys the <a class="reference internal" href="#pymongo.collection.Collection.read_preference" title="pymongo.collection.Collection.read_preference"><code class="xref py py-attr docutils literal"><span class="pre">read_preference</span></code></a>
of this <a class="reference internal" href="#pymongo.collection.Collection" title="pymongo.collection.Collection"><code class="xref py py-class docutils literal"><span class="pre">Collection</span></code></a>.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last">
<li><p class="first"><cite>map</cite>: map function (as a JavaScript string)</p>
</li>
<li><p class="first"><cite>reduce</cite>: reduce function (as a JavaScript string)</p>
</li>
<li><p class="first"><cite>full_response</cite> (optional): if <code class="docutils literal"><span class="pre">True</span></code>, return full response to
this command - otherwise just return the result collection</p>
</li>
<li><p class="first"><cite>**kwargs</cite> (optional): additional arguments to the
<a class="reference external" href="http://docs.mongodb.org/manual/reference/command/mapReduce/">map reduce command</a> may be passed as keyword arguments to this
helper method, e.g.:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">db</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">inline_map_reduce</span><span class="p">(</span><span class="nb">map</span><span class="p">,</span> <span class="n">reduce</span><span class="p">,</span> <span class="n">limit</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span>
</pre></div>
</div>
</li>
</ul>
</td>
</tr>
</tbody>
</table>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.4: </span>Added the <cite>collation</cite> option.</p>
</div>
</dd></dl>
<dl class="method">
<dt id="pymongo.collection.Collection.parallel_scan">
<code class="descname">parallel_scan</code><span class="sig-paren">(</span><em>num_cursors</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.parallel_scan" title="Permalink to this definition">¶</a></dt>
<dd><p>Scan this entire collection in parallel.</p>
<p>Returns a list of up to <code class="docutils literal"><span class="pre">num_cursors</span></code> cursors that can be iterated
concurrently. As long as the collection is not modified during
scanning, each document appears once in one of the cursors result
sets.</p>
<p>For example, to process each document in a collection using some
thread-safe <code class="docutils literal"><span class="pre">process_document()</span></code> function:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">def</span> <span class="nf">process_cursor</span><span class="p">(</span><span class="n">cursor</span><span class="p">):</span>
<span class="gp">... </span> <span class="k">for</span> <span class="n">document</span> <span class="ow">in</span> <span class="n">cursor</span><span class="p">:</span>
<span class="gp">... </span> <span class="c1"># Some thread-safe processing function:</span>
<span class="gp">... </span> <span class="n">process_document</span><span class="p">(</span><span class="n">document</span><span class="p">)</span>
<span class="go">>>></span>
<span class="gp">>>> </span><span class="c1"># Get up to 4 cursors.</span>
<span class="gp">...</span>
<span class="gp">>>> </span><span class="n">cursors</span> <span class="o">=</span> <span class="n">collection</span><span class="o">.</span><span class="n">parallel_scan</span><span class="p">(</span><span class="mi">4</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">threads</span> <span class="o">=</span> <span class="p">[</span>
<span class="gp">... </span> <span class="n">threading</span><span class="o">.</span><span class="n">Thread</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="n">process_cursor</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="p">(</span><span class="n">cursor</span><span class="p">,))</span>
<span class="gp">... </span> <span class="k">for</span> <span class="n">cursor</span> <span class="ow">in</span> <span class="n">cursors</span><span class="p">]</span>
<span class="go">>>></span>
<span class="gp">>>> </span><span class="k">for</span> <span class="n">thread</span> <span class="ow">in</span> <span class="n">threads</span><span class="p">:</span>
<span class="gp">... </span> <span class="n">thread</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
<span class="go">>>></span>
<span class="gp">>>> </span><span class="k">for</span> <span class="n">thread</span> <span class="ow">in</span> <span class="n">threads</span><span class="p">:</span>
<span class="gp">... </span> <span class="n">thread</span><span class="o">.</span><span class="n">join</span><span class="p">()</span>
<span class="go">>>></span>
<span class="gp">>>> </span><span class="c1"># All documents have now been processed.</span>
</pre></div>
</div>
<p>The <a class="reference internal" href="#pymongo.collection.Collection.parallel_scan" title="pymongo.collection.Collection.parallel_scan"><code class="xref py py-meth docutils literal"><span class="pre">parallel_scan()</span></code></a> method obeys the <a class="reference internal" href="#pymongo.collection.Collection.read_preference" title="pymongo.collection.Collection.read_preference"><code class="xref py py-attr docutils literal"><span class="pre">read_preference</span></code></a> of
this <a class="reference internal" href="#pymongo.collection.Collection" title="pymongo.collection.Collection"><code class="xref py py-class docutils literal"><span class="pre">Collection</span></code></a>.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><cite>num_cursors</cite>: the number of cursors to return</li>
<li><cite>**kwargs</cite>: additional options for the parallelCollectionScan
command can be passed as keyword arguments.</li>
</ul>
</td>
</tr>
</tbody>
</table>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Requires server version <strong>>= 2.5.5</strong>.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.4: </span>Added back support for arbitrary keyword arguments. MongoDB 3.4
adds support for maxTimeMS as an option to the
parallelCollectionScan command.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.0: </span>Removed support for arbitrary keyword arguments, since
the parallelCollectionScan command has no optional arguments.</p>
</div>
</dd></dl>
<dl class="method">
<dt id="pymongo.collection.Collection.initialize_unordered_bulk_op">
<code class="descname">initialize_unordered_bulk_op</code><span class="sig-paren">(</span><em>bypass_document_validation=False</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.initialize_unordered_bulk_op" title="Permalink to this definition">¶</a></dt>
<dd><p>Initialize an unordered batch of write operations.</p>
<p>Operations will be performed on the server in arbitrary order,
possibly in parallel. All operations will be attempted.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><cite>bypass_document_validation</cite>: (optional) If <code class="docutils literal"><span class="pre">True</span></code>, allows the
write to opt-out of document level validation. Default is
<code class="docutils literal"><span class="pre">False</span></code>.</li>
</ul>
</td>
</tr>
</tbody>
</table>
<p>Returns a <a class="reference internal" href="bulk.html#pymongo.bulk.BulkOperationBuilder" title="pymongo.bulk.BulkOperationBuilder"><code class="xref py py-class docutils literal"><span class="pre">BulkOperationBuilder</span></code></a> instance.</p>
<p>See <a class="reference internal" href="../../examples/bulk.html#unordered-bulk"><span class="std std-ref">Unordered Bulk Write Operations</span></a> for examples.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last"><cite>bypass_document_validation</cite> requires server version
<strong>>= 3.2</strong></p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.2: </span>Added bypass_document_validation support</p>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.7.</span></p>
</div>
</dd></dl>
<dl class="method">
<dt id="pymongo.collection.Collection.initialize_ordered_bulk_op">
<code class="descname">initialize_ordered_bulk_op</code><span class="sig-paren">(</span><em>bypass_document_validation=False</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.initialize_ordered_bulk_op" title="Permalink to this definition">¶</a></dt>
<dd><p>Initialize an ordered batch of write operations.</p>
<p>Operations will be performed on the server serially, in the
order provided. If an error occurs all remaining operations
are aborted.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><cite>bypass_document_validation</cite>: (optional) If <code class="docutils literal"><span class="pre">True</span></code>, allows the
write to opt-out of document level validation. Default is
<code class="docutils literal"><span class="pre">False</span></code>.</li>
</ul>
</td>
</tr>
</tbody>
</table>
<p>Returns a <a class="reference internal" href="bulk.html#pymongo.bulk.BulkOperationBuilder" title="pymongo.bulk.BulkOperationBuilder"><code class="xref py py-class docutils literal"><span class="pre">BulkOperationBuilder</span></code></a> instance.</p>
<p>See <a class="reference internal" href="../../examples/bulk.html#ordered-bulk"><span class="std std-ref">Ordered Bulk Write Operations</span></a> for examples.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last"><cite>bypass_document_validation</cite> requires server version
<strong>>= 3.2</strong></p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.2: </span>Added bypass_document_validation support</p>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.7.</span></p>
</div>
</dd></dl>
<dl class="method">
<dt id="pymongo.collection.Collection.insert">
<code class="descname">insert</code><span class="sig-paren">(</span><em>doc_or_docs</em>, <em>manipulate=True</em>, <em>check_keys=True</em>, <em>continue_on_error=False</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.insert" title="Permalink to this definition">¶</a></dt>
<dd><p>Insert a document(s) into this collection.</p>
<p><strong>DEPRECATED</strong> - Use <a class="reference internal" href="#pymongo.collection.Collection.insert_one" title="pymongo.collection.Collection.insert_one"><code class="xref py py-meth docutils literal"><span class="pre">insert_one()</span></code></a> or <a class="reference internal" href="#pymongo.collection.Collection.insert_many" title="pymongo.collection.Collection.insert_many"><code class="xref py py-meth docutils literal"><span class="pre">insert_many()</span></code></a> instead.</p>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.0: </span>Removed the <cite>safe</cite> parameter. Pass <code class="docutils literal"><span class="pre">w=0</span></code> for unacknowledged write
operations.</p>
</div>
</dd></dl>
<dl class="method">
<dt id="pymongo.collection.Collection.save">
<code class="descname">save</code><span class="sig-paren">(</span><em>to_save</em>, <em>manipulate=True</em>, <em>check_keys=True</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.save" title="Permalink to this definition">¶</a></dt>
<dd><p>Save a document in this collection.</p>
<p><strong>DEPRECATED</strong> - Use <a class="reference internal" href="#pymongo.collection.Collection.insert_one" title="pymongo.collection.Collection.insert_one"><code class="xref py py-meth docutils literal"><span class="pre">insert_one()</span></code></a> or <a class="reference internal" href="#pymongo.collection.Collection.replace_one" title="pymongo.collection.Collection.replace_one"><code class="xref py py-meth docutils literal"><span class="pre">replace_one()</span></code></a> instead.</p>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.0: </span>Removed the <cite>safe</cite> parameter. Pass <code class="docutils literal"><span class="pre">w=0</span></code> for unacknowledged write
operations.</p>
</div>
</dd></dl>
<dl class="method">
<dt id="pymongo.collection.Collection.update">
<code class="descname">update</code><span class="sig-paren">(</span><em>spec</em>, <em>document</em>, <em>upsert=False</em>, <em>manipulate=False</em>, <em>multi=False</em>, <em>check_keys=True</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.update" title="Permalink to this definition">¶</a></dt>
<dd><p>Update a document(s) in this collection.</p>
<p><strong>DEPRECATED</strong> - Use <a class="reference internal" href="#pymongo.collection.Collection.replace_one" title="pymongo.collection.Collection.replace_one"><code class="xref py py-meth docutils literal"><span class="pre">replace_one()</span></code></a>, <a class="reference internal" href="#pymongo.collection.Collection.update_one" title="pymongo.collection.Collection.update_one"><code class="xref py py-meth docutils literal"><span class="pre">update_one()</span></code></a>, or
<a class="reference internal" href="#pymongo.collection.Collection.update_many" title="pymongo.collection.Collection.update_many"><code class="xref py py-meth docutils literal"><span class="pre">update_many()</span></code></a> instead.</p>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.0: </span>Removed the <cite>safe</cite> parameter. Pass <code class="docutils literal"><span class="pre">w=0</span></code> for unacknowledged write
operations.</p>
</div>
</dd></dl>
<dl class="method">
<dt id="pymongo.collection.Collection.remove">
<code class="descname">remove</code><span class="sig-paren">(</span><em>spec_or_id=None</em>, <em>multi=True</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.remove" title="Permalink to this definition">¶</a></dt>
<dd><p>Remove a document(s) from this collection.</p>
<p><strong>DEPRECATED</strong> - Use <a class="reference internal" href="#pymongo.collection.Collection.delete_one" title="pymongo.collection.Collection.delete_one"><code class="xref py py-meth docutils literal"><span class="pre">delete_one()</span></code></a> or <a class="reference internal" href="#pymongo.collection.Collection.delete_many" title="pymongo.collection.Collection.delete_many"><code class="xref py py-meth docutils literal"><span class="pre">delete_many()</span></code></a> instead.</p>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.0: </span>Removed the <cite>safe</cite> parameter. Pass <code class="docutils literal"><span class="pre">w=0</span></code> for unacknowledged write
operations.</p>
</div>
</dd></dl>
<dl class="method">
<dt id="pymongo.collection.Collection.find_and_modify">
<code class="descname">find_and_modify</code><span class="sig-paren">(</span><em>query={}</em>, <em>update=None</em>, <em>upsert=False</em>, <em>sort=None</em>, <em>full_response=False</em>, <em>manipulate=False</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.find_and_modify" title="Permalink to this definition">¶</a></dt>
<dd><p>Update and return an object.</p>
<p><strong>DEPRECATED</strong> - Use <a class="reference internal" href="#pymongo.collection.Collection.find_one_and_delete" title="pymongo.collection.Collection.find_one_and_delete"><code class="xref py py-meth docutils literal"><span class="pre">find_one_and_delete()</span></code></a>,
<a class="reference internal" href="#pymongo.collection.Collection.find_one_and_replace" title="pymongo.collection.Collection.find_one_and_replace"><code class="xref py py-meth docutils literal"><span class="pre">find_one_and_replace()</span></code></a>, or <a class="reference internal" href="#pymongo.collection.Collection.find_one_and_update" title="pymongo.collection.Collection.find_one_and_update"><code class="xref py py-meth docutils literal"><span class="pre">find_one_and_update()</span></code></a> instead.</p>
</dd></dl>
<dl class="method">
<dt id="pymongo.collection.Collection.ensure_index">
<code class="descname">ensure_index</code><span class="sig-paren">(</span><em>key_or_list</em>, <em>cache_for=300</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#pymongo.collection.Collection.ensure_index" title="Permalink to this definition">¶</a></dt>
<dd><p><strong>DEPRECATED</strong> - Ensures that an index exists on this collection.</p>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.0: </span><strong>DEPRECATED</strong></p>
</div>
</dd></dl>
</dd></dl>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<h4>Previous topic</h4>
<p class="topless"><a href="collation.html"
title="previous chapter"><code class="docutils literal"><span class="pre">collation</span></code> – Tools for working with collations.</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="command_cursor.html"
title="next chapter"><code class="docutils literal"><span class="pre">command_cursor</span></code> – Tools for iterating over MongoDB command results</a></p>
<div role="note" aria-label="source link">
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="../../_sources/api/pymongo/collection.txt"
rel="nofollow">Show Source</a></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
<h3>Quick search</h3>
<form class="search" action="../../search.html" method="get">
<div><input type="text" name="q" /></div>
<div><input type="submit" value="Go" /></div>
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="command_cursor.html" title="command_cursor – Tools for iterating over MongoDB command results"
>next</a> |</li>
<li class="right" >
<a href="collation.html" title="collation – Tools for working with collations."
>previous</a> |</li>
<li class="nav-item nav-item-0"><a href="../../index.html">PyMongo 3.4.0 documentation</a> »</li>
<li class="nav-item nav-item-1"><a href="../index.html" >API Documentation</a> »</li>
<li class="nav-item nav-item-2"><a href="index.html" ><code class="docutils literal"><span class="pre">pymongo</span></code> – Python driver for MongoDB</a> »</li>
</ul>
</div>
<div class="footer" role="contentinfo">
© Copyright 2017 - 2015, MongoDB, Inc..
Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.4.9.
</div>
</body>
</html>
|