/usr/share/doc/python-pymongo-doc/html/api/pymongo/collection.html is in python-pymongo-doc 2.6.3-1build1.
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 | <!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 2.6.3 documentation</title>
<link rel="stylesheet" href="../../_static/default.css" type="text/css" />
<link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: '../../',
VERSION: '2.6.3',
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="top" title="PyMongo 2.6.3 documentation" href="../../index.html" />
<link rel="up" title="pymongo – Python driver for MongoDB" href="index.html" />
<link rel="next" title="cursor – Tools for iterating over MongoDB query results" href="cursor.html" />
<link rel="prev" title="database – Database level operations" href="database.html" />
</head>
<body>
<div class="related">
<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="cursor.html" title="cursor – Tools for iterating over MongoDB query results"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="database.html" title="database – Database level operations"
accesskey="P">previous</a> |</li>
<li><a href="../../index.html">PyMongo 2.6.3 documentation</a> »</li>
<li><a href="../index.html" >API Documentation</a> »</li>
<li><a href="index.html" accesskey="U"><tt class="docutils literal"><span class="pre">pymongo</span></tt> – Python driver for MongoDB</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body">
<div class="section" id="module-pymongo.collection">
<span id="collection-collection-level-operations"></span><h1><tt class="xref py py-mod docutils literal"><span class="pre">collection</span></tt> – 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">
<tt class="descclassname">pymongo.</tt><tt class="descname">ASCENDING</tt><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">
<tt class="descclassname">pymongo.</tt><tt class="descname">DESCENDING</tt><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">
<tt class="descclassname">pymongo.</tt><tt class="descname">GEO2D</tt><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/geospatial-indexes/">geospatial index</a>.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 1.5.1.</span></p>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Geo-spatial indexing requires server version <strong>>= 1.3.3</strong>.</p>
</div>
</dd></dl>
<dl class="data">
<dt id="pymongo.GEOHAYSTACK">
<tt class="descclassname">pymongo.</tt><tt class="descname">GEOHAYSTACK</tt><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/geospatial-indexes/#haystack-indexes">haystack index</a>.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.1.</span></p>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Geo-spatial indexing requires server version <strong>>= 1.5.6</strong>.</p>
</div>
</dd></dl>
<dl class="data">
<dt id="pymongo.GEOSPHERE">
<tt class="descclassname">pymongo.</tt><tt class="descname">GEOSPHERE</tt><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/release-notes/2.4/#new-geospatial-indexes-with-geojson-and-improved-spherical-geometry">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">
<tt class="descclassname">pymongo.</tt><tt class="descname">HASHED</tt><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/release-notes/2.4/#new-hashed-index-and-sharding-with-a-hashed-shard-key">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="class">
<dt id="pymongo.collection.Collection">
<em class="property">class </em><tt class="descclassname">pymongo.collection.</tt><tt class="descname">Collection</tt><big>(</big><em>database, name[, create=False[, **kwargs]]]</em><big>)</big><a class="headerlink" href="#pymongo.collection.Collection" title="Permalink to this definition">¶</a></dt>
<dd><p>Get / create a Mongo collection.</p>
<p>Raises <tt class="xref py py-class docutils literal"><span class="pre">TypeError</span></tt> if <cite>name</cite> is not an instance of
<tt class="xref py py-class docutils literal"><span class="pre">basestring</span></tt> (<tt class="xref py py-class docutils literal"><span class="pre">str</span></tt> in python 3). Raises
<a class="reference internal" href="errors.html#pymongo.errors.InvalidName" title="pymongo.errors.InvalidName"><tt class="xref py py-class docutils literal"><span class="pre">InvalidName</span></tt></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"><tt class="xref py py-meth docutils literal"><span class="pre">create_collection()</span></tt></a> for valid
options.</p>
<p>If <cite>create</cite> is <tt class="docutils literal"><span class="pre">True</span></tt> or additional keyword arguments are
present a create command will be sent. Otherwise, a create
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 <tt class="docutils literal"><span class="pre">True</span></tt>, force collection
creation even without options being set</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 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="versionchanged">
<p><span class="versionmodified">Changed in version 1.5: </span>deprecating <cite>options</cite> in favor of kwargs</p>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 1.5: </span>the <cite>create</cite> parameter</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>
<tt class="descname">c[name] || c.name</tt></dt>
<dd><p>Get the <cite>name</cite> sub-collection of <a class="reference internal" href="#pymongo.collection.Collection" title="pymongo.collection.Collection"><tt class="xref py py-class docutils literal"><span class="pre">Collection</span></tt></a> <cite>c</cite>.</p>
<p>Raises <a class="reference internal" href="errors.html#pymongo.errors.InvalidName" title="pymongo.errors.InvalidName"><tt class="xref py py-class docutils literal"><span class="pre">InvalidName</span></tt></a> if an invalid
collection name is used.</p>
</dd></dl>
<dl class="attribute">
<dt id="pymongo.collection.Collection.full_name">
<tt class="descname">full_name</tt><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"><tt class="xref py py-class docutils literal"><span class="pre">Collection</span></tt></a>.</p>
<p>The full name is of the form <cite>database_name.collection_name</cite>.</p>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 1.3: </span><tt class="docutils literal"><span class="pre">full_name</span></tt> is now a property rather than a method.</p>
</div>
</dd></dl>
<dl class="attribute">
<dt id="pymongo.collection.Collection.name">
<tt class="descname">name</tt><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"><tt class="xref py py-class docutils literal"><span class="pre">Collection</span></tt></a>.</p>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 1.3: </span><tt class="docutils literal"><span class="pre">name</span></tt> is now a property rather than a method.</p>
</div>
</dd></dl>
<dl class="attribute">
<dt id="pymongo.collection.Collection.database">
<tt class="descname">database</tt><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"><tt class="xref py py-class docutils literal"><span class="pre">Database</span></tt></a> that this
<a class="reference internal" href="#pymongo.collection.Collection" title="pymongo.collection.Collection"><tt class="xref py py-class docutils literal"><span class="pre">Collection</span></tt></a> is a part of.</p>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 1.3: </span><tt class="docutils literal"><span class="pre">database</span></tt> is now a property rather than a method.</p>
</div>
</dd></dl>
<dl class="attribute">
<dt id="pymongo.collection.Collection.read_preference">
<tt class="descname">read_preference</tt><a class="headerlink" href="#pymongo.collection.Collection.read_preference" title="Permalink to this definition">¶</a></dt>
<dd><p>The read preference mode for this instance.</p>
<p>See <a class="reference internal" href="index.html#pymongo.read_preferences.ReadPreference" title="pymongo.read_preferences.ReadPreference"><tt class="xref py py-class docutils literal"><span class="pre">ReadPreference</span></tt></a> for available options.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.1.</span></p>
</div>
</dd></dl>
<dl class="attribute">
<dt id="pymongo.collection.Collection.tag_sets">
<tt class="descname">tag_sets</tt><a class="headerlink" href="#pymongo.collection.Collection.tag_sets" title="Permalink to this definition">¶</a></dt>
<dd><p>Set <tt class="docutils literal"><span class="pre">tag_sets</span></tt> to a list of dictionaries like [{‘dc’: ‘ny’}] to
read only from members whose <tt class="docutils literal"><span class="pre">dc</span></tt> tag has the value <tt class="docutils literal"><span class="pre">"ny"</span></tt>.
To specify a priority-order for tag sets, provide a list of
tag sets: <tt class="docutils literal"><span class="pre">[{'dc':</span> <span class="pre">'ny'},</span> <span class="pre">{'dc':</span> <span class="pre">'la'},</span> <span class="pre">{}]</span></tt>. A final, empty tag
set, <tt class="docutils literal"><span class="pre">{}</span></tt>, means “read from any member that matches the mode,
ignoring tags.” ReplicaSetConnection tries each set of tags in turn
until it finds a set of tags with at least one matching member.</p>
<blockquote>
<div><div class="admonition seealso">
<p class="first admonition-title">See also</p>
<p class="last"><a class="reference external" href="http://www.mongodb.org/display/DOCS/Data+Center+Awareness">Data-Center Awareness</a></p>
</div>
</div></blockquote>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.3.</span></p>
</div>
</dd></dl>
<dl class="attribute">
<dt id="pymongo.collection.Collection.secondary_acceptable_latency_ms">
<tt class="descname">secondary_acceptable_latency_ms</tt><a class="headerlink" href="#pymongo.collection.Collection.secondary_acceptable_latency_ms" title="Permalink to this definition">¶</a></dt>
<dd><p>Any replica-set member whose ping time is within
secondary_acceptable_latency_ms of the nearest member may accept
reads. Defaults to 15 milliseconds.</p>
<p>See <a class="reference internal" href="index.html#pymongo.read_preferences.ReadPreference" title="pymongo.read_preferences.ReadPreference"><tt class="xref py py-class docutils literal"><span class="pre">ReadPreference</span></tt></a>.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.3.</span></p>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last"><tt class="docutils literal"><span class="pre">secondary_acceptable_latency_ms</span></tt> is ignored when talking to a
replica set <em>through</em> a mongos. The equivalent is the <a class="reference external" href="http://docs.mongodb.org/manual/reference/mongos/#cmdoption-mongos--localThreshold">localThreshold</a> command
line option.</p>
</div>
</dd></dl>
<dl class="attribute">
<dt id="pymongo.collection.Collection.write_concern">
<tt class="descname">write_concern</tt><a class="headerlink" href="#pymongo.collection.Collection.write_concern" title="Permalink to this definition">¶</a></dt>
<dd><p>The default write concern for this instance.</p>
<p>Supports dict style access for getting/setting write concern
options. Valid options include:</p>
<ul class="simple">
<li><cite>w</cite>: (integer or string) If this is a replica set, write operations
will block until they have been replicated to the specified number
or tagged set of servers. <cite>w=<int></cite> always includes the replica set
primary (e.g. w=3 means write to the primary and wait until
replicated to <strong>two</strong> secondaries). <strong>Setting w=0 disables write
acknowledgement and all other write concern options.</strong></li>
<li><cite>wtimeout</cite>: (integer) Used in conjunction with <cite>w</cite>. Specify a value
in milliseconds to control how long to wait for write propagation
to complete. If replication does not complete in the given
timeframe, a timeout exception is raised.</li>
<li><cite>j</cite>: If <tt class="docutils literal"><span class="pre">True</span></tt> block until write operations have been committed
to the journal. Ignored if the server is running without journaling.</li>
<li><cite>fsync</cite>: If <tt class="docutils literal"><span class="pre">True</span></tt> force the database to fsync all files before
returning. When used with <cite>j</cite> the server awaits the next group
commit before returning.</li>
</ul>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">m</span> <span class="o">=</span> <span class="n">pymongo</span><span class="o">.</span><span class="n">MongoClient</span><span class="p">()</span>
<span class="gp">>>> </span><span class="n">m</span><span class="o">.</span><span class="n">write_concern</span>
<span class="go">{}</span>
<span class="gp">>>> </span><span class="n">m</span><span class="o">.</span><span class="n">write_concern</span> <span class="o">=</span> <span class="p">{</span><span class="s">'w'</span><span class="p">:</span> <span class="mi">2</span><span class="p">,</span> <span class="s">'wtimeout'</span><span class="p">:</span> <span class="mi">1000</span><span class="p">}</span>
<span class="gp">>>> </span><span class="n">m</span><span class="o">.</span><span class="n">write_concern</span>
<span class="go">{'wtimeout': 1000, 'w': 2}</span>
<span class="gp">>>> </span><span class="n">m</span><span class="o">.</span><span class="n">write_concern</span><span class="p">[</span><span class="s">'j'</span><span class="p">]</span> <span class="o">=</span> <span class="bp">True</span>
<span class="gp">>>> </span><span class="n">m</span><span class="o">.</span><span class="n">write_concern</span>
<span class="go">{'wtimeout': 1000, 'j': True, 'w': 2}</span>
<span class="gp">>>> </span><span class="n">m</span><span class="o">.</span><span class="n">write_concern</span> <span class="o">=</span> <span class="p">{</span><span class="s">'j'</span><span class="p">:</span> <span class="bp">True</span><span class="p">}</span>
<span class="gp">>>> </span><span class="n">m</span><span class="o">.</span><span class="n">write_concern</span>
<span class="go">{'j': True}</span>
<span class="gp">>>> </span><span class="c"># Disable write acknowledgement and write concern</span>
<span class="gp">...</span>
<span class="gp">>>> </span><span class="n">m</span><span class="o">.</span><span class="n">write_concern</span><span class="p">[</span><span class="s">'w'</span><span class="p">]</span> <span class="o">=</span> <span class="mi">0</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Accessing <a class="reference internal" href="#pymongo.collection.Collection.write_concern" title="pymongo.collection.Collection.write_concern"><tt class="xref py py-attr docutils literal"><span class="pre">write_concern</span></tt></a> returns its value
(a subclass of <tt class="xref py py-class docutils literal"><span class="pre">dict</span></tt>), not a copy.</p>
</div>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">If you are using <a class="reference internal" href="connection.html#pymongo.connection.Connection" title="pymongo.connection.Connection"><tt class="xref py py-class docutils literal"><span class="pre">Connection</span></tt></a>
or <a class="reference internal" href="replica_set_connection.html#pymongo.replica_set_connection.ReplicaSetConnection" title="pymongo.replica_set_connection.ReplicaSetConnection"><tt class="xref py py-class docutils literal"><span class="pre">ReplicaSetConnection</span></tt></a>
make sure you explicitly set <tt class="docutils literal"><span class="pre">w</span></tt> to 1 (or a greater value) or
<a class="reference internal" href="#pymongo.collection.Collection.safe" title="pymongo.collection.Collection.safe"><tt class="xref py py-attr docutils literal"><span class="pre">safe</span></tt></a> to <tt class="docutils literal"><span class="pre">True</span></tt>. Unlike calling
<a class="reference internal" href="#pymongo.collection.Collection.set_lasterror_options" title="pymongo.collection.Collection.set_lasterror_options"><tt class="xref py py-meth docutils literal"><span class="pre">set_lasterror_options()</span></tt></a>, setting an option in
<a class="reference internal" href="#pymongo.collection.Collection.write_concern" title="pymongo.collection.Collection.write_concern"><tt class="xref py py-attr docutils literal"><span class="pre">write_concern</span></tt></a> does not implicitly set <a class="reference internal" href="#pymongo.collection.Collection.safe" title="pymongo.collection.Collection.safe"><tt class="xref py py-attr docutils literal"><span class="pre">safe</span></tt></a>
to <tt class="docutils literal"><span class="pre">True</span></tt>.</p>
</div>
</dd></dl>
<dl class="attribute">
<dt id="pymongo.collection.Collection.uuid_subtype">
<tt class="descname">uuid_subtype</tt><a class="headerlink" href="#pymongo.collection.Collection.uuid_subtype" title="Permalink to this definition">¶</a></dt>
<dd><p>This attribute specifies which BSON Binary
subtype is used when storing UUIDs. Historically
UUIDs have been stored as BSON Binary subtype 3.
This attribute is used to switch to the newer BSON
binary subtype 4. It can also be used to force
legacy byte order and subtype compatibility with
the Java and C# drivers. See the
<a class="reference internal" href="../bson/binary.html#module-bson.binary" title="bson.binary: Tools for representing binary data to be stored in MongoDB"><tt class="xref py py-mod docutils literal"><span class="pre">bson.binary</span></tt></a> module for all options.</p>
</dd></dl>
<dl class="method">
<dt id="pymongo.collection.Collection.insert">
<tt class="descname">insert</tt><big>(</big><em>doc_or_docs</em><span class="optional">[</span>, <em>manipulate=True</em><span class="optional">[</span>, <em>safe=None</em><span class="optional">[</span>, <em>check_keys=True</em><span class="optional">[</span>, <em>continue_on_error=False</em><span class="optional">[</span>, <em>**kwargs</em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><big>)</big><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>If <cite>manipulate</cite> is <tt class="docutils literal"><span class="pre">True</span></tt>, the document(s) are manipulated using
any <a class="reference internal" href="son_manipulator.html#pymongo.son_manipulator.SONManipulator" title="pymongo.son_manipulator.SONManipulator"><tt class="xref py py-class docutils literal"><span class="pre">SONManipulator</span></tt></a> instances
that have been added to this <a class="reference internal" href="database.html#pymongo.database.Database" title="pymongo.database.Database"><tt class="xref py py-class docutils literal"><span class="pre">Database</span></tt></a>.
In this case an <tt class="docutils literal"><span class="pre">"_id"</span></tt> will be added if the document(s) does
not already contain one and the <tt class="docutils literal"><span class="pre">"id"</span></tt> (or list of <tt class="docutils literal"><span class="pre">"_id"</span></tt>
values for more than one document) will be returned.
If <cite>manipulate</cite> is <tt class="docutils literal"><span class="pre">False</span></tt> and the document(s) does not include
an <tt class="docutils literal"><span class="pre">"_id"</span></tt> one will be added by the server. The server
does not return the <tt class="docutils literal"><span class="pre">"_id"</span></tt> it created so <tt class="docutils literal"><span class="pre">None</span></tt> is returned.</p>
<p>Write concern options can be passed as keyword arguments, overriding
any global defaults. Valid options include w=<int/string>,
wtimeout=<int>, j=<bool>, or fsync=<bool>. See the parameter list below
for a detailed explanation of these options.</p>
<p>By default an acknowledgment is requested from the server that the
insert was successful, raising <a class="reference internal" href="errors.html#pymongo.errors.OperationFailure" title="pymongo.errors.OperationFailure"><tt class="xref py py-class docutils literal"><span class="pre">OperationFailure</span></tt></a>
if an error occurred. <strong>Passing ``w=0`` disables write acknowledgement
and all other write concern options.</strong></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 simple">
<li><cite>doc_or_docs</cite>: a document or list of documents to be
inserted</li>
<li><cite>manipulate</cite> (optional): If <tt class="docutils literal"><span class="pre">True</span></tt> manipulate the documents
before inserting.</li>
<li><cite>safe</cite> (optional): <strong>DEPRECATED</strong> - Use <cite>w</cite> instead.</li>
<li><cite>check_keys</cite> (optional): If <tt class="docutils literal"><span class="pre">True</span></tt> check if keys start with ‘$’
or contain ‘.’, raising <a class="reference internal" href="errors.html#pymongo.errors.InvalidName" title="pymongo.errors.InvalidName"><tt class="xref py py-class docutils literal"><span class="pre">InvalidName</span></tt></a> in
either case.</li>
<li><cite>continue_on_error</cite> (optional): If <tt class="docutils literal"><span class="pre">True</span></tt>, the database will not
stop processing a bulk insert if one fails (e.g. due to duplicate
IDs). This makes bulk insert behave similarly to a series of single
inserts, except lastError will be set if any insert fails, not just
the last one. If multiple errors occur, only the most recent will
be reported by <a class="reference internal" href="database.html#pymongo.database.Database.error" title="pymongo.database.Database.error"><tt class="xref py py-meth docutils literal"><span class="pre">error()</span></tt></a>.</li>
<li><cite>w</cite> (optional): (integer or string) If this is a replica set, write
operations will block until they have been replicated to the
specified number or tagged set of servers. <cite>w=<int></cite> always includes
the replica set primary (e.g. w=3 means write to the primary and wait
until replicated to <strong>two</strong> secondaries). <strong>Passing w=0 disables
write acknowledgement and all other write concern options.</strong></li>
<li><cite>wtimeout</cite> (optional): (integer) Used in conjunction with <cite>w</cite>.
Specify a value in milliseconds to control how long to wait for
write propagation to complete. If replication does not complete in
the given timeframe, a timeout exception is raised.</li>
<li><cite>j</cite> (optional): If <tt class="docutils literal"><span class="pre">True</span></tt> block until write operations have been
committed to the journal. Ignored if the server is running without
journaling.</li>
<li><cite>fsync</cite> (optional): If <tt class="docutils literal"><span class="pre">True</span></tt> force the database to fsync all
files before returning. When used with <cite>j</cite> the server awaits the
next group commit before returning.</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>The <tt class="docutils literal"><span class="pre">'_id'</span></tt> value (or list of ‘_id’ values) of <cite>doc_or_docs</cite> or
<tt class="docutils literal"><span class="pre">[None]</span></tt> if manipulate is <tt class="docutils literal"><span class="pre">False</span></tt> and the documents passed
as <cite>doc_or_docs</cite> do not include an ‘_id’ field.</li>
</ul>
</td>
</tr>
</tbody>
</table>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last"><cite>continue_on_error</cite> requires server version <strong>>= 1.9.1</strong></p>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.1: </span>Support for continue_on_error.</p>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 1.8: </span>Support for passing <cite>getLastError</cite> options as keyword
arguments.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 1.1: </span>Bulk insert works with any iterable</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/insert" name="pymongo.collection.Collection.insert"><em>insert</em></a></p>
</div>
</dd></dl>
<dl class="method">
<dt id="pymongo.collection.Collection.save">
<tt class="descname">save</tt><big>(</big><em>to_save</em><span class="optional">[</span>, <em>manipulate=True</em><span class="optional">[</span>, <em>safe=None</em><span class="optional">[</span>, <em>check_keys=True</em><span class="optional">[</span>, <em>**kwargs</em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><big>)</big><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>If <cite>to_save</cite> already has an <tt class="docutils literal"><span class="pre">"_id"</span></tt> then an <a class="reference internal" href="#pymongo.collection.Collection.update" title="pymongo.collection.Collection.update"><tt class="xref py py-meth docutils literal"><span class="pre">update()</span></tt></a>
(upsert) operation is performed and any existing document with
that <tt class="docutils literal"><span class="pre">"_id"</span></tt> is overwritten. Otherwise an <a class="reference internal" href="#pymongo.collection.Collection.insert" title="pymongo.collection.Collection.insert"><tt class="xref py py-meth docutils literal"><span class="pre">insert()</span></tt></a>
operation is performed. In this case if <cite>manipulate</cite> is <tt class="docutils literal"><span class="pre">True</span></tt>
an <tt class="docutils literal"><span class="pre">"_id"</span></tt> will be added to <cite>to_save</cite> and this method returns
the <tt class="docutils literal"><span class="pre">"_id"</span></tt> of the saved document. If <cite>manipulate</cite> is <tt class="docutils literal"><span class="pre">False</span></tt>
the <tt class="docutils literal"><span class="pre">"_id"</span></tt> will be added by the server but this method will
return <tt class="docutils literal"><span class="pre">None</span></tt>.</p>
<p>Raises <tt class="xref py py-class docutils literal"><span class="pre">TypeError</span></tt> if <cite>to_save</cite> is not an instance of
<tt class="xref py py-class docutils literal"><span class="pre">dict</span></tt>.</p>
<p>Write concern options can be passed as keyword arguments, overriding
any global defaults. Valid options include w=<int/string>,
wtimeout=<int>, j=<bool>, or fsync=<bool>. See the parameter list below
for a detailed explanation of these options.</p>
<p>By default an acknowledgment is requested from the server that the
save was successful, raising <a class="reference internal" href="errors.html#pymongo.errors.OperationFailure" title="pymongo.errors.OperationFailure"><tt class="xref py py-class docutils literal"><span class="pre">OperationFailure</span></tt></a>
if an error occurred. <strong>Passing ``w=0`` disables write acknowledgement
and all other write concern options.</strong></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 simple">
<li><cite>to_save</cite>: the document to be saved</li>
<li><cite>manipulate</cite> (optional): manipulate the document before
saving it?</li>
<li><cite>safe</cite> (optional): <strong>DEPRECATED</strong> - Use <cite>w</cite> instead.</li>
<li><cite>check_keys</cite> (optional): check if keys start with ‘$’ or
contain ‘.’, raising <a class="reference internal" href="errors.html#pymongo.errors.InvalidName" title="pymongo.errors.InvalidName"><tt class="xref py py-class docutils literal"><span class="pre">InvalidName</span></tt></a>
in either case.</li>
<li><cite>w</cite> (optional): (integer or string) If this is a replica set, write
operations will block until they have been replicated to the
specified number or tagged set of servers. <cite>w=<int></cite> always includes
the replica set primary (e.g. w=3 means write to the primary and wait
until replicated to <strong>two</strong> secondaries). <strong>Passing w=0 disables
write acknowledgement and all other write concern options.</strong></li>
<li><cite>wtimeout</cite> (optional): (integer) Used in conjunction with <cite>w</cite>.
Specify a value in milliseconds to control how long to wait for
write propagation to complete. If replication does not complete in
the given timeframe, a timeout exception is raised.</li>
<li><cite>j</cite> (optional): If <tt class="docutils literal"><span class="pre">True</span></tt> block until write operations have been
committed to the journal. Ignored if the server is running without
journaling.</li>
<li><cite>fsync</cite> (optional): If <tt class="docutils literal"><span class="pre">True</span></tt> force the database to fsync all
files before returning. When used with <cite>j</cite> the server awaits the
next group commit before returning.</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>The <tt class="docutils literal"><span class="pre">'_id'</span></tt> value of <cite>to_save</cite> or <tt class="docutils literal"><span class="pre">[None]</span></tt> if <cite>manipulate</cite> is
<tt class="docutils literal"><span class="pre">False</span></tt> and <cite>to_save</cite> has no ‘_id’ field.</li>
</ul>
</td>
</tr>
</tbody>
</table>
<div class="versionadded">
<p><span class="versionmodified">New in version 1.8: </span>Support for passing <cite>getLastError</cite> options as keyword
arguments.</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/insert" name="pymongo.collection.Collection.save"><em>insert</em></a></p>
</div>
</dd></dl>
<dl class="method">
<dt id="pymongo.collection.Collection.update">
<tt class="descname">update</tt><big>(</big><em>spec</em>, <em>document</em><span class="optional">[</span>, <em>upsert=False</em><span class="optional">[</span>, <em>manipulate=False</em><span class="optional">[</span>, <em>safe=None</em><span class="optional">[</span>, <em>multi=False</em><span class="optional">[</span>, <em>check_keys=True</em><span class="optional">[</span>, <em>**kwargs</em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><big>)</big><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>Raises <tt class="xref py py-class docutils literal"><span class="pre">TypeError</span></tt> if either <cite>spec</cite> or <cite>document</cite> is
not an instance of <tt class="docutils literal"><span class="pre">dict</span></tt> or <cite>upsert</cite> is not an instance of
<tt class="docutils literal"><span class="pre">bool</span></tt>.</p>
<p>Write concern options can be passed as keyword arguments, overriding
any global defaults. Valid options include w=<int/string>,
wtimeout=<int>, j=<bool>, or fsync=<bool>. See the parameter list below
for a detailed explanation of these options.</p>
<p>By default an acknowledgment is requested from the server that the
update was successful, raising <a class="reference internal" href="errors.html#pymongo.errors.OperationFailure" title="pymongo.errors.OperationFailure"><tt class="xref py py-class docutils literal"><span class="pre">OperationFailure</span></tt></a>
if an error occurred. <strong>Passing ``w=0`` disables write acknowledgement
and all other write concern options.</strong></p>
<p>There are many useful <a class="reference external" href="http://www.mongodb.org/display/DOCS/Updating">update modifiers</a> which can be used
when performing updates. For example, here we use the
<tt class="docutils literal"><span class="pre">"$set"</span></tt> modifier to modify some fields in a matching
document:</p>
<div class="highlight-python"><div class="highlight"><pre><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">insert</span><span class="p">({</span><span class="s">"x"</span><span class="p">:</span> <span class="s">"y"</span><span class="p">,</span> <span class="s">"a"</span><span class="p">:</span> <span class="s">"b"</span><span class="p">})</span>
<span class="go">ObjectId('...')</span>
<span class="gp">>>> </span><span class="nb">list</span><span class="p">(</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="go">[{u'a': u'b', u'x': u'y', u'_id': ObjectId('...')}]</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">update</span><span class="p">({</span><span class="s">"x"</span><span class="p">:</span> <span class="s">"y"</span><span class="p">},</span> <span class="p">{</span><span class="s">"$set"</span><span class="p">:</span> <span class="p">{</span><span class="s">"a"</span><span class="p">:</span> <span class="s">"c"</span><span class="p">}})</span>
<span class="go">{...}</span>
<span class="gp">>>> </span><span class="nb">list</span><span class="p">(</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="go">[{u'a': u'c', u'x': u'y', u'_id': ObjectId('...')}]</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>spec</cite>: a <tt class="docutils literal"><span class="pre">dict</span></tt> or <a class="reference internal" href="../bson/son.html#bson.son.SON" title="bson.son.SON"><tt class="xref py py-class docutils literal"><span class="pre">SON</span></tt></a> instance
specifying elements which must be present for a document
to be updated</li>
<li><cite>document</cite>: a <tt class="docutils literal"><span class="pre">dict</span></tt> or <a class="reference internal" href="../bson/son.html#bson.son.SON" title="bson.son.SON"><tt class="xref py py-class docutils literal"><span class="pre">SON</span></tt></a>
instance specifying the document to be used for the update
or (in the case of an upsert) insert - see docs on MongoDB
<a class="reference external" href="http://www.mongodb.org/display/DOCS/Updating">update modifiers</a></li>
<li><cite>upsert</cite> (optional): perform an upsert if <tt class="docutils literal"><span class="pre">True</span></tt></li>
<li><cite>manipulate</cite> (optional): manipulate the document before
updating? If <tt class="docutils literal"><span class="pre">True</span></tt> all instances of
<a class="reference internal" href="son_manipulator.html#pymongo.son_manipulator.SONManipulator" title="pymongo.son_manipulator.SONManipulator"><tt class="xref py py-mod docutils literal"><span class="pre">SONManipulator</span></tt></a> added to
this <a class="reference internal" href="database.html#pymongo.database.Database" title="pymongo.database.Database"><tt class="xref py py-class docutils literal"><span class="pre">Database</span></tt></a> will be applied
to the document before performing the update.</li>
<li><cite>check_keys</cite> (optional): check if keys in <cite>document</cite> start
with ‘$’ or contain ‘.’, raising
<a class="reference internal" href="errors.html#pymongo.errors.InvalidName" title="pymongo.errors.InvalidName"><tt class="xref py py-class docutils literal"><span class="pre">InvalidName</span></tt></a>. Only applies to
document replacement, not modification through $
operators.</li>
<li><cite>safe</cite> (optional): <strong>DEPRECATED</strong> - Use <cite>w</cite> instead.</li>
<li><cite>multi</cite> (optional): update all documents that match
<cite>spec</cite>, rather than just the first matching document. The
default value for <cite>multi</cite> is currently <tt class="docutils literal"><span class="pre">False</span></tt>, but this
might eventually change to <tt class="docutils literal"><span class="pre">True</span></tt>. It is recommended
that you specify this argument explicitly for all update
operations in order to prepare your code for that change.</li>
<li><cite>w</cite> (optional): (integer or string) If this is a replica set, write
operations will block until they have been replicated to the
specified number or tagged set of servers. <cite>w=<int></cite> always includes
the replica set primary (e.g. w=3 means write to the primary and wait
until replicated to <strong>two</strong> secondaries). <strong>Passing w=0 disables
write acknowledgement and all other write concern options.</strong></li>
<li><cite>wtimeout</cite> (optional): (integer) Used in conjunction with <cite>w</cite>.
Specify a value in milliseconds to control how long to wait for
write propagation to complete. If replication does not complete in
the given timeframe, a timeout exception is raised.</li>
<li><cite>j</cite> (optional): If <tt class="docutils literal"><span class="pre">True</span></tt> block until write operations have been
committed to the journal. Ignored if the server is running without
journaling.</li>
<li><cite>fsync</cite> (optional): If <tt class="docutils literal"><span class="pre">True</span></tt> force the database to fsync all
files before returning. When used with <cite>j</cite> the server awaits the
next group commit before returning.</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>A document (dict) describing the effect of the update or <tt class="docutils literal"><span class="pre">None</span></tt>
if write acknowledgement is disabled.</li>
</ul>
</td>
</tr>
</tbody>
</table>
<div class="versionadded">
<p><span class="versionmodified">New in version 1.8: </span>Support for passing <cite>getLastError</cite> options as keyword
arguments.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 1.4: </span>Return the response to <em>lastError</em> if <cite>safe</cite> is <tt class="docutils literal"><span class="pre">True</span></tt>.</p>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 1.1.1: </span>The <cite>multi</cite> parameter.</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/update" name="pymongo.collection.Collection.update"><em>update</em></a></p>
</div>
</dd></dl>
<dl class="method">
<dt id="pymongo.collection.Collection.remove">
<tt class="descname">remove</tt><big>(</big><span class="optional">[</span><em>spec_or_id=None</em><span class="optional">[</span>, <em>safe=None</em><span class="optional">[</span>, <em>**kwargs</em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><big>)</big><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>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">Calls to <a class="reference internal" href="#pymongo.collection.Collection.remove" title="pymongo.collection.Collection.remove"><tt class="xref py py-meth docutils literal"><span class="pre">remove()</span></tt></a> should be performed with
care, as removed data cannot be restored.</p>
</div>
<p>If <cite>spec_or_id</cite> is <tt class="docutils literal"><span class="pre">None</span></tt>, all documents in this collection
will be removed. This is not equivalent to calling
<a class="reference internal" href="database.html#pymongo.database.Database.drop_collection" title="pymongo.database.Database.drop_collection"><tt class="xref py py-meth docutils literal"><span class="pre">drop_collection()</span></tt></a>, however,
as indexes will not be removed.</p>
<p>Write concern options can be passed as keyword arguments, overriding
any global defaults. Valid options include w=<int/string>,
wtimeout=<int>, j=<bool>, or fsync=<bool>. See the parameter list below
for a detailed explanation of these options.</p>
<p>By default an acknowledgment is requested from the server that the
remove was successful, raising <a class="reference internal" href="errors.html#pymongo.errors.OperationFailure" title="pymongo.errors.OperationFailure"><tt class="xref py py-class docutils literal"><span class="pre">OperationFailure</span></tt></a>
if an error occurred. <strong>Passing ``w=0`` disables write acknowledgement
and all other write concern options.</strong></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 simple">
<li><cite>spec_or_id</cite> (optional): a dictionary specifying the
documents to be removed OR any other type specifying the
value of <tt class="docutils literal"><span class="pre">"_id"</span></tt> for the document to be removed</li>
<li><cite>safe</cite> (optional): <strong>DEPRECATED</strong> - Use <cite>w</cite> instead.</li>
<li><cite>w</cite> (optional): (integer or string) If this is a replica set, write
operations will block until they have been replicated to the
specified number or tagged set of servers. <cite>w=<int></cite> always includes
the replica set primary (e.g. w=3 means write to the primary and wait
until replicated to <strong>two</strong> secondaries). <strong>Passing w=0 disables
write acknowledgement and all other write concern options.</strong></li>
<li><cite>wtimeout</cite> (optional): (integer) Used in conjunction with <cite>w</cite>.
Specify a value in milliseconds to control how long to wait for
write propagation to complete. If replication does not complete in
the given timeframe, a timeout exception is raised.</li>
<li><cite>j</cite> (optional): If <tt class="docutils literal"><span class="pre">True</span></tt> block until write operations have been
committed to the journal. Ignored if the server is running without
journaling.</li>
<li><cite>fsync</cite> (optional): If <tt class="docutils literal"><span class="pre">True</span></tt> force the database to fsync all
files before returning. When used with <cite>j</cite> the server awaits the
next group commit before returning.</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>A document (dict) describing the effect of the remove or <tt class="docutils literal"><span class="pre">None</span></tt>
if write acknowledgement is disabled.</li>
</ul>
</td>
</tr>
</tbody>
</table>
<div class="versionadded">
<p><span class="versionmodified">New in version 1.8: </span>Support for passing <cite>getLastError</cite> options as keyword arguments.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 1.7: </span>Accept any type other than a <tt class="docutils literal"><span class="pre">dict</span></tt>
instance for removal by <tt class="docutils literal"><span class="pre">"_id"</span></tt>, not just
<a class="reference internal" href="../bson/objectid.html#bson.objectid.ObjectId" title="bson.objectid.ObjectId"><tt class="xref py py-class docutils literal"><span class="pre">ObjectId</span></tt></a> instances.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 1.4: </span>Return the response to <em>lastError</em> if <cite>safe</cite> is <tt class="docutils literal"><span class="pre">True</span></tt>.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 1.2: </span>The <cite>spec_or_id</cite> parameter is now optional. If it is
not specified <em>all</em> documents in the collection will be
removed.</p>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 1.1: </span>The <cite>safe</cite> parameter.</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/remove" name="pymongo.collection.Collection.remove"><em>remove</em></a></p>
</div>
</dd></dl>
<dl class="method">
<dt id="pymongo.collection.Collection.drop">
<tt class="descname">drop</tt><big>(</big><big>)</big><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"><tt class="xref py py-meth docutils literal"><span class="pre">drop_collection()</span></tt></a>.</p>
<p>The following two calls are equivalent:</p>
<div class="highlight-python"><div class="highlight"><pre><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="s">"foo"</span><span class="p">)</span>
</pre></div>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 1.8.</span></p>
</div>
</dd></dl>
<dl class="method">
<dt id="pymongo.collection.Collection.find">
<tt class="descname">find</tt><big>(</big><span class="optional">[</span><em>spec=None</em><span class="optional">[</span>, <em>fields=None</em><span class="optional">[</span>, <em>skip=0</em><span class="optional">[</span>, <em>limit=0</em><span class="optional">[</span>, <em>timeout=True</em><span class="optional">[</span>, <em>snapshot=False</em><span class="optional">[</span>, <em>tailable=False</em><span class="optional">[</span>, <em>sort=None</em><span class="optional">[</span>, <em>max_scan=None</em><span class="optional">[</span>, <em>as_class=None</em><span class="optional">[</span>, <em>slave_okay=False</em><span class="optional">[</span>, <em>await_data=False</em><span class="optional">[</span>, <em>partial=False</em><span class="optional">[</span>, <em>manipulate=True</em><span class="optional">[</span>, <em>read_preference=ReadPreference.PRIMARY</em><span class="optional">[</span>, <em>exhaust=False</em><span class="optional">[</span>, <em>**kwargs</em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#pymongo.collection.Collection.find" title="Permalink to this definition">¶</a></dt>
<dd><p>Query the database.</p>
<p>The <cite>spec</cite> argument is a prototype document that all results
must match. For example:</p>
<div class="highlight-python"><div class="highlight"><pre><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="s">"hello"</span><span class="p">:</span> <span class="s">"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>fields</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 <tt class="xref py py-class docutils literal"><span class="pre">TypeError</span></tt> 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"><tt class="xref py py-class docutils literal"><span class="pre">Cursor</span></tt></a> corresponding to this query.</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>spec</cite> (optional): a SON object specifying elements which
must be present for a document to be included in the
result set</li>
<li><cite>fields</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>fields</cite> is a list “_id” will
always be returned. Use a dict to exclude fields from
the result (e.g. fields={‘_id’: False}).</li>
<li><cite>skip</cite> (optional): the number of documents to omit (from
the start of the result set) when returning the results</li>
<li><cite>limit</cite> (optional): the maximum number of results to
return</li>
<li><cite>timeout</cite> (optional): if True (the default), any returned
cursor is closed by the server after 10 minutes of
inactivity. If set to False, the returned cursor will never
time out on the server. Care should be taken to ensure that
cursors with timeout turned off are properly closed.</li>
<li><cite>snapshot</cite> (optional): if True, snapshot mode will be used
for this query. Snapshot mode assures no duplicates are
returned, or objects missed, which were present at both
the start and end of the query’s execution. For details,
see the <a class="reference external" href="http://dochub.mongodb.org/core/snapshot">snapshot documentation</a>.</li>
<li><cite>tailable</cite> (optional): the result of this find call will
be a tailable cursor - tailable cursors aren’t closed when
the last data is retrieved but are kept open and the
cursors location marks the final document’s 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><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"><tt class="xref py py-meth docutils literal"><span class="pre">sort()</span></tt></a> for details.</li>
<li><cite>max_scan</cite> (optional): limit the number of documents
examined when performing the query</li>
<li><cite>as_class</cite> (optional): class to use for documents in the
query result (default is
<a class="reference internal" href="mongo_client.html#pymongo.mongo_client.MongoClient.document_class" title="pymongo.mongo_client.MongoClient.document_class"><tt class="xref py py-attr docutils literal"><span class="pre">document_class</span></tt></a>)</li>
<li><cite>slave_okay</cite> (optional): if True, allows this query to
be run against a replica secondary.</li>
<li><cite>await_data</cite> (optional): if True, the server will block for
some extra time before returning, waiting for more data to
return. Ignored if <cite>tailable</cite> is False.</li>
<li><cite>partial</cite> (optional): if True, mongos will return partial
results if some shards are down instead of returning an error.</li>
<li><cite>manipulate</cite>: (optional): If True (the default), apply any
outgoing SON manipulators before returning.</li>
<li><cite>network_timeout</cite> (optional): specify a timeout to use for
this query, which will override the
<a class="reference internal" href="mongo_client.html#pymongo.mongo_client.MongoClient" title="pymongo.mongo_client.MongoClient"><tt class="xref py py-class docutils literal"><span class="pre">MongoClient</span></tt></a>-level default</li>
<li><cite>read_preference</cite> (optional): The read preference for
this query.</li>
<li><cite>tag_sets</cite> (optional): The tag sets for this query.</li>
<li><cite>secondary_acceptable_latency_ms</cite> (optional): Any replica-set
member whose ping time is within secondary_acceptable_latency_ms of
the nearest member may accept reads. Default 15 milliseconds.
<strong>Ignored by mongos</strong> and must be configured on the command line.
See the <a class="reference external" href="http://docs.mongodb.org/manual/reference/mongos/#cmdoption-mongos--localThreshold">localThreshold</a> option for more information.</li>
<li><cite>exhaust</cite> (optional): If <tt class="docutils literal"><span class="pre">True</span></tt> create an “exhaust” cursor.
MongoDB will stream batched results to the client without waiting
for the client to request each batch, reducing latency.</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 the <cite>exhaust</cite>
parameter:</p>
<blockquote class="last">
<div><p>1. The <cite>exhaust</cite> and <cite>limit</cite> options are incompatible and can
not be used together.</p>
<p>2. The <cite>exhaust</cite> option is not supported by mongos and can not be
used with a sharded cluster.</p>
<p>3. A <a class="reference internal" href="cursor.html#pymongo.cursor.Cursor" title="pymongo.cursor.Cursor"><tt class="xref py py-class docutils literal"><span class="pre">Cursor</span></tt></a> instance created with the
<cite>exhaust</cite> option requires an exclusive <tt class="xref py py-class docutils literal"><span class="pre">socket</span></tt>
connection to MongoDB. If the <a class="reference internal" href="cursor.html#pymongo.cursor.Cursor" title="pymongo.cursor.Cursor"><tt class="xref py py-class docutils literal"><span class="pre">Cursor</span></tt></a> is
discarded without being completely iterated the underlying
<tt class="xref py py-class docutils literal"><span class="pre">socket</span></tt> connection will be closed and discarded
without being returned to the connection pool.</p>
<p>4. A <a class="reference internal" href="cursor.html#pymongo.cursor.Cursor" title="pymongo.cursor.Cursor"><tt class="xref py py-class docutils literal"><span class="pre">Cursor</span></tt></a> instance created with the
<cite>exhaust</cite> option in a <a class="reference internal" href="../../examples/requests.html"><em>request</em></a> <strong>must</strong>
be completely iterated before executing any other operation.</p>
<p>5. The <cite>network_timeout</cite> option is ignored when using the
<cite>exhaust</cite> option.</p>
</div></blockquote>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The <cite>manipulate</cite> parameter may default to False in
a future release.</p>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The <cite>max_scan</cite> parameter requires server
version <strong>>= 1.5.1</strong></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="versionadded">
<p><span class="versionmodified">New in version 1.11+: </span>The <cite>await_data</cite>, <cite>partial</cite>, and <cite>manipulate</cite> parameters.</p>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 1.8: </span>The <cite>network_timeout</cite> parameter.</p>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 1.7: </span>The <cite>sort</cite>, <cite>max_scan</cite> and <cite>as_class</cite> parameters.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 1.7: </span>The <cite>fields</cite> parameter can now be a dict or any iterable in
addition to a list.</p>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 1.1: </span>The <cite>tailable</cite> parameter.</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">
<tt class="descname">find_one</tt><big>(</big><span class="optional">[</span><em>spec_or_id=None</em><span class="optional">[</span>, <em>*args</em><span class="optional">[</span>, <em>**kwargs</em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><big>)</big><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"><tt class="xref py py-meth docutils literal"><span class="pre">find()</span></tt></a> are also valid arguments for
<a class="reference internal" href="#pymongo.collection.Collection.find_one" title="pymongo.collection.Collection.find_one"><tt class="xref py py-meth docutils literal"><span class="pre">find_one()</span></tt></a>, although any <cite>limit</cite> argument will be
ignored. Returns a single document, or <tt class="docutils literal"><span class="pre">None</span></tt> if no matching
document is found.</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>spec_or_id</cite> (optional): a dictionary specifying
the query to be performed OR any other type to be used as
the value for a query for <tt class="docutils literal"><span class="pre">"_id"</span></tt>.</li>
<li><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"><tt class="xref py py-meth docutils literal"><span class="pre">find()</span></tt></a>.</li>
<li><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"><tt class="xref py py-meth docutils literal"><span class="pre">find()</span></tt></a>.</li>
</ul>
</td>
</tr>
</tbody>
</table>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 1.7: </span>Allow passing any of the arguments that are valid for
<a class="reference internal" href="#pymongo.collection.Collection.find" title="pymongo.collection.Collection.find"><tt class="xref py py-meth docutils literal"><span class="pre">find()</span></tt></a>.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 1.7: </span>Accept any type other than a <tt class="docutils literal"><span class="pre">dict</span></tt>
instance as an <tt class="docutils literal"><span class="pre">"_id"</span></tt> query, not just
<a class="reference internal" href="../bson/objectid.html#bson.objectid.ObjectId" title="bson.objectid.ObjectId"><tt class="xref py py-class docutils literal"><span class="pre">ObjectId</span></tt></a> instances.</p>
</div>
</dd></dl>
<dl class="method">
<dt id="pymongo.collection.Collection.count">
<tt class="descname">count</tt><big>(</big><big>)</big><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>To get the number of documents matching a specific query use
<a class="reference internal" href="cursor.html#pymongo.cursor.Cursor.count" title="pymongo.cursor.Cursor.count"><tt class="xref py py-meth docutils literal"><span class="pre">pymongo.cursor.Cursor.count()</span></tt></a>.</p>
</dd></dl>
<dl class="method">
<dt id="pymongo.collection.Collection.create_index">
<tt class="descname">create_index</tt><big>(</big><em>key_or_list</em>, <em>cache_for=300</em>, <em>**kwargs</em><big>)</big><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 <tt class="xref py py-class docutils literal"><span class="pre">basestring</span></tt>
(<tt class="xref py py-class docutils literal"><span class="pre">str</span></tt> in python 3), and the directions must be one of
(<a class="reference internal" href="#pymongo.ASCENDING" title="pymongo.ASCENDING"><tt class="xref py py-data docutils literal"><span class="pre">ASCENDING</span></tt></a>, <a class="reference internal" href="#pymongo.DESCENDING" title="pymongo.DESCENDING"><tt class="xref py py-data docutils literal"><span class="pre">DESCENDING</span></tt></a>,
<a class="reference internal" href="#pymongo.GEO2D" title="pymongo.GEO2D"><tt class="xref py py-data docutils literal"><span class="pre">GEO2D</span></tt></a>). Returns the name of the created index.</p>
<p>To create a single key index on the key <tt class="docutils literal"><span class="pre">'mike'</span></tt> we just use
a string argument:</p>
<div class="highlight-python"><div class="highlight"><pre><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="s">"mike"</span><span class="p">)</span>
</pre></div>
</div>
<p>For a compound index on <tt class="docutils literal"><span class="pre">'mike'</span></tt> descending and <tt class="docutils literal"><span class="pre">'eliot'</span></tt>
ascending we need to use a list of tuples:</p>
<div class="highlight-python"><div class="highlight"><pre><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="s">"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="s">"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. Valid options include:</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>: should this index guarantee uniqueness?</li>
<li><cite>dropDups</cite> or <cite>drop_dups</cite>: should we drop duplicates</li>
<li><cite>background</cite>: if this index should be created in the
background</li>
<li><cite>sparse</cite>: if True, omit from the index any documents that lack
the indexed field</li>
<li><cite>bucketSize</cite> or <cite>bucket_size</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"><tt class="xref py py-data docutils literal"><span class="pre">GEO2D</span></tt></a>
index</li>
<li><cite>max</cite>: maximum value for keys in a <a class="reference internal" href="#pymongo.GEO2D" title="pymongo.GEO2D"><tt class="xref py py-data docutils literal"><span class="pre">GEO2D</span></tt></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>
</ul>
</div></blockquote>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last"><cite>expireAfterSeconds</cite> requires server version <strong>>= 2.1.2</strong></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>key_or_list</cite>: a single key or a list of (key, direction)
pairs specifying the index to create</li>
<li><cite>cache_for</cite> (optional): time window (in seconds) during which
this index will be recognized by subsequent calls to
<a class="reference internal" href="#pymongo.collection.Collection.ensure_index" title="pymongo.collection.Collection.ensure_index"><tt class="xref py py-meth docutils literal"><span class="pre">ensure_index()</span></tt></a> - see documentation for
<a class="reference internal" href="#pymongo.collection.Collection.ensure_index" title="pymongo.collection.Collection.ensure_index"><tt class="xref py py-meth docutils literal"><span class="pre">ensure_index()</span></tt></a> for details</li>
<li><cite>**kwargs</cite> (optional): any additional index creation
options (see the above list) should be passed as keyword
arguments</li>
<li><cite>ttl</cite> (deprecated): Use <cite>cache_for</cite> instead.</li>
</ul>
</td>
</tr>
</tbody>
</table>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 2.3: </span>The <cite>ttl</cite> parameter has been deprecated to avoid confusion with
TTL collections. Use <cite>cache_for</cite> instead.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 2.2: </span>Removed deprecated argument: deprecated_unique</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 1.5.1: </span>Accept kwargs to support all index creation options.</p>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 1.5: </span>The <cite>name</cite> parameter.</p>
</div>
<div class="admonition seealso">
<p class="first admonition-title">See also</p>
<p class="last"><a class="reference internal" href="#pymongo.collection.Collection.ensure_index" title="pymongo.collection.Collection.ensure_index"><tt class="xref py py-meth docutils literal"><span class="pre">ensure_index()</span></tt></a></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.ensure_index">
<tt class="descname">ensure_index</tt><big>(</big><em>key_or_list</em>, <em>cache_for=300</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#pymongo.collection.Collection.ensure_index" title="Permalink to this definition">¶</a></dt>
<dd><p>Ensures that an index exists 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 <tt class="xref py py-class docutils literal"><span class="pre">basestring</span></tt>
(<tt class="xref py py-class docutils literal"><span class="pre">str</span></tt> in python 3), and the direction(s) must be one of
(<a class="reference internal" href="#pymongo.ASCENDING" title="pymongo.ASCENDING"><tt class="xref py py-data docutils literal"><span class="pre">ASCENDING</span></tt></a>, <a class="reference internal" href="#pymongo.DESCENDING" title="pymongo.DESCENDING"><tt class="xref py py-data docutils literal"><span class="pre">DESCENDING</span></tt></a>,
<a class="reference internal" href="#pymongo.GEO2D" title="pymongo.GEO2D"><tt class="xref py py-data docutils literal"><span class="pre">GEO2D</span></tt></a>). See <a class="reference internal" href="#pymongo.collection.Collection.create_index" title="pymongo.collection.Collection.create_index"><tt class="xref py py-meth docutils literal"><span class="pre">create_index()</span></tt></a> for a detailed
example.</p>
<p>Unlike <a class="reference internal" href="#pymongo.collection.Collection.create_index" title="pymongo.collection.Collection.create_index"><tt class="xref py py-meth docutils literal"><span class="pre">create_index()</span></tt></a>, which attempts to create an index
unconditionally, <a class="reference internal" href="#pymongo.collection.Collection.ensure_index" title="pymongo.collection.Collection.ensure_index"><tt class="xref py py-meth docutils literal"><span class="pre">ensure_index()</span></tt></a> takes advantage of some
caching within the driver such that it only attempts to create
indexes that might not already exist. When an index is created
(or ensured) by PyMongo it is “remembered” for <cite>cache_for</cite>
seconds. Repeated calls to <a class="reference internal" href="#pymongo.collection.Collection.ensure_index" title="pymongo.collection.Collection.ensure_index"><tt class="xref py py-meth docutils literal"><span class="pre">ensure_index()</span></tt></a> within that
time limit will be lightweight - they will not attempt to
actually create the index.</p>
<p>Care must be taken when the database is being accessed through
multiple clients at once. If an index is created using
this client and deleted using another, any call to
<a class="reference internal" href="#pymongo.collection.Collection.ensure_index" title="pymongo.collection.Collection.ensure_index"><tt class="xref py py-meth docutils literal"><span class="pre">ensure_index()</span></tt></a> within the cache window will fail to
re-create the missing index.</p>
<p>Returns the name of the created index if an index is actually
created. Returns <tt class="docutils literal"><span class="pre">None</span></tt> if the index already exists.</p>
<p>All optional index creation parameters should be passed as
keyword arguments to this method. Valid options include:</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>: should this index guarantee uniqueness?</li>
<li><cite>dropDups</cite> or <cite>drop_dups</cite>: should we drop duplicates
during index creation when creating a unique index?</li>
<li><cite>background</cite>: if this index should be created in the
background</li>
<li><cite>sparse</cite>: if True, omit from the index any documents that lack
the indexed field</li>
<li><cite>bucketSize</cite> or <cite>bucket_size</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"><tt class="xref py py-data docutils literal"><span class="pre">GEO2D</span></tt></a>
index</li>
<li><cite>max</cite>: maximum value for keys in a <a class="reference internal" href="#pymongo.GEO2D" title="pymongo.GEO2D"><tt class="xref py py-data docutils literal"><span class="pre">GEO2D</span></tt></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>
</ul>
</div></blockquote>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last"><cite>expireAfterSeconds</cite> requires server version <strong>>= 2.1.2</strong></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>key_or_list</cite>: a single key or a list of (key, direction)
pairs specifying the index to create</li>
<li><cite>cache_for</cite> (optional): time window (in seconds) during which
this index will be recognized by subsequent calls to
<a class="reference internal" href="#pymongo.collection.Collection.ensure_index" title="pymongo.collection.Collection.ensure_index"><tt class="xref py py-meth docutils literal"><span class="pre">ensure_index()</span></tt></a></li>
<li><cite>**kwargs</cite> (optional): any additional index creation
options (see the above list) should be passed as keyword
arguments</li>
<li><cite>ttl</cite> (deprecated): Use <cite>cache_for</cite> instead.</li>
</ul>
</td>
</tr>
</tbody>
</table>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 2.3: </span>The <cite>ttl</cite> parameter has been deprecated to avoid confusion with
TTL collections. Use <cite>cache_for</cite> instead.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 2.2: </span>Removed deprecated argument: deprecated_unique</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 1.5.1: </span>Accept kwargs to support all index creation options.</p>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 1.5: </span>The <cite>name</cite> parameter.</p>
</div>
<div class="admonition seealso">
<p class="first admonition-title">See also</p>
<p class="last"><a class="reference internal" href="#pymongo.collection.Collection.create_index" title="pymongo.collection.Collection.create_index"><tt class="xref py py-meth docutils literal"><span class="pre">create_index()</span></tt></a></p>
</div>
</dd></dl>
<dl class="method">
<dt id="pymongo.collection.Collection.drop_index">
<tt class="descname">drop_index</tt><big>(</big><em>index_or_name</em><big>)</big><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. <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"><tt class="xref py py-meth docutils literal"><span class="pre">create_index()</span></tt></a> or
<a class="reference internal" href="#pymongo.collection.Collection.ensure_index" title="pymongo.collection.Collection.ensure_index"><tt class="xref py py-meth docutils literal"><span class="pre">ensure_index()</span></tt></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>
</dd></dl>
<dl class="method">
<dt id="pymongo.collection.Collection.drop_indexes">
<tt class="descname">drop_indexes</tt><big>(</big><big>)</big><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>
</dd></dl>
<dl class="method">
<dt id="pymongo.collection.Collection.reindex">
<tt class="descname">reindex</tt><big>(</big><big>)</big><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="versionadded">
<p><span class="versionmodified">New in version 1.11+.</span></p>
</div>
</dd></dl>
<dl class="method">
<dt id="pymongo.collection.Collection.index_information">
<tt class="descname">index_information</tt><big>(</big><big>)</big><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, <tt class="docutils literal"><span class="pre">"key"</span></tt> which
is a list of (key, direction) pairs specifying the index (as
passed to create_index()). It will also contain any other
information in <cite>system.indexes</cite>, except for the <tt class="docutils literal"><span class="pre">"ns"</span></tt> and
<tt class="docutils literal"><span class="pre">"name"</span></tt> keys, which are cleaned. Example output might look
like this:</p>
<div class="highlight-python"><div class="highlight"><pre><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="s">"x"</span><span class="p">,</span> <span class="n">unique</span><span class="o">=</span><span class="bp">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>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 1.7: </span>The values in the resultant dictionary are now dictionaries
themselves, whose <tt class="docutils literal"><span class="pre">"key"</span></tt> item contains the list that was
the value in previous versions of PyMongo.</p>
</div>
</dd></dl>
<dl class="method">
<dt id="pymongo.collection.Collection.options">
<tt class="descname">options</tt><big>(</big><big>)</big><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"><tt class="xref py py-meth docutils literal"><span class="pre">create_collection()</span></tt></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.aggregate">
<tt class="descname">aggregate</tt><big>(</big><em>pipeline</em>, <em>**kwargs</em><big>)</big><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>With <a class="reference internal" href="mongo_replica_set_client.html#pymongo.mongo_replica_set_client.MongoReplicaSetClient" title="pymongo.mongo_replica_set_client.MongoReplicaSetClient"><tt class="xref py py-class docutils literal"><span class="pre">MongoReplicaSetClient</span></tt></a>
or <a class="reference internal" href="master_slave_connection.html#pymongo.master_slave_connection.MasterSlaveConnection" title="pymongo.master_slave_connection.MasterSlaveConnection"><tt class="xref py py-class docutils literal"><span class="pre">MasterSlaveConnection</span></tt></a>,
if the <cite>read_preference</cite> attribute of this instance is not set to
<tt class="xref py py-attr docutils literal"><span class="pre">pymongo.read_preferences.ReadPreference.PRIMARY</span></tt> or the
(deprecated) <cite>slave_okay</cite> attribute of this instance is set to <cite>True</cite>
the <a class="reference external" href="http://docs.mongodb.org/manual/applications/aggregation">aggregate command</a> will be sent to a secondary or slave.</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>pipeline</cite>: a single command or list of aggregation commands</li>
<li><cite>**kwargs</cite>: send arbitrary parameters to the aggregate command</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.1.0</strong>.</p>
</div>
<p>With server version <strong>>= 2.5.1</strong>, pass
<tt class="docutils literal"><span class="pre">cursor={}</span></tt> to retrieve unlimited aggregation results
with a <a class="reference internal" href="cursor.html#pymongo.cursor.Cursor" title="pymongo.cursor.Cursor"><tt class="xref py py-class docutils literal"><span class="pre">Cursor</span></tt></a>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">pipeline</span> <span class="o">=</span> <span class="p">[{</span><span class="s">'$project'</span><span class="p">:</span> <span class="p">{</span><span class="s">'name'</span><span class="p">:</span> <span class="p">{</span><span class="s">'$toUpper'</span><span class="p">:</span> <span class="s">'$name'</span><span class="p">}}}]</span>
<span class="n">cursor</span> <span class="o">=</span> <span class="n">collection</span><span class="o">.</span><span class="n">aggregate</span><span class="p">(</span><span class="n">pipeline</span><span class="p">,</span> <span class="n">cursor</span><span class="o">=</span><span class="p">{})</span>
<span class="k">for</span> <span class="n">doc</span> <span class="ow">in</span> <span class="n">cursor</span><span class="p">:</span>
<span class="k">print</span> <span class="n">doc</span>
</pre></div>
</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>
</dd></dl>
<dl class="method">
<dt id="pymongo.collection.Collection.group">
<tt class="descname">group</tt><big>(</big><em>key</em>, <em>condition</em>, <em>initial</em>, <em>reduce</em>, <em>finalize=None</em><big>)</big><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><tt class="docutils literal"><span class="pre">None</span></tt> to use the entire document as a key.</li>
<li>A <tt class="xref py py-class docutils literal"><span class="pre">list</span></tt> of keys (each a <tt class="xref py py-class docutils literal"><span class="pre">basestring</span></tt>
(<tt class="xref py py-class docutils literal"><span class="pre">str</span></tt> in python 3)) to group by.</li>
<li>A <tt class="xref py py-class docutils literal"><span class="pre">basestring</span></tt> (<tt class="xref py py-class docutils literal"><span class="pre">str</span></tt> in python 3), or
<a class="reference internal" href="../bson/code.html#bson.code.Code" title="bson.code.Code"><tt class="xref py py-class docutils literal"><span class="pre">Code</span></tt></a> instance containing a JavaScript
function to be applied to each document, returning the key
to group by.</li>
</ul>
</div></blockquote>
<p>With <a class="reference internal" href="mongo_replica_set_client.html#pymongo.mongo_replica_set_client.MongoReplicaSetClient" title="pymongo.mongo_replica_set_client.MongoReplicaSetClient"><tt class="xref py py-class docutils literal"><span class="pre">MongoReplicaSetClient</span></tt></a>
or <a class="reference internal" href="master_slave_connection.html#pymongo.master_slave_connection.MasterSlaveConnection" title="pymongo.master_slave_connection.MasterSlaveConnection"><tt class="xref py py-class docutils literal"><span class="pre">MasterSlaveConnection</span></tt></a>,
if the <cite>read_preference</cite> attribute of this instance is not set to
<tt class="xref py py-attr docutils literal"><span class="pre">pymongo.read_preferences.ReadPreference.PRIMARY</span></tt> or
<tt class="xref py py-attr docutils literal"><span class="pre">pymongo.read_preferences.ReadPreference.PRIMARY_PREFERRED</span></tt>, or
the (deprecated) <cite>slave_okay</cite> attribute of this instance is set to
<cite>True</cite>, the group command will be sent to a secondary or slave.</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"><tt class="xref py py-meth docutils literal"><span class="pre">find()</span></tt></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>
</ul>
</td>
</tr>
</tbody>
</table>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 2.2: </span>Removed deprecated argument: command</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 1.4: </span>The <cite>key</cite> argument can now be <tt class="docutils literal"><span class="pre">None</span></tt> or a JavaScript function,
in addition to a <tt class="xref py py-class docutils literal"><span class="pre">list</span></tt> of keys.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 1.3: </span>The <cite>command</cite> argument now defaults to <tt class="docutils literal"><span class="pre">True</span></tt> and is deprecated.</p>
</div>
</dd></dl>
<dl class="method">
<dt id="pymongo.collection.Collection.rename">
<tt class="descname">rename</tt><big>(</big><em>new_name</em>, <em>**kwargs</em><big>)</big><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 <tt class="xref py py-class docutils literal"><span class="pre">TypeError</span></tt> if
<cite>new_name</cite> is not an instance of <tt class="xref py py-class docutils literal"><span class="pre">basestring</span></tt>
(<tt class="xref py py-class docutils literal"><span class="pre">str</span></tt> in python 3). Raises <a class="reference internal" href="errors.html#pymongo.errors.InvalidName" title="pymongo.errors.InvalidName"><tt class="xref py py-class docutils literal"><span class="pre">InvalidName</span></tt></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): any additional rename options
should be passed as keyword arguments
(i.e. <tt class="docutils literal"><span class="pre">dropTarget=True</span></tt>)</li>
</ul>
</td>
</tr>
</tbody>
</table>
<div class="versionadded">
<p><span class="versionmodified">New in version 1.7: </span>support for accepting keyword arguments for rename options</p>
</div>
</dd></dl>
<dl class="method">
<dt id="pymongo.collection.Collection.distinct">
<tt class="descname">distinct</tt><big>(</big><em>key</em><big>)</big><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 <tt class="xref py py-class docutils literal"><span class="pre">TypeError</span></tt> if <cite>key</cite> is not an instance of
<tt class="xref py py-class docutils literal"><span class="pre">basestring</span></tt> (<tt class="xref py py-class docutils literal"><span class="pre">str</span></tt> in python 3).</p>
<p>To get the distinct values for a key in the result set of a
query use <a class="reference internal" href="cursor.html#pymongo.cursor.Cursor.distinct" title="pymongo.cursor.Cursor.distinct"><tt class="xref py py-meth docutils literal"><span class="pre">distinct()</span></tt></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 key for which we want to get the distinct values</li>
</ul>
</td>
</tr>
</tbody>
</table>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Requires server version <strong>>= 1.1.0</strong></p>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 1.1.1.</span></p>
</div>
</dd></dl>
<dl class="method">
<dt id="pymongo.collection.Collection.map_reduce">
<tt class="descname">map_reduce</tt><big>(</big><em>map</em>, <em>reduce</em>, <em>out</em>, <em>full_response=False</em>, <em>**kwargs</em><big>)</big><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 <tt class="docutils literal"><span class="pre">False</span></tt> (default) returns a
<a class="reference internal" href="#pymongo.collection.Collection" title="pymongo.collection.Collection"><tt class="xref py py-class docutils literal"><span class="pre">Collection</span></tt></a> instance containing
the results of the operation. Otherwise, returns the full
response from the server to the <a class="reference external" href="http://www.mongodb.org/display/DOCS/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://www.mongodb.org/display/DOCS/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"><tt class="xref py py-class docutils literal"><span class="pre">SON</span></tt></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 <tt class="docutils literal"><span class="pre">True</span></tt>, 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://www.mongodb.org/display/DOCS/MapReduce">map reduce command</a> may be passed as keyword arguments to this
helper method, e.g.:</p>
<div class="highlight-python"><div class="highlight"><pre><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="nb">reduce</span><span class="p">,</span> <span class="s">"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">Requires server version <strong>>= 1.1.1</strong></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"><em>Aggregation Examples</em></a></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="versionchanged">
<p><span class="versionmodified">Changed in version 1.11+: </span>DEPRECATED The merge_output and reduce_output parameters.</p>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 1.2.</span></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">
<tt class="descname">inline_map_reduce</tt><big>(</big><em>map</em>, <em>reduce</em>, <em>full_response=False</em>, <em>**kwargs</em><big>)</big><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 <tt class="docutils literal"><span class="pre">False</span></tt> (default) returns the
result documents in a list. Otherwise, returns the full
response from the server to the <a class="reference external" href="http://www.mongodb.org/display/DOCS/MapReduce">map reduce command</a>.</p>
<p>With <a class="reference internal" href="mongo_replica_set_client.html#pymongo.mongo_replica_set_client.MongoReplicaSetClient" title="pymongo.mongo_replica_set_client.MongoReplicaSetClient"><tt class="xref py py-class docutils literal"><span class="pre">MongoReplicaSetClient</span></tt></a>
or <a class="reference internal" href="master_slave_connection.html#pymongo.master_slave_connection.MasterSlaveConnection" title="pymongo.master_slave_connection.MasterSlaveConnection"><tt class="xref py py-class docutils literal"><span class="pre">MasterSlaveConnection</span></tt></a>,
if the <cite>read_preference</cite> attribute of this instance is not set to
<tt class="xref py py-attr docutils literal"><span class="pre">pymongo.read_preferences.ReadPreference.PRIMARY</span></tt> or
<tt class="xref py py-attr docutils literal"><span class="pre">pymongo.read_preferences.ReadPreference.PRIMARY_PREFERRED</span></tt>, or
the (deprecated) <cite>slave_okay</cite> attribute of this instance is set to
<cite>True</cite>, the inline map reduce will be run on a secondary or slave.</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 <tt class="docutils literal"><span class="pre">True</span></tt>, 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://www.mongodb.org/display/DOCS/MapReduce">map reduce command</a> may be passed as keyword arguments to this
helper method, e.g.:</p>
<div class="highlight-python"><div class="highlight"><pre><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="nb">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="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Requires server version <strong>>= 1.7.4</strong></p>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 1.10.</span></p>
</div>
</dd></dl>
<dl class="method">
<dt id="pymongo.collection.Collection.find_and_modify">
<tt class="descname">find_and_modify</tt><big>(</big><em>query={}</em>, <em>update=None</em>, <em>upsert=False</em>, <em>sort=None</em>, <em>full_response=False</em>, <em>**kwargs</em><big>)</big><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>This is a thin wrapper around the <a class="reference external" href="http://dochub.mongodb.org/core/findAndModify">findAndModify</a> command. The
positional arguments are designed to match the first three arguments
to <a class="reference internal" href="#pymongo.collection.Collection.update" title="pymongo.collection.Collection.update"><tt class="xref py py-meth docutils literal"><span class="pre">update()</span></tt></a> however most options should be passed as named
parameters. Either <cite>update</cite> or <cite>remove</cite> arguments are required, all
others are optional.</p>
<p>Returns either the object before or after modification based on <cite>new</cite>
parameter. If no objects match the <cite>query</cite> and <cite>upsert</cite> is false,
returns <tt class="docutils literal"><span class="pre">None</span></tt>. If upserting and <cite>new</cite> is false, returns <tt class="docutils literal"><span class="pre">{}</span></tt>.</p>
<p>If the full_response parameter is <tt class="docutils literal"><span class="pre">True</span></tt>, the return value will be
the entire response object from the server, including the ‘ok’ and
‘lastErrorObject’ fields, rather than just the modified object.
This is useful mainly because the ‘lastErrorObject’ document holds
information about the command’s execution.</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>query</cite>: filter for the update (default <tt class="docutils literal"><span class="pre">{}</span></tt>)</li>
<li><cite>update</cite>: see second argument to <a class="reference internal" href="#pymongo.collection.Collection.update" title="pymongo.collection.Collection.update"><tt class="xref py py-meth docutils literal"><span class="pre">update()</span></tt></a> (no default)</li>
<li><cite>upsert</cite>: insert if object doesn’t exist (default <tt class="docutils literal"><span class="pre">False</span></tt>)</li>
<li><cite>sort</cite>: 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"><tt class="xref py py-meth docutils literal"><span class="pre">sort()</span></tt></a>
for details.</li>
<li><cite>full_response</cite>: return the entire response object from the
server (default <tt class="docutils literal"><span class="pre">False</span></tt>)</li>
<li><cite>remove</cite>: remove rather than updating (default <tt class="docutils literal"><span class="pre">False</span></tt>)</li>
<li><cite>new</cite>: return updated rather than original object
(default <tt class="docutils literal"><span class="pre">False</span></tt>)</li>
<li><cite>fields</cite>: see second argument to <a class="reference internal" href="#pymongo.collection.Collection.find" title="pymongo.collection.Collection.find"><tt class="xref py py-meth docutils literal"><span class="pre">find()</span></tt></a> (default all)</li>
<li><cite>**kwargs</cite>: any other options the <a class="reference external" href="http://dochub.mongodb.org/core/findAndModify">findAndModify</a> command
supports can be passed here.</li>
</ul>
</td>
</tr>
</tbody>
</table>
<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/findAndModify" name="pymongo.collection.Collection.find_and_modify"><em>findAndModify</em></a></p>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Requires server version <strong>>= 1.3.0</strong></p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 2.5: </span>Added the optional full_response parameter</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 2.4: </span>Deprecated the use of mapping types for the sort parameter</p>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 1.10.</span></p>
</div>
</dd></dl>
<dl class="attribute">
<dt id="pymongo.collection.Collection.slave_okay">
<tt class="descname">slave_okay</tt><a class="headerlink" href="#pymongo.collection.Collection.slave_okay" title="Permalink to this definition">¶</a></dt>
<dd><p>DEPRECATED. Use <a class="reference internal" href="#pymongo.collection.Collection.read_preference" title="pymongo.collection.Collection.read_preference"><tt class="xref py py-attr docutils literal"><span class="pre">read_preference</span></tt></a> instead.</p>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 2.1: </span>Deprecated slave_okay.</p>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.0.</span></p>
</div>
</dd></dl>
<dl class="attribute">
<dt id="pymongo.collection.Collection.safe">
<tt class="descname">safe</tt><a class="headerlink" href="#pymongo.collection.Collection.safe" title="Permalink to this definition">¶</a></dt>
<dd><p><strong>DEPRECATED:</strong> Use the ‘w’ <a class="reference internal" href="#pymongo.collection.Collection.write_concern" title="pymongo.collection.Collection.write_concern"><tt class="xref py py-attr docutils literal"><span class="pre">write_concern</span></tt></a> option instead.</p>
<p>Use getlasterror with every write operation?</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.0.</span></p>
</div>
</dd></dl>
<dl class="method">
<dt id="pymongo.collection.Collection.get_lasterror_options">
<tt class="descname">get_lasterror_options</tt><big>(</big><big>)</big><a class="headerlink" href="#pymongo.collection.Collection.get_lasterror_options" title="Permalink to this definition">¶</a></dt>
<dd><p>DEPRECATED: Use <a class="reference internal" href="#pymongo.collection.Collection.write_concern" title="pymongo.collection.Collection.write_concern"><tt class="xref py py-attr docutils literal"><span class="pre">write_concern</span></tt></a> instead.</p>
<p>Returns a dict of the getlasterror options set on this instance.</p>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 2.4: </span>Deprecated get_lasterror_options.</p>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.0.</span></p>
</div>
</dd></dl>
<dl class="method">
<dt id="pymongo.collection.Collection.set_lasterror_options">
<tt class="descname">set_lasterror_options</tt><big>(</big><em>**kwargs</em><big>)</big><a class="headerlink" href="#pymongo.collection.Collection.set_lasterror_options" title="Permalink to this definition">¶</a></dt>
<dd><p>DEPRECATED: Use <a class="reference internal" href="#pymongo.collection.Collection.write_concern" title="pymongo.collection.Collection.write_concern"><tt class="xref py py-attr docutils literal"><span class="pre">write_concern</span></tt></a> instead.</p>
<p>Set getlasterror options for this instance.</p>
<p>Valid options include j=<bool>, w=<int/string>, wtimeout=<int>,
and fsync=<bool>. Implies safe=True.</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><dl class="first docutils">
<dt><cite>**kwargs</cite>: Options should be passed as keyword</dt>
<dd><p class="first last">arguments (e.g. w=2, fsync=True)</p>
</dd>
</dl>
</li>
</ul>
</td>
</tr>
</tbody>
</table>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 2.4: </span>Deprecated set_lasterror_options.</p>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.0.</span></p>
</div>
</dd></dl>
<dl class="method">
<dt id="pymongo.collection.Collection.unset_lasterror_options">
<tt class="descname">unset_lasterror_options</tt><big>(</big><em>*options</em><big>)</big><a class="headerlink" href="#pymongo.collection.Collection.unset_lasterror_options" title="Permalink to this definition">¶</a></dt>
<dd><p>DEPRECATED: Use <a class="reference internal" href="#pymongo.collection.Collection.write_concern" title="pymongo.collection.Collection.write_concern"><tt class="xref py py-attr docutils literal"><span class="pre">write_concern</span></tt></a> instead.</p>
<p>Unset getlasterror options for this instance.</p>
<p>If no options are passed unsets all getlasterror options.
This does not set <cite>safe</cite> to False.</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>*options</cite>: The list of options to unset.</li>
</ul>
</td>
</tr>
</tbody>
</table>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 2.4: </span>Deprecated unset_lasterror_options.</p>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.0.</span></p>
</div>
</dd></dl>
</dd></dl>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar">
<div class="sphinxsidebarwrapper">
<h4>Previous topic</h4>
<p class="topless"><a href="database.html"
title="previous chapter"><tt class="docutils literal"><span class="pre">database</span></tt> – Database level operations</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="cursor.html"
title="next chapter"><tt class="docutils literal"><span class="pre">cursor</span></tt> – Tools for iterating over MongoDB query results</a></p>
<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 id="searchbox" style="display: none">
<h3>Quick search</h3>
<form class="search" action="../../search.html" method="get">
<input type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
<p class="searchtip" style="font-size: 90%">
Enter search terms or a module, class or function name.
</p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related">
<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="cursor.html" title="cursor – Tools for iterating over MongoDB query results"
>next</a> |</li>
<li class="right" >
<a href="database.html" title="database – Database level operations"
>previous</a> |</li>
<li><a href="../../index.html">PyMongo 2.6.3 documentation</a> »</li>
<li><a href="../index.html" >API Documentation</a> »</li>
<li><a href="index.html" ><tt class="docutils literal"><span class="pre">pymongo</span></tt> – Python driver for MongoDB</a> »</li>
</ul>
</div>
<div class="footer">
© Copyright 2008 - 2012, 10gen, Inc..
Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.2.
</div>
</body>
</html>
|