/usr/lib/x86_64-linux-gnu/perl5/5.22/MongoDB/MongoClient.pm is in libmongodb-perl 1.2.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 | #
# Copyright 2009-2013 MongoDB, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
package MongoDB::MongoClient;
# ABSTRACT: A connection to a MongoDB server or multi-server deployment
use version;
our $VERSION = 'v1.2.2';
use Moo;
use MongoDB;
use MongoDB::BSON;
use MongoDB::BSON::Binary;
use MongoDB::BSON::Regexp;
use MongoDB::Cursor;
use MongoDB::DBRef;
use MongoDB::Error;
use MongoDB::Op::_Command;
use MongoDB::Op::_FSyncUnlock;
use MongoDB::ReadPreference;
use MongoDB::WriteConcern;
use MongoDB::ReadConcern;
use MongoDB::_Topology;
use MongoDB::_Constants;
use MongoDB::_Credential;
use MongoDB::_URI;
use Digest::MD5;
use Tie::IxHash;
use Time::HiRes qw/usleep/;
use Carp 'carp', 'croak', 'confess';
use Safe::Isa;
use Scalar::Util qw/reftype weaken/;
use boolean;
use Encode;
use Try::Tiny;
use MongoDB::_Types qw(
ArrayOfHashRef
AuthMechanism
BSONCodec
NonNegNum
ReadPrefMode
ReadPreference
);
use Types::Standard qw(
Bool
HashRef
InstanceOf
Undef
Int
Num
Str
Maybe
);
use namespace::clean -except => 'meta';
#--------------------------------------------------------------------------#
# deprecated global variables
#--------------------------------------------------------------------------#
# These are used to configure a BSON codec options if none is provided.
$MongoDB::BSON::looks_like_number = 0;
$MongoDB::BSON::char = '$';
#--------------------------------------------------------------------------#
# public attributes
#
# Of these, only host, port and bson_codec are set without regard for
# connection string options. The rest are built lazily in BUILD so that
# option precedence can be resolved.
#--------------------------------------------------------------------------#
#pod =attr host
#pod
#pod The C<host> attribute specifies either a single server to connect to (as
#pod C<hostname> or C<hostname:port>), or else a L<connection string URI|/CONNECTION
#pod STRING URI> with a seed list of one or more servers plus connection options.
#pod
#pod Defaults to the connection string URI C<mongodb://localhost:27017>.
#pod
#pod For IPv6 support, you must have a recent version of L<IO::Socket::IP>
#pod installed. This module ships with the Perl core since v5.20.0 and is
#pod available on CPAN for older Perls.
#pod
#pod =cut
has host => (
is => 'ro',
isa => Str,
default => 'mongodb://localhost:27017',
);
#pod =attr auth_mechanism
#pod
#pod This attribute determines how the client authenticates with the server.
#pod Valid values are:
#pod
#pod =for :list
#pod * NONE
#pod * DEFAULT
#pod * MONGODB-CR
#pod * MONGODB-X509
#pod * GSSAPI
#pod * PLAIN
#pod * SCRAM-SHA-1
#pod
#pod If not specified, then if no username is provided, it defaults to NONE.
#pod If a username is provided, it is set to DEFAULT, which chooses SCRAM-SHA-1 if
#pod available or MONGODB-CR otherwise.
#pod
#pod This may be set in a connection string with the C<authMechanism> option.
#pod
#pod =cut
has auth_mechanism => (
is => 'lazy',
isa => AuthMechanism,
builder => '_build_auth_mechanism',
);
sub _build_auth_mechanism {
my ($self) = @_;
my $default =
$self->sasl ? $self->sasl_mechanism
: $self->username ? 'DEFAULT'
: 'NONE';
return $self->__uri_or_else(
u => 'authmechanism',
e => 'auth_mechanism',
d => $default,
);
}
#pod =attr auth_mechanism_properties
#pod
#pod This is an optional hash reference of authentication mechanism specific properties.
#pod See L</AUTHENTICATION> for details.
#pod
#pod This may be set in a connection string with the C<authMechanismProperties>
#pod option. If given, the value must be key/value pairs joined with a ":".
#pod Multiple pairs must be separated by a comma. If ": or "," appear in a key or
#pod value, they must be URL encoded.
#pod
#pod =cut
has auth_mechanism_properties => (
is => 'lazy',
isa => HashRef,
builder => '_build_auth_mechanism_properties',
);
sub _build_auth_mechanism_properties {
my ($self) = @_;
return $self->__uri_or_else(
u => 'authmechanismproperties',
e => 'auth_mechanism_properties',
d => {},
);
}
#pod =attr bson_codec
#pod
#pod An object that provides the C<encode_one> and C<decode_one> methods, such as
#pod from L<MongoDB::BSON>. It may be initialized with a hash reference that will
#pod be coerced into a new L<MongoDB::BSON> object.
#pod
#pod If not provided, one will be generated as follows:
#pod
#pod MongoDB::BSON->new(
#pod dbref_callback => sub { return MongoDB::DBRef->new(shift) },
#pod dt_type => $client->dt_type,
#pod prefer_numeric => $MongoDB::BSON::looks_like_number || 0,
#pod (
#pod $MongoDB::BSON::char ne '$' ?
#pod ( op_char => $MongoDB::BSON::char ) : ()
#pod ),
#pod );
#pod
#pod This will inflate all DBRefs to L<MongoDB::DBRef> objects, set C<dt_type>
#pod based on the client's C<db_type> accessor, and set the C<prefer_numeric>
#pod and C<op_char> attributes based on the deprecated legacy global variables.
#pod
#pod =cut
has bson_codec => (
is => 'lazy',
isa => BSONCodec,
coerce => BSONCodec->coercion,
writer => '_set_bson_codec',
builder => '_build_bson_codec',
);
# skipping op_char unless $MongoDB::BSON::char is not '$' is an optimization
sub _build_bson_codec {
my ($self) = @_;
return MongoDB::BSON->new(
dbref_callback => sub { return MongoDB::DBRef->new(shift) },
dt_type => $self->dt_type,
prefer_numeric => $MongoDB::BSON::looks_like_number || 0,
( $MongoDB::BSON::char ne '$' ? ( op_char => $MongoDB::BSON::char ) : () ),
);
}
#pod =attr connect_timeout_ms
#pod
#pod This attribute specifies the amount of time in milliseconds to wait for a
#pod new connection to a server.
#pod
#pod The default is 10,000 ms.
#pod
#pod If set to a negative value, connection operations will block indefinitely
#pod until the server replies or until the operating system TCP/IP stack gives
#pod up (e.g. if the name can't resolve or there is no process listening on the
#pod target host/port).
#pod
#pod A zero value polls the socket during connection and is thus likely to fail
#pod except when talking to a local process (and perhaps even then).
#pod
#pod This may be set in a connection string with the C<connectTimeoutMS> option.
#pod
#pod =cut
has connect_timeout_ms => (
is => 'lazy',
isa => Num,
builder => '_build_connect_timeout_ms',
);
sub _build_connect_timeout_ms {
my ($self) = @_;
return $self->__uri_or_else(
u => 'connecttimeoutms',
e => 'connect_timeout_ms',
d => $self->timeout, # deprecated legacy attribute as default
);
}
#pod =attr db_name
#pod
#pod Optional. If an L</auth_mechanism> requires a database for authentication,
#pod this attribute will be used. Otherwise, it will be ignored. Defaults to
#pod "admin".
#pod
#pod This may be provided in the L<connection string URI|/CONNECTION STRING URI> as
#pod a path between the authority and option parameter sections. For example, to
#pod authenticate against the "admin" database (showing a configuration option only
#pod for illustration):
#pod
#pod mongodb://localhost/admin?readPreference=primary
#pod
#pod =cut
has db_name => (
is => 'lazy',
isa => Str,
builder => '_build_db_name',
);
sub _build_db_name {
my ($self) = @_;
return __string( $self->_uri->db_name, $self->_deferred->{db_name} );
}
#pod =attr heartbeat_frequency_ms
#pod
#pod The time in milliseconds (non-negative) between scans of all servers to
#pod check if they are up and update their latency. Defaults to 60,000 ms.
#pod
#pod This may be set in a connection string with the C<heartbeatFrequencyMS> option.
#pod
#pod =cut
has heartbeat_frequency_ms => (
is => 'lazy',
isa => NonNegNum,
builder => '_build_heartbeat_frequency_ms',
);
sub _build_heartbeat_frequency_ms {
my ($self) = @_;
return $self->__uri_or_else(
u => 'heartbeatfrequencyms',
e => 'heartbeat_frequency_ms',
d => 60000,
);
}
#pod =attr j
#pod
#pod If true, the client will block until write operations have been committed to the
#pod server's journal. Prior to MongoDB 2.6, this option was ignored if the server was
#pod running without journaling. Starting with MongoDB 2.6, write operations will fail
#pod if this option is used when the server is running without journaling.
#pod
#pod This may be set in a connection string with the C<journal> option as the
#pod strings 'true' or 'false'.
#pod
#pod =cut
has j => (
is => 'lazy',
isa => Bool,
builder => '_build_j',
);
sub _build_j {
my ($self) = @_;
return $self->__uri_or_else(
u => 'journal',
e => 'j',
d => 0,
);
}
#pod =attr local_threshold_ms
#pod
#pod The width of the 'latency window': when choosing between multiple suitable
#pod servers for an operation, the acceptable delta in milliseconds
#pod (non-negative) between shortest and longest average round-trip times.
#pod Servers within the latency window are selected randomly.
#pod
#pod Set this to "0" to always select the server with the shortest average round
#pod trip time. Set this to a very high value to always randomly choose any known
#pod server.
#pod
#pod Defaults to 15 ms.
#pod
#pod See L</SERVER SELECTION> for more details.
#pod
#pod This may be set in a connection string with the C<localThresholdMS> option.
#pod
#pod =cut
has local_threshold_ms => (
is => 'lazy',
isa => NonNegNum,
builder => '_build_local_threshold_ms',
);
sub _build_local_threshold_ms {
my ($self) = @_;
return $self->__uri_or_else(
u => 'localthresholdms',
e => 'local_threshold_ms',
d => 15,
);
}
#pod =attr max_time_ms
#pod
#pod Specifies the maximum amount of time in (non-negative) milliseconds that the
#pod server should use for working on a database command. Defaults to 0, which disables
#pod this feature. Make sure this value is shorter than C<socket_timeout_ms>.
#pod
#pod B<Note>: this will only be used for server versions 2.6 or greater, as that
#pod was when the C<$maxTimeMS> meta-operator was introduced.
#pod
#pod You are B<strongly> encouraged to set this variable if you know your
#pod environment has MongoDB 2.6 or later, as getting a definitive error response
#pod from the server is vastly preferred over a getting a network socket timeout.
#pod
#pod This may be set in a connection string with the C<maxTimeMS> option.
#pod
#pod =cut
has max_time_ms => (
is => 'lazy',
isa => NonNegNum,
builder => '_build_max_time_ms',
);
sub _build_max_time_ms {
my ($self) = @_;
return $self->__uri_or_else(
u => 'maxtimems',
e => 'max_time_ms',
d => 0,
);
}
#pod =attr password
#pod
#pod If an L</auth_mechanism> requires a password, this attribute will be
#pod used. Otherwise, it will be ignored.
#pod
#pod This may be provided in the L<connection string URI|/CONNECTION STRING URI> as
#pod a C<username:password> pair in the leading portion of the authority section
#pod before a C<@> character. For example, to authenticate as user "mulder" with
#pod password "trustno1":
#pod
#pod mongodb://mulder:trustno1@localhost
#pod
#pod If the username or password have a ":" or "@" in it, they must be URL encoded.
#pod An empty password still requires a ":" character.
#pod
#pod =cut
has password => (
is => 'lazy',
isa => Str,
builder => '_build_password',
);
sub _build_password {
my ($self) = @_;
return
defined( $self->_uri->password ) ? $self->_uri->password
: defined( $self->_deferred->{password} ) ? $self->_deferred->{password}
: '';
}
#pod =attr port
#pod
#pod If a network port is not specified as part of the C<host> attribute, this
#pod attribute provides the port to use. It defaults to 27107.
#pod
#pod =cut
has port => (
is => 'ro',
isa => Int,
default => 27017,
);
#pod =attr read_pref_mode
#pod
#pod The read preference mode determines which server types are candidates
#pod for a read operation. Valid values are:
#pod
#pod =for :list
#pod * primary
#pod * primaryPreferred
#pod * secondary
#pod * secondaryPreferred
#pod * nearest
#pod
#pod For core documentation on read preference see
#pod L<http://docs.mongodb.org/manual/core/read-preference/>.
#pod
#pod This may be set in a connection string with the C<readPreference> option.
#pod
#pod =cut
has read_pref_mode => (
is => 'lazy',
isa => ReadPrefMode,
coerce => ReadPrefMode->coercion,
builder => '_build_read_pref_mode',
);
sub _build_read_pref_mode {
my ($self) = @_;
return $self->__uri_or_else(
u => 'readpreference',
e => 'read_pref_mode',
d => 'primary',
);
}
#pod =attr read_pref_tag_sets
#pod
#pod The C<read_pref_tag_sets> parameter is an ordered list of tag sets used to
#pod restrict the eligibility of servers, such as for data center awareness. It
#pod must be an array reference of hash references.
#pod
#pod The application of C<read_pref_tag_sets> varies depending on the
#pod C<read_pref_mode> parameter. If the C<read_pref_mode> is 'primary', then
#pod C<read_pref_tag_sets> must not be supplied.
#pod
#pod For core documentation on read preference see
#pod L<http://docs.mongodb.org/manual/core/read-preference/>.
#pod
#pod This may be set in a connection string with the C<readPreferenceTags> option.
#pod If given, the value must be key/value pairs joined with a ":". Multiple pairs
#pod must be separated by a comma. If ": or "," appear in a key or value, they must
#pod be URL encoded. The C<readPreferenceTags> option may appear more than once, in
#pod which case each document will be added to the tag set list.
#pod
#pod =cut
has read_pref_tag_sets => (
is => 'lazy',
isa => ArrayOfHashRef,
coerce => ArrayOfHashRef->coercion,
builder => '_build_read_pref_tag_sets',
);
sub _build_read_pref_tag_sets {
my ($self) = @_;
return $self->__uri_or_else(
u => 'readpreferencetags',
e => 'read_pref_tag_sets',
d => [ {} ],
);
}
#pod =attr replica_set_name
#pod
#pod Specifies the replica set name to connect to. If this string is non-empty,
#pod then the topology is treated as a replica set and all server replica set
#pod names must match this or they will be removed from the topology.
#pod
#pod This may be set in a connection string with the C<replicaSet> option.
#pod
#pod =cut
has replica_set_name => (
is => 'lazy',
isa => Str,
builder => '_build_replica_set_name',
);
sub _build_replica_set_name {
my ($self) = @_;
return $self->__uri_or_else(
u => 'replicaset',
e => 'replica_set_name',
d => '',
);
}
#pod =attr server_selection_timeout_ms
#pod
#pod This attribute specifies the amount of time in milliseconds to wait for a
#pod suitable server to be available for a read or write operation. If no
#pod server is available within this time period, an exception will be thrown.
#pod
#pod The default is 30,000 ms.
#pod
#pod See L</SERVER SELECTION> for more details.
#pod
#pod This may be set in a connection string with the C<serverSelectionTimeoutMS>
#pod option.
#pod
#pod =cut
has server_selection_timeout_ms => (
is => 'lazy',
isa => Num,
builder => '_build_server_selection_timeout_ms',
);
sub _build_server_selection_timeout_ms {
my ($self) = @_;
return $self->__uri_or_else(
u => 'serverselectiontimeoutms',
e => 'server_selection_timeout_ms',
d => 30000,
);
}
#pod =attr server_selection_try_once
#pod
#pod This attribute controls whether the client will make only a single attempt
#pod to find a suitable server for a read or write operation. The default is true.
#pod
#pod When true, the client will B<not> use the C<server_selection_timeout_ms>.
#pod Instead, if the topology information is stale and needs to be checked or
#pod if no suitable server is available, the client will make a single
#pod scan of all known servers to try to find a suitable one.
#pod
#pod When false, the client will continually scan known servers until a suitable
#pod server is found or the C<serverSelectionTimeoutMS> is reached.
#pod
#pod See L</SERVER SELECTION> for more details.
#pod
#pod This may be set in a connection string with the C<serverSelectionTryOnce>
#pod option.
#pod
#pod =cut
has server_selection_try_once => (
is => 'lazy',
isa => Bool,
builder => '_build_server_selection_try_once',
);
sub _build_server_selection_try_once {
my ($self) = @_;
return $self->__uri_or_else(
u => 'serverselectiontryonce',
e => 'server_selection_try_once',
d => 1,
);
}
#pod =attr socket_check_interval_ms
#pod
#pod If a socket to a server has not been used in this many milliseconds, an
#pod C<ismaster> command will be issued to check the status of the server before
#pod issuing any reads or writes. Must be non-negative.
#pod
#pod The default is 5,000 ms.
#pod
#pod This may be set in a connection string with the C<socketCheckIntervalMS>
#pod option.
#pod
#pod =cut
has socket_check_interval_ms => (
is => 'lazy',
isa => NonNegNum,
builder => '_build_socket_check_interval_ms',
);
sub _build_socket_check_interval_ms {
my ($self) = @_;
return $self->__uri_or_else(
u => 'socketcheckintervalms',
e => 'socket_check_interval_ms',
d => 5000,
);
}
#pod =attr socket_timeout_ms
#pod
#pod This attribute specifies the amount of time in milliseconds to wait for a
#pod reply from the server before issuing a network exception.
#pod
#pod The default is 30,000 ms.
#pod
#pod If set to a negative value, socket operations will block indefinitely
#pod until the server replies or until the operating system TCP/IP stack
#pod gives up.
#pod
#pod A zero value polls the socket for available data and is thus likely to fail
#pod except when talking to a local process (and perhaps even then).
#pod
#pod This may be set in a connection string with the C<socketTimeoutMS> option.
#pod
#pod =cut
has socket_timeout_ms => (
is => 'lazy',
isa => Num,
builder => '_build_socket_timeout_ms',
);
sub _build_socket_timeout_ms {
my ($self) = @_;
return $self->__uri_or_else(
u => 'sockettimeoutms',
e => 'socket_timeout_ms',
d => $self->query_timeout, # deprecated legacy timeout as default
);
}
#pod =attr ssl
#pod
#pod ssl => 1
#pod ssl => \%ssl_options
#pod
#pod This tells the driver that you are connecting to an SSL mongodb instance.
#pod
#pod You must have L<IO::Socket::SSL> 1.42+ and L<Net::SSLeay> 1.49+ installed for
#pod SSL support.
#pod
#pod The C<ssl> attribute takes either a boolean value or a hash reference of
#pod options to pass to IO::Socket::SSL. For example, to set a CA file to validate
#pod the server certificate and set a client certificate for the server to validate,
#pod you could set the attribute like this:
#pod
#pod ssl => {
#pod SSL_ca_file => "/path/to/ca.pem",
#pod SSL_cert_file => "/path/to/client.pem",
#pod }
#pod
#pod If C<SSL_ca_file> is not provided, server certificates are verified against a
#pod default list of CAs, either L<Mozilla::CA> or an operating-system-specific
#pod default CA file. To disable verification, you can use
#pod C<< SSL_verify_mode => 0x00 >>.
#pod
#pod B<You are strongly encouraged to use your own CA file for increased security>.
#pod
#pod Server hostnames are also validated against the CN name in the server
#pod certificate using C<< SSL_verifycn_scheme => 'http' >>. You can use the
#pod scheme 'none' to disable this check.
#pod
#pod B<Disabling certificate or hostname verification is a security risk and is not
#pod recommended>.
#pod
#pod This may be set to the string 'true' or 'false' in a connection string with the
#pod C<ssl> option, which will enable ssl with default configuration. (A future
#pod version of the driver may support customizing ssl via the connection string.)
#pod
#pod =cut
has ssl => (
is => 'lazy',
isa => Bool|HashRef,
builder => '_build_ssl',
);
sub _build_ssl {
my ($self) = @_;
return $self->__uri_or_else(
u => 'ssl',
e => 'ssl',
d => 0,
);
}
#pod =attr username
#pod
#pod Optional username for this client connection. If this field is set, the client
#pod will attempt to authenticate when connecting to servers. Depending on the
#pod L</auth_mechanism>, the L</password> field or other attributes will need to be
#pod set for authentication to succeed.
#pod
#pod This may be provided in the L<connection string URI|/CONNECTION STRING URI> as
#pod a C<username:password> pair in the leading portion of the authority section
#pod before a C<@> character. For example, to authenticate as user "mulder" with
#pod password "trustno1":
#pod
#pod mongodb://mulder:trustno1@localhost
#pod
#pod If the username or password have a ":" or "@" in it, they must be URL encoded.
#pod An empty password still requires a ":" character.
#pod
#pod =cut
has username => (
is => 'lazy',
isa => Str,
builder => '_build_username',
);
sub _build_username {
my ($self) = @_;
return
defined( $self->_uri->username ) ? $self->_uri->username
: defined( $self->_deferred->{username} ) ? $self->_deferred->{username}
: '';
}
#pod =attr w
#pod
#pod The client I<write concern>.
#pod
#pod =over 4
#pod
#pod =item * C<0> Unacknowledged. MongoClient will B<NOT> wait for an acknowledgment that
#pod the server has received and processed the request. Older documentation may refer
#pod to this as "fire-and-forget" mode. This option is not recommended.
#pod
#pod =item * C<1> Acknowledged. This is the default. MongoClient will wait until the
#pod primary MongoDB acknowledges the write.
#pod
#pod =item * C<2> Replica acknowledged. MongoClient will wait until at least two
#pod replicas (primary and one secondary) acknowledge the write. You can set a higher
#pod number for more replicas.
#pod
#pod =item * C<all> All replicas acknowledged.
#pod
#pod =item * C<majority> A majority of replicas acknowledged.
#pod
#pod =back
#pod
#pod In MongoDB v2.0+, you can "tag" replica members. With "tagging" you can
#pod specify a custom write concern For more information see L<Data Center
#pod Awareness|http://docs.mongodb.org/manual/data-center-awareness/>
#pod
#pod This may be set in a connection string with the C<w> option.
#pod
#pod =cut
has w => (
is => 'lazy',
isa => Int|Str|Undef,
builder => '_build_w',
);
sub _build_w {
my ($self) = @_;
return $self->__uri_or_else(
u => 'w',
e => 'w',
d => undef,
);
}
#pod =attr wtimeout
#pod
#pod The number of milliseconds an operation should wait for C<w> secondaries to
#pod replicate it.
#pod
#pod Defaults to 1000 (1 second).
#pod
#pod See C<w> above for more information.
#pod
#pod This may be set in a connection string with the C<wTimeoutMS> option.
#pod
#pod =cut
has wtimeout => (
is => 'lazy',
isa => Int,
builder => '_build_wtimeout',
);
sub _build_wtimeout {
my ($self) = @_;
return $self->__uri_or_else(
u => 'wtimeoutms',
e => 'wtimeout',
d => 1000,
);
}
#pod =attr read_concern_level
#pod
#pod The read concern level determines the consistency level required
#pod of data being read.
#pod
#pod The default level is C<undef>, which means the server will use its configured
#pod default.
#pod
#pod If the level is set to "local", reads will return the latest data a server has
#pod locally.
#pod
#pod Additional levels are storage engine specific. See L<Read
#pod Concern|http://docs.mongodb.org/manual/search/?query=readConcern> in the MongoDB
#pod documentation for more details.
#pod
#pod This may be set in a connection string with the the C<readConcernLevel> option.
#pod
#pod =cut
has read_concern_level => (
is => 'lazy',
isa => Maybe [Str],
builder => '_build_read_concern_level',
);
sub _build_read_concern_level {
my ($self) = @_;
return $self->__uri_or_else(
u => 'readconcernlevel',
e => 'read_concern_level',
d => undef,
);
}
#--------------------------------------------------------------------------#
# deprecated public attributes
#--------------------------------------------------------------------------#
#pod =attr dt_type (DEPRECATED AND READ-ONLY)
#pod
#pod Sets the type of object which is returned for DateTime fields. The default
#pod is L<DateTime>. Other acceptable values are L<DateTime::Tiny>,
#pod L<Time::Moment> and C<undef>. The latter will give you the raw epoch value
#pod (possibly as a floating point value) rather than an object.
#pod
#pod This will be used to construct L</bson_codec> object if one is not provided.
#pod
#pod As this has a one-time effect, it is now read-only to help you detect
#pod code that was trying to change after the fact during program execution.
#pod
#pod For temporary or localized changes, look into overriding the C<bson_codec>
#pod object for a database or collection object.
#pod
#pod =cut
has dt_type => (
is => 'ro',
default => 'DateTime'
);
#pod =attr query_timeout (DEPRECATED AND READ-ONLY)
#pod
#pod # set query timeout to 1 second
#pod my $client = MongoDB::MongoClient->new(query_timeout => 1000);
#pod
#pod This option has been renamed as L</socket_timeout_ms>. If this option is set
#pod and that one is not, this will be used.
#pod
#pod This value is in milliseconds and defaults to 30000.
#pod
#pod =cut
has query_timeout => (
is => 'ro',
isa => Int,
default => 30000,
);
#pod =attr sasl (DEPRECATED)
#pod
#pod If true, the driver will set the authentication mechanism based on the
#pod C<sasl_mechanism> property.
#pod
#pod =cut
has sasl => (
is => 'ro',
isa => Bool,
default => 0
);
#pod =attr sasl_mechanism (DEPRECATED)
#pod
#pod This specifies the SASL mechanism to use for authentication with a MongoDB server.
#pod It has the same valid values as L</auth_mechanism>. The default is GSSAPI.
#pod
#pod =cut
has sasl_mechanism => (
is => 'ro',
isa => AuthMechanism,
default => 'GSSAPI',
);
#pod =attr timeout (DEPRECATED AND READ-ONLY)
#pod
#pod This option has been renamed as L</connect_timeout_ms>. If this option is set
#pod and that one is not, this will be used.
#pod
#pod Connection timeout is in milliseconds. Defaults to C<10000>.
#pod
#pod =cut
has timeout => (
is => 'ro',
isa => Int,
default => 10000,
);
#--------------------------------------------------------------------------#
# computed attributes - these are private and can't be set in the
# constructor, but have a public accessor
#--------------------------------------------------------------------------#
#pod =method read_preference
#pod
#pod Returns a L<MongoDB::ReadPreference> object constructed from
#pod L</read_pref_mode> and L</read_pref_tag_sets>
#pod
#pod B<The use of C<read_preference> as a mutator has been removed.> Read
#pod preference is read-only. If you need a different read preference for
#pod a database or collection, you can specify that in C<get_database> or
#pod C<get_collection>.
#pod
#pod =cut
has _read_preference => (
is => 'lazy',
isa => ReadPreference,
reader => 'read_preference',
init_arg => undef,
builder => '_build__read_preference',
);
sub _build__read_preference {
my ($self) = @_;
return MongoDB::ReadPreference->new(
( $self->read_pref_mode ? ( mode => $self->read_pref_mode ) : () ),
( $self->read_pref_tag_sets ? ( tag_sets => $self->read_pref_tag_sets ) : () ),
);
}
#pod =method write_concern
#pod
#pod Returns a L<MongoDB::WriteConcern> object constructed from L</w>, L</write_concern>
#pod and L</j>.
#pod
#pod =cut
has _write_concern => (
is => 'lazy',
isa => InstanceOf['MongoDB::WriteConcern'],
reader => 'write_concern',
init_arg => undef,
builder => '_build__write_concern',
);
sub _build__write_concern {
my ($self) = @_;
return MongoDB::WriteConcern->new(
( $self->w ? ( w => $self->w ) : () ),
( $self->wtimeout ? ( wtimeout => $self->wtimeout ) : () ),
( $self->j ? ( j => $self->j ) : () ),
);
}
#pod =method read_concern
#pod
#pod Returns a L<MongoDB::ReadConcern> object constructed from
#pod L</read_concern_level>.
#pod
#pod =cut
has _read_concern => (
is => 'lazy',
isa => InstanceOf['MongoDB::ReadConcern'],
reader => 'read_concern',
init_arg => undef,
builder => '_build__read_concern',
);
sub _build__read_concern {
my ($self) = @_;
return MongoDB::ReadConcern->new(
( $self->read_concern_level ?
( level => $self->read_concern_level ) : () ),
);
}
#--------------------------------------------------------------------------#
# private attributes
#--------------------------------------------------------------------------#
# collects constructor options and defer them so precedence can be resolved
# against the _uri options; unlike other private args, this needs a valid
# init argument
has _deferred => (
is => 'ro',
isa => HashRef,
init_arg => '_deferred',
default => sub { {} },
);
#pod =method topology_type
#pod
#pod Returns an enumerated topology type. If the L</replica_set_name> is
#pod set, the value will be either 'ReplicaSetWithPrimary' or 'ReplicaSetNoPrimary'
#pod (if the primary is down or not yet discovered). Without L</replica_set_name>,
#pod the type will be 'Single' if there is only one server in the list of hosts, and
#pod 'Sharded' if there are more than one.
#pod
#pod N.B. A single mongos will have a topology type of 'Single', as that mongos will
#pod be used for all reads and writes, just like a standalone mongod. The 'Sharded'
#pod type indicates a sharded cluster with multiple mongos servers, and reads/writes
#pod will be distributed acc
#pod
#pod =cut
has _topology => (
is => 'lazy',
isa => InstanceOf ['MongoDB::_Topology'],
init_arg => undef,
builder => '_build__topology',
handles => { topology_type => 'type' },
clearer => '_clear__topology',
);
sub _build__topology {
my ($self) = @_;
my $type =
length( $self->replica_set_name ) ? 'ReplicaSetNoPrimary'
: @{ $self->_uri->hostpairs } > 1 ? 'Sharded'
: 'Single';
MongoDB::_Topology->new(
uri => $self->_uri,
bson_codec => $self->bson_codec,
type => $type,
replica_set_name => $self->replica_set_name,
server_selection_timeout_sec => $self->server_selection_timeout_ms / 1000,
server_selection_try_once => $self->server_selection_try_once,
local_threshold_sec => $self->local_threshold_ms / 1000,
heartbeat_frequency_sec => $self->heartbeat_frequency_ms / 1000,
max_wire_version => MAX_WIRE_VERSION,
min_wire_version => MIN_WIRE_VERSION,
credential => $self->_credential,
link_options => {
connect_timeout => $self->connect_timeout_ms >= 0 ? $self->connect_timeout_ms / 1000 : undef,
socket_timeout => $self->socket_timeout_ms >= 0 ? $self->socket_timeout_ms / 1000 : undef,
with_ssl => !!$self->ssl,
( ref( $self->ssl ) eq 'HASH' ? ( SSL_options => $self->ssl ) : () ),
},
);
}
has _credential => (
is => 'lazy',
isa => InstanceOf ['MongoDB::_Credential'],
init_arg => undef,
builder => '_build__credential',
);
sub _build__credential {
my ($self) = @_;
my $mechanism = $self->auth_mechanism;
my $cred = MongoDB::_Credential->new(
mechanism => $mechanism,
mechanism_properties => $self->auth_mechanism_properties,
( $self->username ? ( username => $self->username ) : () ),
( $self->password ? ( password => $self->password ) : () ),
( $self->db_name ? ( source => $self->db_name ) : () ),
);
return $cred;
}
has _uri => (
is => 'lazy',
isa => InstanceOf ['MongoDB::_URI'],
init_arg => undef,
builder => '_build__uri',
);
sub _build__uri {
my ($self) = @_;
if ( $self->host =~ m{^\w+://} ) {
return MongoDB::_URI->new( uri => $self->host );
}
else {
my $uri = $self->host =~ /:\d+$/
? $self->host
: sprintf("%s:%s", map { $self->$_ } qw/host port/ );
return MongoDB::_URI->new( uri => ("mongodb://$uri") );
}
}
#--------------------------------------------------------------------------#
# Constructor customization
#--------------------------------------------------------------------------#
# these attributes are lazy, built from either _uri->options or from
# _config_options captured in BUILDARGS
my @deferred_options = qw(
auth_mechanism
auth_mechanism_properties
connect_timeout_ms
db_name
heartbeat_frequency_ms
j
local_threshold_ms
max_time_ms
read_pref_mode
read_pref_tag_sets
replica_set_name
server_selection_timeout_ms
server_selection_try_once
socket_check_interval_ms
socket_timeout_ms
ssl
username
password
w
wtimeout
read_concern_level
);
around BUILDARGS => sub {
my $orig = shift;
my $class = shift;
my $hr = $class->$orig(@_);
my $deferred = {};
for my $k ( @deferred_options ) {
$deferred->{$k} = delete $hr->{$k}
if exists $hr->{$k};
}
$hr->{_deferred} = $deferred;
return $hr;
};
sub BUILD {
my ($self, $opts) = @_;
my $uri = $self->_uri;
my @addresses = @{ $uri->hostpairs };
# resolve and validate all deferred attributes
$self->$_ for @deferred_options;
# resolve and validate read pref and write concern
$self->read_preference;
$self->write_concern;
# Add error handler to codec if user didn't provide their own
unless ( $self->bson_codec->error_callback ) {
$self->_set_bson_codec(
$self->bson_codec->clone(
error_callback => sub {
my ($msg, $ref, $op) = @_;
if ( $op =~ /^encode/ ) {
MongoDB::DocumentError->throw(
message => $msg,
document => $ref
);
}
else {
MongoDB::DecodingError->throw($msg);
}
},
)
);
}
return;
}
#--------------------------------------------------------------------------#
# helper functions
#--------------------------------------------------------------------------#
sub __uri_or_else {
my ( $self, %spec ) = @_;
my $uri_options = $self->_uri->options;
my $deferred = $self->_deferred;
my ( $u, $e, $default ) = @spec{qw/u e d/};
return
exists $uri_options->{$u} ? $uri_options->{$u}
: exists $deferred->{$e} ? $deferred->{$e}
: $default;
}
sub __string {
local $_;
my ($first) = grep { defined && length } @_;
return $first || '';
}
#--------------------------------------------------------------------------#
# public methods - network communication
#--------------------------------------------------------------------------#
#pod =method connect
#pod
#pod $client->connect;
#pod
#pod Calling this method is unnecessary, as connections are established
#pod automatically as needed. It is kept for backwards compatibility. Calling it
#pod will check all servers in the deployment which ensures a connection to any
#pod that are available.
#pod
#pod See L</reconnect> for a method that is useful when using forks or threads.
#pod
#pod =cut
sub connect {
my ($self) = @_;
$self->_topology->scan_all_servers;
return 1;
}
#pod =method disconnect
#pod
#pod $client->disconnect;
#pod
#pod Drops all connections to servers.
#pod
#pod =cut
sub disconnect {
my ($self) = @_;
$self->_topology->close_all_links;
return 1;
}
#pod =method reconnect
#pod
#pod $client->reconnect;
#pod
#pod This method closes all connections to the server, as if L</disconnect> were
#pod called, and then immediately reconnects. Use this after forking or spawning
#pod off a new thread.
#pod
#pod =cut
sub reconnect {
my ($self) = @_;
$self->_topology->close_all_links;
$self->_topology->scan_all_servers;
return 1;
}
#pod =method topology_status
#pod
#pod $client->topology_status;
#pod $client->topology_status( refresh => 1 );
#pod
#pod Returns a hash reference with server topology information like this:
#pod
#pod {
#pod 'topology_type' => 'ReplicaSetWithPrimary'
#pod 'replica_set_name' => 'foo',
#pod 'last_scan_time' => '1433766895.183241',
#pod 'servers' => [
#pod {
#pod 'address' => 'localhost:50003',
#pod 'ewma_rtt_ms' => '0.223462326',
#pod 'type' => 'RSSecondary'
#pod },
#pod {
#pod 'address' => 'localhost:50437',
#pod 'ewma_rtt_ms' => '0.268435456',
#pod 'type' => 'RSArbiter'
#pod },
#pod {
#pod 'address' => 'localhost:50829',
#pod 'ewma_rtt_ms' => '0.737782272',
#pod 'type' => 'RSPrimary'
#pod }
#pod },
#pod }
#pod
#pod If the 'refresh' argument is true, then the topology will be scanned
#pod to update server data before returning the hash reference.
#pod
#pod =cut
sub topology_status {
my ($self, %opts) = @_;
$self->_topology->scan_all_servers if $opts{refresh};
return $self->_topology->status_struct;
}
#--------------------------------------------------------------------------#
# semi-private methods; these are public but undocumented and their
# semantics might change in future releases
#--------------------------------------------------------------------------#
# Undocumented in old MongoDB::MongoClient; semantics don't translate, but
# best approximation is checking if we can send a command to a server
sub connected {
my ($self) = @_;
return eval { $self->send_admin_command([ismaster => 1]); 1 };
}
sub send_admin_command {
my ( $self, $command, $read_pref ) = @_;
$read_pref = MongoDB::ReadPreference->new(
ref($read_pref) ? $read_pref : ( mode => $read_pref ) )
if $read_pref && ref($read_pref) ne 'MongoDB::ReadPreference';
my $op = MongoDB::Op::_Command->_new(
db_name => 'admin',
query => $command,
query_flags => {},
bson_codec => $self->bson_codec,
read_preference => $read_pref,
);
return $self->send_read_op( $op );
}
# op dispatcher written in highly optimized style
sub send_direct_op {
my ( $self, $op, $address ) = @_;
my ( $link, $result );
( $link = $self->_topology->get_specific_link($address) ), (
eval { ($result) = $op->execute($link); 1 } or do {
my $err = length($@) ? $@ : "caught error, but it was lost in eval unwind";
if ( $err->$_isa("MongoDB::ConnectionError") ) {
$self->_topology->mark_server_unknown( $link->server, $err );
}
elsif ( $err->$_isa("MongoDB::NotMasterError") ) {
$self->_topology->mark_server_unknown( $link->server, $err );
$self->_topology->mark_stale;
}
# regardless of cleanup, rethrow the error
WITH_ASSERTS ? ( confess $err ) : ( die $err );
}
),
return $result;
}
# op dispatcher written in highly optimized style
sub send_write_op {
my ( $self, $op ) = @_;
my ( $link, $result );
( $link = $self->_topology->get_writable_link ), (
eval { ($result) = $op->execute($link); 1 } or do {
my $err = length($@) ? $@ : "caught error, but it was lost in eval unwind";
if ( $err->$_isa("MongoDB::ConnectionError") ) {
$self->_topology->mark_server_unknown( $link->server, $err );
}
elsif ( $err->$_isa("MongoDB::NotMasterError") ) {
$self->_topology->mark_server_unknown( $link->server, $err );
$self->_topology->mark_stale;
}
# regardless of cleanup, rethrow the error
WITH_ASSERTS ? ( confess $err ) : ( die $err );
}
),
return $result;
}
# op dispatcher written in highly optimized style
sub send_read_op {
my ( $self, $op ) = @_;
my ( $link, $type, $result );
( $link = $self->_topology->get_readable_link( $op->read_preference ) ),
( $type = $self->_topology->type ), (
eval { ($result) = $op->execute( $link, $type ); 1 } or do {
my $err = length($@) ? $@ : "caught error, but it was lost in eval unwind";
if ( $err->$_isa("MongoDB::ConnectionError") ) {
$self->_topology->mark_server_unknown( $link->server, $err );
}
elsif ( $err->$_isa("MongoDB::NotMasterError") ) {
$self->_topology->mark_server_unknown( $link->server, $err );
$self->_topology->mark_stale;
}
# regardless of cleanup, rethrow the error
WITH_ASSERTS ? ( confess $err ) : ( die $err );
}
),
return $result;
}
#--------------------------------------------------------------------------#
# database helper methods
#--------------------------------------------------------------------------#
#pod =method database_names
#pod
#pod my @dbs = $client->database_names;
#pod
#pod Lists all databases on the MongoDB server.
#pod
#pod =cut
sub database_names {
my ($self) = @_;
my @databases;
my $max_tries = 3;
for my $try ( 1 .. $max_tries ) {
last if try {
my $output = $self->send_admin_command([ listDatabases => 1 ])->output;
if (ref($output) eq 'HASH' && exists $output->{databases}) {
@databases = map { $_->{name} } @{ $output->{databases} };
}
return 1;
} catch {
if ( $_->$_isa("MongoDB::DatabaseError" ) ) {
return if $_->result->output->{code} == CANT_OPEN_DB_IN_READ_LOCK() || $try < $max_tries;
}
die $_;
};
}
return @databases;
}
#pod =method get_database, db
#pod
#pod my $database = $client->get_database('foo');
#pod my $database = $client->get_database('foo', $options);
#pod my $database = $client->db('foo', $options);
#pod
#pod Returns a L<MongoDB::Database> instance for the database with the given
#pod C<$name>.
#pod
#pod It takes an optional hash reference of options that are passed to the
#pod L<MongoDB::Database> constructor.
#pod
#pod The C<db> method is an alias for C<get_database>.
#pod
#pod =cut
sub get_database {
my ( $self, $database_name, $options ) = @_;
return MongoDB::Database->new(
read_preference => $self->read_preference,
write_concern => $self->write_concern,
read_concern => $self->read_concern,
bson_codec => $self->bson_codec,
max_time_ms => $self->max_time_ms,
( $options ? %$options : () ),
# not allowed to be overridden by options
_client => $self,
name => $database_name,
);
}
{ no warnings 'once'; *db = \&get_database }
#pod =method get_namespace, ns
#pod
#pod my $collection = $client->get_namespace('test.foo');
#pod my $collection = $client->get_namespace('test.foo', $options);
#pod my $collection = $client->ns('test.foo', $options);
#pod
#pod Returns a L<MongoDB::Collection> instance for the given namespace.
#pod The namespace has both the database name and the collection name
#pod separated with a dot character.
#pod
#pod This is a quick way to get a collection object if you don't need
#pod the database object separately.
#pod
#pod It takes an optional hash reference of options that are passed to the
#pod L<MongoDB::Collection> constructor. The intermediate L<MongoDB::Database>
#pod object will be created with default options.
#pod
#pod The C<ns> method is an alias for C<get_namespace>.
#pod
#pod =cut
sub get_namespace {
my ( $self, $ns, $options ) = @_;
MongoDB::UsageError->throw("namespace requires a string argument")
unless defined($ns) && length($ns);
my ( $db, $coll ) = split /\./, $ns, 2;
MongoDB::UsageError->throw("$ns is not a valid namespace")
unless defined($db) && defined($coll);
return $self->db($db)->coll( $coll, $options );
}
{ no warnings 'once'; *ns = \&get_namespace }
#pod =method fsync(\%args)
#pod
#pod $client->fsync();
#pod
#pod A function that will forces the server to flush all pending writes to the storage layer.
#pod
#pod The fsync operation is synchronous by default, to run fsync asynchronously, use the following form:
#pod
#pod $client->fsync({async => 1});
#pod
#pod The primary use of fsync is to lock the database during backup operations. This will flush all data to the data storage layer and block all write operations until you unlock the database. Note: you can still read while the database is locked.
#pod
#pod $conn->fsync({lock => 1});
#pod
#pod =cut
sub fsync {
my ($self, $args) = @_;
$args ||= {};
# Pass this in as array-ref to ensure that 'fsync => 1' is the first argument.
return $self->get_database('admin')->run_command([fsync => 1, %$args]);
}
#pod =method fsync_unlock
#pod
#pod $conn->fsync_unlock();
#pod
#pod Unlocks a database server to allow writes and reverses the operation of a $conn->fsync({lock => 1}); operation.
#pod
#pod =cut
sub fsync_unlock {
my ($self) = @_;
my $op = MongoDB::Op::_FSyncUnlock->_new(
db_name => 'admin',
client => $self,
bson_codec => $self->bson_codec,
);
return $self->send_read_op($op);
}
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
MongoDB::MongoClient - A connection to a MongoDB server or multi-server deployment
=head1 VERSION
version v1.2.2
=head1 SYNOPSIS
use MongoDB; # also loads MongoDB::MongoClient
# connect to localhost:27017
my $client = MongoDB::MongoClient->new;
# connect to specific host and port
my $client = MongoDB::MongoClient->new(
host => "mongodb://mongo.example.com:27017"
);
# connect to a replica set (set name *required*)
my $client = MongoDB::MongoClient->new(
host => "mongodb://mongo1.example.com,mongo2.example.com",
replica_set_name => 'myset',
);
# connect to a replica set with URI (set name *required*)
my $client = MongoDB::MongoClient->new(
host => "mongodb://mongo1.example.com,mongo2.example.com/?replicaSet=myset",
);
my $db = $client->get_database("test");
my $coll = $db->get_collection("people");
$coll->insert({ name => "John Doe", age => 42 });
my @people = $coll->find()->all();
=head1 DESCRIPTION
The C<MongoDB::MongoClient> class represents a client connection to one or
more MongoDB servers.
By default, it connects to a single server running on the local machine
listening on the default port 27017:
# connects to localhost:27017
my $client = MongoDB::MongoClient->new;
It can connect to a database server running anywhere, though:
my $client = MongoDB::MongoClient->new(host => 'example.com:12345');
See the L</"host"> attribute for more options for connecting to MongoDB.
MongoDB can be started in L<authentication
mode|http://docs.mongodb.org/manual/core/authentication/>, which requires
clients to log in before manipulating data. By default, MongoDB does not start
in this mode, so no username or password is required to make a fully functional
connection. To configure the client for authentication, see the
L</AUTHENTICATION> section.
The actual socket connections are lazy and created on demand. When the client
object goes out of scope, all socket will be closed. Note that
L<MongoDB::Database>, L<MongoDB::Collection> and related classes could hold a
reference to the client as well. Only when all references are out of scope
will the sockets be closed.
=head1 ATTRIBUTES
=head2 host
The C<host> attribute specifies either a single server to connect to (as
C<hostname> or C<hostname:port>), or else a L<connection string URI|/CONNECTION
STRING URI> with a seed list of one or more servers plus connection options.
Defaults to the connection string URI C<mongodb://localhost:27017>.
For IPv6 support, you must have a recent version of L<IO::Socket::IP>
installed. This module ships with the Perl core since v5.20.0 and is
available on CPAN for older Perls.
=head2 auth_mechanism
This attribute determines how the client authenticates with the server.
Valid values are:
=over 4
=item *
NONE
=item *
DEFAULT
=item *
MONGODB-CR
=item *
MONGODB-X509
=item *
GSSAPI
=item *
PLAIN
=item *
SCRAM-SHA-1
=back
If not specified, then if no username is provided, it defaults to NONE.
If a username is provided, it is set to DEFAULT, which chooses SCRAM-SHA-1 if
available or MONGODB-CR otherwise.
This may be set in a connection string with the C<authMechanism> option.
=head2 auth_mechanism_properties
This is an optional hash reference of authentication mechanism specific properties.
See L</AUTHENTICATION> for details.
This may be set in a connection string with the C<authMechanismProperties>
option. If given, the value must be key/value pairs joined with a ":".
Multiple pairs must be separated by a comma. If ": or "," appear in a key or
value, they must be URL encoded.
=head2 bson_codec
An object that provides the C<encode_one> and C<decode_one> methods, such as
from L<MongoDB::BSON>. It may be initialized with a hash reference that will
be coerced into a new L<MongoDB::BSON> object.
If not provided, one will be generated as follows:
MongoDB::BSON->new(
dbref_callback => sub { return MongoDB::DBRef->new(shift) },
dt_type => $client->dt_type,
prefer_numeric => $MongoDB::BSON::looks_like_number || 0,
(
$MongoDB::BSON::char ne '$' ?
( op_char => $MongoDB::BSON::char ) : ()
),
);
This will inflate all DBRefs to L<MongoDB::DBRef> objects, set C<dt_type>
based on the client's C<db_type> accessor, and set the C<prefer_numeric>
and C<op_char> attributes based on the deprecated legacy global variables.
=head2 connect_timeout_ms
This attribute specifies the amount of time in milliseconds to wait for a
new connection to a server.
The default is 10,000 ms.
If set to a negative value, connection operations will block indefinitely
until the server replies or until the operating system TCP/IP stack gives
up (e.g. if the name can't resolve or there is no process listening on the
target host/port).
A zero value polls the socket during connection and is thus likely to fail
except when talking to a local process (and perhaps even then).
This may be set in a connection string with the C<connectTimeoutMS> option.
=head2 db_name
Optional. If an L</auth_mechanism> requires a database for authentication,
this attribute will be used. Otherwise, it will be ignored. Defaults to
"admin".
This may be provided in the L<connection string URI|/CONNECTION STRING URI> as
a path between the authority and option parameter sections. For example, to
authenticate against the "admin" database (showing a configuration option only
for illustration):
mongodb://localhost/admin?readPreference=primary
=head2 heartbeat_frequency_ms
The time in milliseconds (non-negative) between scans of all servers to
check if they are up and update their latency. Defaults to 60,000 ms.
This may be set in a connection string with the C<heartbeatFrequencyMS> option.
=head2 j
If true, the client will block until write operations have been committed to the
server's journal. Prior to MongoDB 2.6, this option was ignored if the server was
running without journaling. Starting with MongoDB 2.6, write operations will fail
if this option is used when the server is running without journaling.
This may be set in a connection string with the C<journal> option as the
strings 'true' or 'false'.
=head2 local_threshold_ms
The width of the 'latency window': when choosing between multiple suitable
servers for an operation, the acceptable delta in milliseconds
(non-negative) between shortest and longest average round-trip times.
Servers within the latency window are selected randomly.
Set this to "0" to always select the server with the shortest average round
trip time. Set this to a very high value to always randomly choose any known
server.
Defaults to 15 ms.
See L</SERVER SELECTION> for more details.
This may be set in a connection string with the C<localThresholdMS> option.
=head2 max_time_ms
Specifies the maximum amount of time in (non-negative) milliseconds that the
server should use for working on a database command. Defaults to 0, which disables
this feature. Make sure this value is shorter than C<socket_timeout_ms>.
B<Note>: this will only be used for server versions 2.6 or greater, as that
was when the C<$maxTimeMS> meta-operator was introduced.
You are B<strongly> encouraged to set this variable if you know your
environment has MongoDB 2.6 or later, as getting a definitive error response
from the server is vastly preferred over a getting a network socket timeout.
This may be set in a connection string with the C<maxTimeMS> option.
=head2 password
If an L</auth_mechanism> requires a password, this attribute will be
used. Otherwise, it will be ignored.
This may be provided in the L<connection string URI|/CONNECTION STRING URI> as
a C<username:password> pair in the leading portion of the authority section
before a C<@> character. For example, to authenticate as user "mulder" with
password "trustno1":
mongodb://mulder:trustno1@localhost
If the username or password have a ":" or "@" in it, they must be URL encoded.
An empty password still requires a ":" character.
=head2 port
If a network port is not specified as part of the C<host> attribute, this
attribute provides the port to use. It defaults to 27107.
=head2 read_pref_mode
The read preference mode determines which server types are candidates
for a read operation. Valid values are:
=over 4
=item *
primary
=item *
primaryPreferred
=item *
secondary
=item *
secondaryPreferred
=item *
nearest
=back
For core documentation on read preference see
L<http://docs.mongodb.org/manual/core/read-preference/>.
This may be set in a connection string with the C<readPreference> option.
=head2 read_pref_tag_sets
The C<read_pref_tag_sets> parameter is an ordered list of tag sets used to
restrict the eligibility of servers, such as for data center awareness. It
must be an array reference of hash references.
The application of C<read_pref_tag_sets> varies depending on the
C<read_pref_mode> parameter. If the C<read_pref_mode> is 'primary', then
C<read_pref_tag_sets> must not be supplied.
For core documentation on read preference see
L<http://docs.mongodb.org/manual/core/read-preference/>.
This may be set in a connection string with the C<readPreferenceTags> option.
If given, the value must be key/value pairs joined with a ":". Multiple pairs
must be separated by a comma. If ": or "," appear in a key or value, they must
be URL encoded. The C<readPreferenceTags> option may appear more than once, in
which case each document will be added to the tag set list.
=head2 replica_set_name
Specifies the replica set name to connect to. If this string is non-empty,
then the topology is treated as a replica set and all server replica set
names must match this or they will be removed from the topology.
This may be set in a connection string with the C<replicaSet> option.
=head2 server_selection_timeout_ms
This attribute specifies the amount of time in milliseconds to wait for a
suitable server to be available for a read or write operation. If no
server is available within this time period, an exception will be thrown.
The default is 30,000 ms.
See L</SERVER SELECTION> for more details.
This may be set in a connection string with the C<serverSelectionTimeoutMS>
option.
=head2 server_selection_try_once
This attribute controls whether the client will make only a single attempt
to find a suitable server for a read or write operation. The default is true.
When true, the client will B<not> use the C<server_selection_timeout_ms>.
Instead, if the topology information is stale and needs to be checked or
if no suitable server is available, the client will make a single
scan of all known servers to try to find a suitable one.
When false, the client will continually scan known servers until a suitable
server is found or the C<serverSelectionTimeoutMS> is reached.
See L</SERVER SELECTION> for more details.
This may be set in a connection string with the C<serverSelectionTryOnce>
option.
=head2 socket_check_interval_ms
If a socket to a server has not been used in this many milliseconds, an
C<ismaster> command will be issued to check the status of the server before
issuing any reads or writes. Must be non-negative.
The default is 5,000 ms.
This may be set in a connection string with the C<socketCheckIntervalMS>
option.
=head2 socket_timeout_ms
This attribute specifies the amount of time in milliseconds to wait for a
reply from the server before issuing a network exception.
The default is 30,000 ms.
If set to a negative value, socket operations will block indefinitely
until the server replies or until the operating system TCP/IP stack
gives up.
A zero value polls the socket for available data and is thus likely to fail
except when talking to a local process (and perhaps even then).
This may be set in a connection string with the C<socketTimeoutMS> option.
=head2 ssl
ssl => 1
ssl => \%ssl_options
This tells the driver that you are connecting to an SSL mongodb instance.
You must have L<IO::Socket::SSL> 1.42+ and L<Net::SSLeay> 1.49+ installed for
SSL support.
The C<ssl> attribute takes either a boolean value or a hash reference of
options to pass to IO::Socket::SSL. For example, to set a CA file to validate
the server certificate and set a client certificate for the server to validate,
you could set the attribute like this:
ssl => {
SSL_ca_file => "/path/to/ca.pem",
SSL_cert_file => "/path/to/client.pem",
}
If C<SSL_ca_file> is not provided, server certificates are verified against a
default list of CAs, either L<Mozilla::CA> or an operating-system-specific
default CA file. To disable verification, you can use
C<< SSL_verify_mode => 0x00 >>.
B<You are strongly encouraged to use your own CA file for increased security>.
Server hostnames are also validated against the CN name in the server
certificate using C<< SSL_verifycn_scheme => 'http' >>. You can use the
scheme 'none' to disable this check.
B<Disabling certificate or hostname verification is a security risk and is not
recommended>.
This may be set to the string 'true' or 'false' in a connection string with the
C<ssl> option, which will enable ssl with default configuration. (A future
version of the driver may support customizing ssl via the connection string.)
=head2 username
Optional username for this client connection. If this field is set, the client
will attempt to authenticate when connecting to servers. Depending on the
L</auth_mechanism>, the L</password> field or other attributes will need to be
set for authentication to succeed.
This may be provided in the L<connection string URI|/CONNECTION STRING URI> as
a C<username:password> pair in the leading portion of the authority section
before a C<@> character. For example, to authenticate as user "mulder" with
password "trustno1":
mongodb://mulder:trustno1@localhost
If the username or password have a ":" or "@" in it, they must be URL encoded.
An empty password still requires a ":" character.
=head2 w
The client I<write concern>.
=over 4
=item * C<0> Unacknowledged. MongoClient will B<NOT> wait for an acknowledgment that
the server has received and processed the request. Older documentation may refer
to this as "fire-and-forget" mode. This option is not recommended.
=item * C<1> Acknowledged. This is the default. MongoClient will wait until the
primary MongoDB acknowledges the write.
=item * C<2> Replica acknowledged. MongoClient will wait until at least two
replicas (primary and one secondary) acknowledge the write. You can set a higher
number for more replicas.
=item * C<all> All replicas acknowledged.
=item * C<majority> A majority of replicas acknowledged.
=back
In MongoDB v2.0+, you can "tag" replica members. With "tagging" you can
specify a custom write concern For more information see L<Data Center
Awareness|http://docs.mongodb.org/manual/data-center-awareness/>
This may be set in a connection string with the C<w> option.
=head2 wtimeout
The number of milliseconds an operation should wait for C<w> secondaries to
replicate it.
Defaults to 1000 (1 second).
See C<w> above for more information.
This may be set in a connection string with the C<wTimeoutMS> option.
=head2 read_concern_level
The read concern level determines the consistency level required
of data being read.
The default level is C<undef>, which means the server will use its configured
default.
If the level is set to "local", reads will return the latest data a server has
locally.
Additional levels are storage engine specific. See L<Read
Concern|http://docs.mongodb.org/manual/search/?query=readConcern> in the MongoDB
documentation for more details.
This may be set in a connection string with the the C<readConcernLevel> option.
=head2 dt_type (DEPRECATED AND READ-ONLY)
Sets the type of object which is returned for DateTime fields. The default
is L<DateTime>. Other acceptable values are L<DateTime::Tiny>,
L<Time::Moment> and C<undef>. The latter will give you the raw epoch value
(possibly as a floating point value) rather than an object.
This will be used to construct L</bson_codec> object if one is not provided.
As this has a one-time effect, it is now read-only to help you detect
code that was trying to change after the fact during program execution.
For temporary or localized changes, look into overriding the C<bson_codec>
object for a database or collection object.
=head2 query_timeout (DEPRECATED AND READ-ONLY)
# set query timeout to 1 second
my $client = MongoDB::MongoClient->new(query_timeout => 1000);
This option has been renamed as L</socket_timeout_ms>. If this option is set
and that one is not, this will be used.
This value is in milliseconds and defaults to 30000.
=head2 sasl (DEPRECATED)
If true, the driver will set the authentication mechanism based on the
C<sasl_mechanism> property.
=head2 sasl_mechanism (DEPRECATED)
This specifies the SASL mechanism to use for authentication with a MongoDB server.
It has the same valid values as L</auth_mechanism>. The default is GSSAPI.
=head2 timeout (DEPRECATED AND READ-ONLY)
This option has been renamed as L</connect_timeout_ms>. If this option is set
and that one is not, this will be used.
Connection timeout is in milliseconds. Defaults to C<10000>.
=head1 METHODS
=head2 read_preference
Returns a L<MongoDB::ReadPreference> object constructed from
L</read_pref_mode> and L</read_pref_tag_sets>
B<The use of C<read_preference> as a mutator has been removed.> Read
preference is read-only. If you need a different read preference for
a database or collection, you can specify that in C<get_database> or
C<get_collection>.
=head2 write_concern
Returns a L<MongoDB::WriteConcern> object constructed from L</w>, L</write_concern>
and L</j>.
=head2 read_concern
Returns a L<MongoDB::ReadConcern> object constructed from
L</read_concern_level>.
=head2 topology_type
Returns an enumerated topology type. If the L</replica_set_name> is
set, the value will be either 'ReplicaSetWithPrimary' or 'ReplicaSetNoPrimary'
(if the primary is down or not yet discovered). Without L</replica_set_name>,
the type will be 'Single' if there is only one server in the list of hosts, and
'Sharded' if there are more than one.
N.B. A single mongos will have a topology type of 'Single', as that mongos will
be used for all reads and writes, just like a standalone mongod. The 'Sharded'
type indicates a sharded cluster with multiple mongos servers, and reads/writes
will be distributed acc
=head2 connect
$client->connect;
Calling this method is unnecessary, as connections are established
automatically as needed. It is kept for backwards compatibility. Calling it
will check all servers in the deployment which ensures a connection to any
that are available.
See L</reconnect> for a method that is useful when using forks or threads.
=head2 disconnect
$client->disconnect;
Drops all connections to servers.
=head2 reconnect
$client->reconnect;
This method closes all connections to the server, as if L</disconnect> were
called, and then immediately reconnects. Use this after forking or spawning
off a new thread.
=head2 topology_status
$client->topology_status;
$client->topology_status( refresh => 1 );
Returns a hash reference with server topology information like this:
{
'topology_type' => 'ReplicaSetWithPrimary'
'replica_set_name' => 'foo',
'last_scan_time' => '1433766895.183241',
'servers' => [
{
'address' => 'localhost:50003',
'ewma_rtt_ms' => '0.223462326',
'type' => 'RSSecondary'
},
{
'address' => 'localhost:50437',
'ewma_rtt_ms' => '0.268435456',
'type' => 'RSArbiter'
},
{
'address' => 'localhost:50829',
'ewma_rtt_ms' => '0.737782272',
'type' => 'RSPrimary'
}
},
}
If the 'refresh' argument is true, then the topology will be scanned
to update server data before returning the hash reference.
=head2 database_names
my @dbs = $client->database_names;
Lists all databases on the MongoDB server.
=head2 get_database, db
my $database = $client->get_database('foo');
my $database = $client->get_database('foo', $options);
my $database = $client->db('foo', $options);
Returns a L<MongoDB::Database> instance for the database with the given
C<$name>.
It takes an optional hash reference of options that are passed to the
L<MongoDB::Database> constructor.
The C<db> method is an alias for C<get_database>.
=head2 get_namespace, ns
my $collection = $client->get_namespace('test.foo');
my $collection = $client->get_namespace('test.foo', $options);
my $collection = $client->ns('test.foo', $options);
Returns a L<MongoDB::Collection> instance for the given namespace.
The namespace has both the database name and the collection name
separated with a dot character.
This is a quick way to get a collection object if you don't need
the database object separately.
It takes an optional hash reference of options that are passed to the
L<MongoDB::Collection> constructor. The intermediate L<MongoDB::Database>
object will be created with default options.
The C<ns> method is an alias for C<get_namespace>.
=head2 fsync(\%args)
$client->fsync();
A function that will forces the server to flush all pending writes to the storage layer.
The fsync operation is synchronous by default, to run fsync asynchronously, use the following form:
$client->fsync({async => 1});
The primary use of fsync is to lock the database during backup operations. This will flush all data to the data storage layer and block all write operations until you unlock the database. Note: you can still read while the database is locked.
$conn->fsync({lock => 1});
=head2 fsync_unlock
$conn->fsync_unlock();
Unlocks a database server to allow writes and reverses the operation of a $conn->fsync({lock => 1}); operation.
=for Pod::Coverage connected
send_admin_command
send_direct_op
send_read_op
send_write_op
=head1 DEPLOYMENT TOPOLOGY
MongoDB can operate as a single server or as a distributed system. One or more
servers that collectively provide access to a single logical set of MongoDB
databases are referred to as a "deployment".
There are three types of deployments:
=over 4
=item *
Single server – a stand-alone mongod database
=item *
Replica set – a set of mongod databases with data replication and fail-over capability
=item *
Sharded cluster – a distributed deployment that spreads data across one or more shards, each of which can be a replica set. Clients communicate with a mongos process that routes operations to the correct share.
=back
The state of a deployment, including its type, which servers are members, the
server types of members and the round-trip network latency to members is
referred to as the "topology" of the deployment.
To the greatest extent possible, the MongoDB driver abstracts away the details
of communicating with different deployment types. It determines the deployment
topology through a combination of the connection string, configuration options
and direct discovery communicating with servers in the deployment.
=head1 CONNECTION STRING URI
MongoDB uses a pseudo-URI connection string to specify one or more servers to
connect to, along with configuration options.
To connect to more than one database server, provide host or host:port pairs
as a comma separated list:
mongodb://host1[:port1][,host2[:port2],...[,hostN[:portN]]]
This list is referred to as the "seed list". An arbitrary number of hosts can
be specified. If a port is not specified for a given host, it will default to
27017.
If multiple hosts are given in the seed list or discovered by talking to
servers in the seed list, they must all be replica set members or must all be
mongos servers for a sharded cluster.
A replica set B<MUST> have the C<replicaSet> option set to the replica set
name.
If there is only single host in the seed list and C<replicaSet> is not
provided, the deployment is treated as a single server deployment and all
reads and writes will be sent to that host.
Providing a replica set member as a single host without the set name is the
way to get a "direct connection" for carrying out administrative activities
on that server.
The connection string may also have a username and password:
mongodb://username:password@host1:port1,host2:port2
The username and password must be URL-escaped.
A optional database name for authentication may be given:
mongodb://username:password@host1:port1,host2:port2/my_database
Finally, connection string options may be given as URI attribute pairs in a query
string:
mongodb://host1:port1,host2:port2/?ssl=1&wtimeoutMS=1000
mongodb://username:password@host1:port1,host2:port2/my_database?ssl=1&wtimeoutMS=1000
The currently supported connection string options are:
=over 4
*authMechanism
*authMechanism.SERVICE_NAME
*connectTimeoutMS
*journal
*readPreference
*readPreferenceTags
*replicaSet
*ssl
*w
*wtimeoutMS
=back
See the official MongoDB documentation on connection strings for more on the URI
format and connection string options:
L<http://docs.mongodb.org/manual/reference/connection-string/>.
=head1 SERVER SELECTION
For a single server deployment or a direct connection to a mongod or
mongos, all reads and writes and sent to that server. Any read-preference
is ignored.
When connected to a deployment with multiple servers, such as a replica set
or sharded cluster, the driver chooses a server for operations based on the
type of operation (read or write), the types of servers available and a
read preference.
For a replica set deployment, writes are sent to the primary (if available)
and reads are sent to a server based on the L</read_preference> attribute,
which defaults to sending reads to the primary. See
L<MongoDB::ReadPreference> for more.
For a sharded cluster reads and writes are distributed across mongos
servers in the seed list. Any read preference is passed through to the
mongos and used by it when executing reads against shards.
If multiple servers can service an operation (e.g. multiple mongos servers,
or multiple replica set members), one is chosen at random from within the
"latency window". The server with the shortest average round-trip time
(RTT) is always in the window. Any servers with an average round-trip time
less than or equal to the shortest RTT plus the L</local_threshold_ms> are
also in the latency window.
If a suitable server is not immediately available, what happens next
depends on the L</server_selection_try_once> option.
If that option is true, a single topology scan will be performed.
Afterwards if a suitable server is available, it will be returned;
otherwise, an exception is thrown.
If that option is false, the driver will do topology scans repeatedly
looking for a suitable server. When more than
L</server_selection_timeout_ms> milliseconds have elapsed since the start
of server selection without a suitable server being found, an exception is
thrown.
B<Note>: the actual maximum wait time for server selection could be as long
C<server_selection_timeout_ms> plus the amount of time required to do a
topology scan.
=head1 SERVER MONITORING AND FAILOVER
When the client first needs to find a server for a database operation, all
servers from the L</host> attribute are scanned to determine which servers to
monitor. If the deployment is a replica set, additional hosts may be
discovered in this process. Invalid hosts are dropped.
After the initial scan, whenever the servers have not been checked in
L</heartbeat_frequency_ms> milliseconds, the scan will be repeated. This
amortizes monitoring time over many of operations. Additionally, if a
socket has been idle for a while, it will be checked before being used for
an operation.
If a server operation fails because of a "not master" or "node is
recovering" error, or if there is a network error or timeout, then the
server is flagged as unavailable and exception will be thrown. See
L<MongoDB::Errors> for exception types.
If the error is caught and handled, the next operation will rescan all
servers immediately to update its view of the topology. The driver can
continue to function as long as servers are suitable per L</SERVER
SELECTION>.
When catching an exception, users must determine whether or not their
application should retry an operation based on the specific operation
attempted and other use-case-specific considerations. For automating
retries despite exceptions, consider using the L<Try::Tiny::Retry> module.
=head1 AUTHENTICATION
The MongoDB server provides several authentication mechanisms, though some
are only available in the Enterprise edition.
MongoDB client authentication is controlled via the L</auth_mechanism>
attribute, which takes one of the following values:
=over 4
=item *
MONGODB-CR -- legacy username-password challenge-response
=item *
SCRAM-SHA-1 -- secure username-password challenge-response (3.0+)
=item *
MONGODB-X509 -- SSL client certificate authentication (2.6+)
=item *
PLAIN -- LDAP authentication via SASL PLAIN (Enterprise only)
=item *
GSSAPI -- Kerberos authentication (Enterprise only)
=back
The mechanism to use depends on the authentication configuration of the
server. See the core documentation on authentication:
L<http://docs.mongodb.org/manual/core/access-control/>.
Usage information for each mechanism is given below.
=head2 MONGODB-CR and SCRAM-SHA-1 (for username/password)
These mechnisms require a username and password, given either as
constructor attributes or in the C<host> connection string.
If a username is provided and an authentication mechanism is not specified,
the client will use SCRAM-SHA-1 for version 3.0 or later servers and will
fall back to MONGODB-CR for older servers.
my $mc = MongoDB::MongoClient->new(
host => "mongodb://mongo.example.com/",
username => "johndoe",
password => "trustno1",
);
my $mc = MongoDB::MongoClient->new(
host => "mongodb://johndoe:trustno1@mongo.example.com/",
);
Usernames and passwords will be UTF-8 encoded before use. The password is
never sent over the wire -- only a secure digest is used. The SCRAM-SHA-1
mechanism is the Salted Challenge Response Authentication Mechanism
definedin L<RFC 5802|http://tools.ietf.org/html/rfc5802>.
The default database for authentication is 'admin'. If another database
name should be used, specify it with the C<db_name> attribute or via the
connection string.
db_name => auth_db
mongodb://johndoe:trustno1@mongo.example.com/auth_db
=head2 MONGODB-X509 (for SSL client certificate)
X509 authentication requires SSL support (L<IO::Socket::SSL>) and requires
that a client certificate be configured and that the username attribute be
set to the "Subject" field, formatted according to RFC 2253. To find the
correct username, run the C<openssl> program as follows:
$ openssl x509 -in certs/client.pem -inform PEM -subject -nameopt RFC2253
subject= CN=XXXXXXXXXXX,OU=XXXXXXXX,O=XXXXXXX,ST=XXXXXXXXXX,C=XX
In this case the C<username> attribute would be
C<CN=XXXXXXXXXXX,OU=XXXXXXXX,O=XXXXXXX,ST=XXXXXXXXXX,C=XX>.
Configure your client with the correct username and ssl parameters, and
specify the "MONGODB-X509" authentication mechanism.
my $mc = MongoDB::MongoClient->new(
host => "mongodb://sslmongo.example.com/",
ssl => {
SSL_ca_file => "certs/ca.pem",
SSL_cert_file => "certs/client.pem",
},
auth_mechanism => "MONGODB-X509",
username => "CN=XXXXXXXXXXX,OU=XXXXXXXX,O=XXXXXXX,ST=XXXXXXXXXX,C=XX"
);
=head2 PLAIN (for LDAP)
This mechanism requires a username and password, which will be UTF-8
encoded before use. The C<auth_mechanism> parameter must be given as a
constructor attribute or in the C<host> connection string:
my $mc = MongoDB::MongoClient->new(
host => "mongodb://mongo.example.com/",
username => "johndoe",
password => "trustno1",
auth_mechanism => "PLAIN",
);
my $mc = MongoDB::MongoClient->new(
host => "mongodb://johndoe:trustno1@mongo.example.com/authMechanism=PLAIN",
);
=head2 GSSAPI (for Kerberos)
Kerberos authentication requires the CPAN module L<Authen::SASL> and a
GSSAPI-capable backend.
On Debian systems, L<Authen::SASL> may be available as
C<libauthen-sasl-perl>; on RHEL systems, it may be available as
C<perl-Authen-SASL>.
The L<Authen::SASL::Perl> backend comes with L<Authen::SASL> and requires
the L<GSSAPI> CPAN module for GSSAPI support. On Debian systems, this may
be available as C<libgssapi-perl>; on RHEL systems, it may be available as
C<perl-GSSAPI>.
Installing the L<GSSAPI> module from CPAN rather than an OS package
requires C<libkrb5> and the C<krb5-config> utility (available for
Debian/RHEL systems in the C<libkrb5-dev> package).
Alternatively, the L<Authen::SASL::XS> or L<Authen::SASL::Cyrus> modules
may be used. Both rely on Cyrus C<libsasl>. L<Authen::SASL::XS> is
preferred, but not yet available as an OS package. L<Authen::SASL::Cyrus>
is available on Debian as C<libauthen-sasl-cyrus-perl> and on RHEL as
C<perl-Authen-SASL-Cyrus>.
Installing L<Authen::SASL::XS> or L<Authen::SASL::Cyrus> from CPAN requires
C<libsasl>. On Debian systems, it is available from C<libsasl2-dev>; on
RHEL, it is available in C<cyrus-sasl-devel>.
To use the GSSAPI mechanism, first run C<kinit> to authenticate with the ticket
granting service:
$ kinit johndoe@EXAMPLE.COM
Configure MongoDB::MongoClient with the principal name as the C<username>
parameter and specify 'GSSAPI' as the C<auth_mechanism>:
my $mc = MongoDB::MongoClient->new(
host => 'mongodb://mongo.example.com',
username => 'johndoe@EXAMPLE.COM',
auth_mechanism => 'GSSAPI',
);
Both can be specified in the C<host> connection string, keeping in mind
that the '@' in the principal name must be encoded as "%40":
my $mc = MongoDB::MongoClient->new(
host =>
'mongodb://johndoe%40EXAMPLE.COM@mongo.examplecom/?authMechanism=GSSAPI',
);
The default service name is 'mongodb'. It can be changed with the
C<auth_mechanism_properties> attribute or in the connection string.
auth_mechanism_properties => { SERVICE_NAME => 'other_service' }
mongodb://.../?authMechanism=GSSAPI&authMechanismProperties=SERVICE_NAME:other_service
=head1 THREAD-SAFETY AND FORK-SAFETY
You B<MUST> call the L</reconnect> method on any MongoDB::MongoClient objects
after forking or spawning a thread.
=head1 AUTHORS
=over 4
=item *
David Golden <david@mongodb.com>
=item *
Mike Friedman <friedo@friedo.com>
=item *
Kristina Chodorow <k.chodorow@gmail.com>
=item *
Florian Ragwitz <rafl@debian.org>
=back
=head1 COPYRIGHT AND LICENSE
This software is Copyright (c) 2016 by MongoDB, Inc..
This is free software, licensed under:
The Apache License, Version 2.0, January 2004
=cut
|