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 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380
|
# Copyright 2012 - present MongoDB, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
use strict;
use warnings;
package MongoDB::MongoClient;
# ABSTRACT: A connection to a MongoDB server or multi-server deployment
use version;
our $VERSION = 'v2.2.2';
use Moo;
use MongoDB::ClientSession;
use MongoDB::Cursor;
use MongoDB::Error;
use MongoDB::Op::_Command;
use MongoDB::Op::_FSyncUnlock;
use MongoDB::ReadConcern;
use MongoDB::ReadPreference;
use MongoDB::WriteConcern;
use MongoDB::_Constants;
use MongoDB::_Credential;
use MongoDB::_Dispatcher;
use MongoDB::_SessionPool;
use MongoDB::_Topology;
use MongoDB::_URI;
use BSON 1.012000;
use Digest::MD5;
use UUID::URandom;
use Tie::IxHash;
use Time::HiRes qw/usleep/;
use Carp 'carp', 'croak', 'confess';
use Safe::Isa 1.000007;
use Scalar::Util qw/reftype weaken/;
use boolean;
use Encode;
use MongoDB::_Types qw(
ArrayOfHashRef
AuthMechanism
Boolish
BSONCodec
CompressionType
Document
HeartbeatFreq
MaxStalenessNum
NonNegNum
ReadPrefMode
ReadPreference
ZlibCompressionLevel
);
use Types::Standard qw(
CodeRef
HashRef
ArrayRef
InstanceOf
Undef
Int
Num
Str
Maybe
);
use namespace::clean -except => 'meta';
#--------------------------------------------------------------------------#
# 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 B<NOTE>: Options specified in the connection string take precedence over options
#pod provided as constructor arguments.
#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 app_name
#pod
#pod This attribute specifies an application name that should be associated with
#pod this client. The application name will be communicated to the server as
#pod part of the initial connection handshake, and will appear in
#pod connection-level and operation-level diagnostics on the server generated on
#pod behalf of this client. This may be set in a connection string with the
#pod C<appName> option.
#pod
#pod The default is the empty string, which indicates a lack of an application
#pod name.
#pod
#pod The application name must not exceed 128 bytes.
#pod
#pod =cut
has app_name => (
is => 'lazy',
isa => Str,
builder => '_build_app_name',
);
sub _build_app_name {
my ($self) = @_;
my $app_name = $self->__uri_or_else(
u => 'appname',
e => 'app_name',
d => '',
);
unless ( length($app_name) <= 128 ) {
MongoDB::UsageError->throw("app name must be at most 128 bytes");
}
return $app_name;
}
#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 or C<authSource> URI option is provided,
#pod it defaults to NONE. Otherwise, it is set to DEFAULT, which chooses
#pod SCRAM-SHA-1 if 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 $source = $self->_uri->options->{authsource} // "";
my $default = length( $self->username ) || length($source) ? '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<BSON>. It may be initialized with a hash reference that will
#pod be coerced into a new L<BSON> object.
#pod
#pod If not provided, a L<BSON> object with default values will be generated.
#pod
#pod =cut
has bson_codec => (
is => 'lazy',
isa => BSONCodec,
coerce => BSONCodec->coercion,
writer => '_set_bson_codec',
builder => '_build_bson_codec',
);
sub _build_bson_codec {
my ($self) = @_;
return BSON->new();
}
#pod =attr compressors
#pod
#pod An array reference of compression type names. Currently, C<zlib>, C<zstd> and
#pod C<snappy> are supported.
#pod
#pod =cut
has compressors => (
is => 'lazy',
isa => ArrayRef[CompressionType],
builder => '_build_compressors',
);
sub _build_compressors {
my ($self) = @_;
return $self->__uri_or_else(
u => 'compressors',
e => 'compressors',
d => [],
);
}
#pod =attr zlib_compression_level
#pod
#pod An integer from C<-1> to C<9> specifying the compression level to use
#pod when L</compression> is set to C<zlib>.
#pod
#pod B<Note>: When the special value C<-1> is given, the default compression
#pod level will be used.
#pod
#pod =cut
has zlib_compression_level => (
is => 'lazy',
isa => ZlibCompressionLevel,
builder => '_build_zlib_compression_level',
);
sub _build_zlib_compression_level {
my ($self) = @_;
return $self->__uri_or_else(
u => 'zlibcompressionlevel',
e => 'zlib_compression_level',
d => -1,
);
}
#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 => 10000,
);
}
#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 => HeartbeatFreq,
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 => Boolish,
builder => '_build_j',
);
sub _build_j {
my ($self) = @_;
return $self->__uri_or_else(
u => 'journal',
e => 'j',
d => undef,
);
}
#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_staleness_seconds
#pod
#pod The C<max_staleness_seconds> parameter represents the maximum replication lag in
#pod seconds (wall clock time) that a secondary can suffer and still be
#pod eligible for reads. The default is -1, which disables staleness checks.
#pod Otherwise, it must be a positive integer.
#pod
#pod B<Note>: this will only be used for server versions 3.4 or greater, as that
#pod was when support for staleness tracking was added.
#pod
#pod If the read preference mode is 'primary', then C<max_staleness_seconds> must not
#pod be supplied.
#pod
#pod The C<max_staleness_seconds> must be at least the C<heartbeat_frequency_ms>
#pod plus 10 seconds (which is how often the server makes idle writes to the
#pod oplog).
#pod
#pod This may be set in a connection string with the C<maxStalenessSeconds> option.
#pod
#pod =cut
has max_staleness_seconds => (
is => 'lazy',
isa => MaxStalenessNum,
builder => '_build_max_staleness_seconds',
);
sub _build_max_staleness_seconds {
my ($self) = @_;
return $self->__uri_or_else(
u => 'maxstalenessseconds',
e => 'max_staleness_seconds',
d => -1,
);
}
#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 monitoring_callback
#pod
#pod Specifies a code reference used to receive monitoring events. See
#pod L<MongoDB::Monitoring> for more details.
#pod
#pod =cut
has monitoring_callback => (
is => 'ro',
isa => Maybe [CodeRef],
);
#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_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,
);
}
#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 retry_reads
#pod
#pod =cut
has retry_reads => (
is => 'lazy',
isa => Boolish,
builder => '_build_retry_reads',
);
sub _build_retry_reads {
my ( $self ) = @_;
return $self->__uri_or_else(
u => 'retryreads',
e => 'retry_reads',
d => 1,
);
}
#pod =attr retry_writes
#pod
#pod Whether the client should use retryable writes for supported commands. The
#pod default value is true, which means that commands which support retryable writes
#pod will be retried on certain errors, such as C<not master> and C<node is
#pod recovering> errors.
#pod
#pod This may be set in a connection string with the C<retryWrites> option.
#pod
#pod Note that this is only supported on MongoDB > 3.6 in Replica Set or Shard
#pod Clusters, and will be ignored on other deployments.
#pod
#pod Unacknowledged write operations also do not support retryable writes, even when
#pod retry_writes has been enabled.
#pod
#pod The supported single statement write operations are currently as follows:
#pod
#pod =for :list
#pod * C<insert_one>
#pod * C<update_one>
#pod * C<replace_one>
#pod * C<delete_one>
#pod * C<find_one_and_delete>
#pod * C<find_one_and_replace>
#pod * C<find_one_and_update>
#pod
#pod The supported multi statement write operations are as follows:
#pod
#pod =for :list
#pod * C<insert_many>
#pod * C<bulk_write>
#pod
#pod The multi statement operations may be ether ordered or unordered. Note that for
#pod C<bulk_write> operations, the request may not include update_many or
#pod delete_many operations.
#pod
#pod =cut
has retry_writes => (
is => 'lazy',
isa => Boolish,
builder => '_build_retry_writes',
);
sub _build_retry_writes {
my ( $self ) = @_;
return $self->__uri_or_else(
u => 'retrywrites',
e => 'retry_writes',
d => 1,
);
}
#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 => Boolish,
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 server_selector
#pod
#pod Optional. This takes a function that augments the server selection rules.
#pod The function takes as a parameter a list of server descriptions representing
#pod the suitable servers for the read or write operation, and returns a list of
#pod server descriptions that should still be considered suitable. Most users
#pod should rely on the default server selection algorithm and should not need
#pod to set this attribute.
#pod
#pod =cut
has server_selector => (
is => 'ro',
isa => Maybe[CodeRef],
);
#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 The driver automatically sets the TCP keepalive option when initializing the
#pod socket. For keepalive related issues, check the MongoDB documentation for
#pod L<Does TCP keepalive time affect MongoDB Deployments?|https://docs.mongodb.com/v3.2/faq/diagnostics/#does-tcp-keepalive-time-affect-mongodb-deployments>.
#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 => 30000,
);
}
#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. (See
#pod L<connection string URI|/CONNECTION STRING URI> for additional TLS
#pod configuration options.)
#pod
#pod =cut
has ssl => (
is => 'lazy',
isa => Boolish|HashRef,
builder => '_build_ssl',
);
sub _build_ssl {
my ($self) = @_;
# options will be undef if not provided
my $uri_ssl = $self->__ssl_from_uri();
my $opt_ssl = exists $self->_deferred->{ssl} ? $self->_deferred->{ssl} : undef;
# no SSL options exist
if ( !defined $uri_ssl && !defined $opt_ssl ) {
return 0;
}
# validate deferred ssl arg type
if ( ref $opt_ssl && ref $opt_ssl ne 'HASH' ) {
MongoDB::UsageError->throw("ssl attribute must be scalar or hashref")
}
# no URI SSL defined means use opts SSL
if ( !defined $uri_ssl ) {
return $opt_ssl;
}
# if URI SSL is false, that takes precedence
if ( ! $uri_ssl ) {
return $uri_ssl;
}
# if opt SSL isn't a hashref, it's irrelevant
if ( ref $opt_ssl ne 'HASH' ) {
return $uri_ssl;
}
# if uri SSL isn't a hashref, we prefer opt SSL hashref
if ( ref $uri_ssl ne 'HASH' ) {
return $opt_ssl;
}
# both are hashes, so merge them with URI taking precedence
return { %$opt_ssl, %$uri_ssl };
}
my @tls_options = qw(
tlsallowinvalidcertificates
tlsallowinvalidhostnames
tlscafile
tlscertificatekeyfile
tlscertificatekeyfilepassword
tlsinsecure
);
sub __ssl_from_uri {
my ($self) = @_;
my $uri_options = $self->_uri->options;
my $saw_tls_boolean = exists $uri_options->{tls};
my $saw_tls_options = grep { length } map { exists $uri_options->{$_} } @tls_options;
if (!$saw_tls_options) {
return $saw_tls_boolean ? $uri_options->{tls} : undef;
}
my $ssl = {};
if (exists($uri_options->{tlscafile})) {
$ssl->{SSL_ca_file} = $uri_options->{tlscafile};
}
if (exists($uri_options->{tlscertificatekeyfile})) {
$ssl->{SSL_cert_file} = $uri_options->{tlscertificatekeyfile};
}
if (exists($uri_options->{tlscertificatekeyfilepassword})) {
$ssl->{SSL_passwd_cb} = sub { $uri_options->{tlscertificatekeyfilepassword} };
}
if (exists($uri_options->{tlsallowinvalidhostnames})) {
$ssl->{SSL_verifycn_scheme} = 'none';
}
if (exists($uri_options->{tlsallowinvalidcertificates})) {
$ssl->{SSL_verify_mode} = 0x00;
}
if (exists($uri_options->{tlsinsecure})) {
$ssl->{SSL_verify_mode} = 0x00;
$ssl->{SSL_verifycn_scheme} = 'none';
}
return $ssl;
}
#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. 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 If not set, the server default is used, which is typically "1".
#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). If you set this to undef, it could block indefinitely
#pod (or until socket timeout is reached).
#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 => Maybe[Int],
builder => '_build_wtimeout',
);
sub _build_wtimeout {
my ($self) = @_;
return $self->__uri_or_else(
u => 'wtimeoutms',
e => 'wtimeout',
d => 1000,
);
}
#--------------------------------------------------------------------------#
# 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(
( defined $self->read_pref_mode ? ( mode => $self->read_pref_mode ) : () ),
( defined $self->read_pref_tag_sets ? ( tag_sets => $self->read_pref_tag_sets ) : () ),
( defined $self->max_staleness_seconds ? ( max_staleness_seconds => $self->max_staleness_seconds ) : () ),
);
}
#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->_write_concern_options );
}
# Seperated out for use in transaction option defaults
sub _write_concern_options {
my ($self) = @_;
return (
wtimeout => $self->wtimeout,
# Must check for defined as w can be 0, and defaults to undef
( defined $self->w ? ( w => $self->w ) : () ),
( defined $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
#--------------------------------------------------------------------------#
# used for a more accurate 'is this client the same one' for sessions, instead
# of memory location which just feels... yucky
has _id => (
is => 'ro',
init_arg => undef,
default => sub { UUID::URandom::create_uuid_string() },
);
# 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 set,
#pod the value will be either 'ReplicaSetWithPrimary' or 'ReplicaSetNoPrimary'
#pod (if the primary is down or not yet discovered). Without
#pod L</replica_set_name>, if there is more than one server in the list of
#pod hosts, the type will be 'Sharded'.
#pod
#pod With only a single host and no replica set name, the topology type will
#pod start as 'Direct' until the server is contacted the first time, after which
#pod the type will be 'Sharded' for a mongos or 'Single' for standalone server
#pod or direct connection to a replica set member.
#pod
#pod =cut
has _topology => (
is => 'lazy',
isa => InstanceOf ['MongoDB::_Topology'],
init_arg => undef,
builder => '_build__topology',
handles => {
topology_type => 'type',
_cluster_time => 'cluster_time',
_update_cluster_time => 'update_cluster_time',
},
clearer => '_clear__topology',
);
sub _build__topology {
my ($self) = @_;
my $type =
length( $self->replica_set_name ) ? 'ReplicaSetNoPrimary'
: @{ $self->_uri->hostids } > 1 ? 'Sharded'
: 'Direct';
MongoDB::_Topology->new(
uri => $self->_uri,
type => $type,
app_name => $self->app_name,
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,
min_server_version => MIN_SERVER_VERSION,
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 ) : () ),
},
monitoring_callback => $self->monitoring_callback,
compressors => $self->compressors,
zlib_compression_level => $self->zlib_compression_level,
socket_check_interval_sec => $self->socket_check_interval_ms / 1000,
server_selector => $self->server_selector,
);
}
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 $uri_options = $self->_uri->options;
my $source = $uri_options->{authsource};
my $cred = MongoDB::_Credential->new(
monitoring_callback => $self->monitoring_callback,
mechanism => $mechanism,
mechanism_properties => $self->auth_mechanism_properties,
( $self->username ? ( username => $self->username ) : () ),
( $self->password ? ( password => $self->password ) : () ),
( $source ? ( source => $source ) : () ),
( $self->db_name ? ( db_name => $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") );
}
}
has _dispatcher => (
is => 'lazy',
isa => InstanceOf ['MongoDB::_Dispatcher'],
init_arg => undef,
builder => '_build__dispatcher',
handles => [
qw(
send_direct_op
send_primary_op
send_retryable_read_op
send_read_op
send_retryable_write_op
send_write_op
)
],
);
sub _build__dispatcher {
my $self = shift;
return MongoDB::_Dispatcher->new(
topology => $self->_topology,
retry_writes => $self->retry_writes,
retry_reads => $self->retry_reads,
);
}
has _server_session_pool => (
is => 'lazy',
isa => InstanceOf['MongoDB::_SessionPool'],
init_arg => undef,
builder => '_build__server_session_pool',
);
sub _build__server_session_pool {
my $self = shift;
return MongoDB::_SessionPool->new(
dispatcher => $self->_dispatcher,
topology => $self->_topology,
);
}
#--------------------------------------------------------------------------#
# Constructor customization
#--------------------------------------------------------------------------#
# these attributes are lazy, built from either _uri->options or from
# _config_options captured in BUILDARGS
my @deferred_options = qw(
app_name
auth_mechanism
auth_mechanism_properties
connect_timeout_ms
db_name
heartbeat_frequency_ms
j
local_threshold_ms
max_staleness_seconds
max_time_ms
read_pref_mode
read_pref_tag_sets
replica_set_name
retry_writes
retry_reads
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->hostids };
# 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);
}
},
)
);
}
# Instantiate topology
$self->_topology;
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. It also clears the session
#pod cache. Use this after forking or spawning off a new thread.
#pod
#pod =cut
sub reconnect {
my ($self) = @_;
$self->_topology->close_all_links;
$self->_server_session_pool->reset_pool;
$self->_topology->scan_all_servers(1);
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(1) if $opts{refresh};
return $self->_topology->status_struct;
}
#pod =method start_session
#pod
#pod $client->start_session;
#pod $client->start_session( $options );
#pod
#pod Returns a new L<MongoDB::ClientSession> with the supplied options.
#pod
#pod will throw a C<MongoDB::ConfigurationError> if sessions are not supported by
#pod the connected MongoDB deployment.
#pod
#pod the options hash is an optional hash which can have the following keys:
#pod
#pod =for :list
#pod * C<causalConsistency> - Enable Causally Consistent reads for this session.
#pod Defaults to true.
#pod
#pod for more information see L<MongoDB::ClientSession/options>.
#pod
#pod =cut
sub start_session {
my ( $self, $opts ) = @_;
unless ( $self->_topology->_supports_sessions ) {
MongoDB::ConfigurationError->throw( "Sessions are not supported by this MongoDB deployment" );
}
return $self->_start_client_session( 1, $opts );
}
sub _maybe_get_implicit_session {
my ($self) = @_;
# Dont return an error as implicit sessions need to be backwards compatible
return undef unless $self->_topology->_supports_sessions; ## no critic
return $self->_start_client_session(0);
}
sub _start_client_session {
my ( $self, $is_explicit, $opts ) = @_;
$opts ||= {};
my $session = $self->_server_session_pool->get_server_session;
return MongoDB::ClientSession->new(
client => $self,
options => $opts,
_is_explicit => $is_explicit,
server_session => $session,
);
}
#--------------------------------------------------------------------------#
# 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,
session => $self->_maybe_get_implicit_session,
monitoring_callback => $self->monitoring_callback,
);
return $self->send_retryable_read_op( $op );
}
# Ostensibly the same as above, but allows for specific addressing - uses 'send_direct_op'.
sub _send_direct_admin_command {
my ( $self, $address, $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,
session => $self->_maybe_get_implicit_session,
monitoring_callback => $self->monitoring_callback,
);
return $self->send_direct_op( $op, $address );
}
#--------------------------------------------------------------------------#
# database helper methods
#--------------------------------------------------------------------------#
#pod =method list_databases
#pod
#pod # get all information on all databases
#pod my @dbs = $client->list_databases;
#pod
#pod # get only the foo databases
#pod my @foo_dbs = $client->list_databases({ filter => { name => qr/^foo/ } });
#pod
#pod Lists all databases with information on each database. Supports filtering by
#pod any of the output fields under the C<filter> argument, such as:
#pod
#pod =for :list
#pod * C<name>
#pod * C<sizeOnDisk>
#pod * C<empty>
#pod * C<shards>
#pod
#pod =cut
sub list_databases {
my ( $self, $args ) = @_;
my @databases;
eval {
my $output = $self->send_admin_command([ listDatabases => 1, ( $args ? %$args : () ) ])->output;
if (ref($output) eq 'HASH' && exists $output->{databases}) {
@databases = @{ $output->{databases} };
}
return 1;
} or do {
my $error = $@ || "Unknown error";
if ( $error->$_isa("MongoDB::DatabaseError" ) ) {
return if $error->result->output->{code} == CANT_OPEN_DB_IN_READ_LOCK();
}
die $error;
};
return @databases;
}
#pod =method database_names
#pod
#pod my @dbs = $client->database_names;
#pod
#pod # get only the foo database names
#pod my @foo_dbs = $client->database_names({ filter => { name => qr/^foo/ } });
#pod
#pod List of all database names on the MongoDB server. Supports filters in the same
#pod way as L</"list_databases">.
#pod
#pod =cut
sub database_names {
my ( $self, $args ) = @_;
$args ||= {};
$args->{nameOnly} = 1;
my @output = $self->list_databases($args);
my @databases = map { $_->{name} } @output;
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,
monitoring_callback => $self->monitoring_callback,
);
return $self->send_primary_op($op);
}
sub _get_session_from_hashref {
my ( $self, $hashref ) = @_;
my $session = delete $hashref->{session};
if ( defined $session ) {
MongoDB::UsageError->throw( "Cannot use session from another client" )
if ( $session->client->_id ne $self->_id );
MongoDB::UsageError->throw( "Cannot use session which has ended" )
if ! defined $session->session_id;
} else {
$session = $self->_maybe_get_implicit_session;
}
return $session;
}
#pod =method watch
#pod
#pod Watches for changes on the cluster.
#pod
#pod Perform an aggregation with an implicit initial C<$changeStream> stage
#pod and returns a L<MongoDB::ChangeStream> result which can be used to
#pod iterate over the changes in the cluster. This functionality is
#pod available since MongoDB 4.0.
#pod
#pod my $stream = $client->watch();
#pod my $stream = $client->watch( \@pipeline );
#pod my $stream = $client->watch( \@pipeline, \%options );
#pod
#pod while (1) {
#pod
#pod # This inner loop will only run until no more changes are
#pod # available.
#pod while (my $change = $stream->next) {
#pod # process $change
#pod }
#pod }
#pod
#pod The returned stream will not block forever waiting for changes. If you
#pod want to respond to changes over a longer time use C<maxAwaitTimeMS> and
#pod regularly call C<next> in a loop.
#pod
#pod See L<MongoDB::Collection/watch> for details on usage and available
#pod options.
#pod
#pod =cut
sub watch {
my ( $self, $pipeline, $options ) = @_;
$pipeline ||= [];
$options ||= {};
my $session = $self->_get_session_from_hashref( $options );
return MongoDB::ChangeStream->new(
exists($options->{startAtOperationTime})
? (start_at_operation_time => delete $options->{startAtOperationTime})
: (),
exists($options->{fullDocument})
? (full_document => delete $options->{fullDocument})
: (full_document => 'default'),
exists($options->{resumeAfter})
? (resume_after => delete $options->{resumeAfter})
: (),
exists($options->{startAfter})
? (start_after => delete $options->{startAfter})
: (),
exists($options->{maxAwaitTimeMS})
? (max_await_time_ms => delete $options->{maxAwaitTimeMS})
: (),
client => $self,
all_changes_for_cluster => 1,
pipeline => $pipeline,
session => $session,
options => $options,
op_args => {
read_concern => $self->read_concern,
db_name => 'admin',,
coll_name => 1,
full_name => 'admin.1',
bson_codec => $self->bson_codec,
write_concern => $self->write_concern,
read_concern => $self->read_concern,
read_preference => $self->read_preference,
monitoring_callback => $self->monitoring_callback,
},
);
}
sub _primary_server_version {
my $self = shift;
my $build = $self->send_admin_command( [ buildInfo => 1 ] )->output;
my ($version_str) = $build->{version} =~ m{^([0-9.]+)};
return version->parse("v$version_str");
}
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
MongoDB::MongoClient - A connection to a MongoDB server or multi-server deployment
=head1 VERSION
version v2.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.
B<NOTE>: Options specified in the connection string take precedence over options
provided as constructor arguments.
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 app_name
This attribute specifies an application name that should be associated with
this client. The application name will be communicated to the server as
part of the initial connection handshake, and will appear in
connection-level and operation-level diagnostics on the server generated on
behalf of this client. This may be set in a connection string with the
C<appName> option.
The default is the empty string, which indicates a lack of an application
name.
The application name must not exceed 128 bytes.
=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 or C<authSource> URI option is provided,
it defaults to NONE. Otherwise, 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<BSON>. It may be initialized with a hash reference that will
be coerced into a new L<BSON> object.
If not provided, a L<BSON> object with default values will be generated.
=head2 compressors
An array reference of compression type names. Currently, C<zlib>, C<zstd> and
C<snappy> are supported.
=head2 zlib_compression_level
An integer from C<-1> to C<9> specifying the compression level to use
when L</compression> is set to C<zlib>.
B<Note>: When the special value C<-1> is given, the default compression
level will be used.
=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_staleness_seconds
The C<max_staleness_seconds> parameter represents the maximum replication lag in
seconds (wall clock time) that a secondary can suffer and still be
eligible for reads. The default is -1, which disables staleness checks.
Otherwise, it must be a positive integer.
B<Note>: this will only be used for server versions 3.4 or greater, as that
was when support for staleness tracking was added.
If the read preference mode is 'primary', then C<max_staleness_seconds> must not
be supplied.
The C<max_staleness_seconds> must be at least the C<heartbeat_frequency_ms>
plus 10 seconds (which is how often the server makes idle writes to the
oplog).
This may be set in a connection string with the C<maxStalenessSeconds> 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 monitoring_callback
Specifies a code reference used to receive monitoring events. See
L<MongoDB::Monitoring> for more details.
=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_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 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 retry_reads
=head2 retry_writes
Whether the client should use retryable writes for supported commands. The
default value is true, which means that commands which support retryable writes
will be retried on certain errors, such as C<not master> and C<node is
recovering> errors.
This may be set in a connection string with the C<retryWrites> option.
Note that this is only supported on MongoDB > 3.6 in Replica Set or Shard
Clusters, and will be ignored on other deployments.
Unacknowledged write operations also do not support retryable writes, even when
retry_writes has been enabled.
The supported single statement write operations are currently as follows:
=over 4
=item *
C<insert_one>
=item *
C<update_one>
=item *
C<replace_one>
=item *
C<delete_one>
=item *
C<find_one_and_delete>
=item *
C<find_one_and_replace>
=item *
C<find_one_and_update>
=back
The supported multi statement write operations are as follows:
=over 4
=item *
C<insert_many>
=item *
C<bulk_write>
=back
The multi statement operations may be ether ordered or unordered. Note that for
C<bulk_write> operations, the request may not include update_many or
delete_many operations.
=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 server_selector
Optional. This takes a function that augments the server selection rules.
The function takes as a parameter a list of server descriptions representing
the suitable servers for the read or write operation, and returns a list of
server descriptions that should still be considered suitable. Most users
should rely on the default server selection algorithm and should not need
to set this attribute.
=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.
The driver automatically sets the TCP keepalive option when initializing the
socket. For keepalive related issues, check the MongoDB documentation for
L<Does TCP keepalive time affect MongoDB Deployments?|https://docs.mongodb.com/v3.2/faq/diagnostics/#does-tcp-keepalive-time-affect-mongodb-deployments>.
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. (See
L<connection string URI|/CONNECTION STRING URI> for additional TLS
configuration options.)
=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. 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
If not set, the server default is used, which is typically "1".
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). If you set this to undef, it could block indefinitely
(or until socket timeout is reached).
See C<w> above for more information.
This may be set in a connection string with the C<wTimeoutMS> option.
=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>, if there is more than one server in the list of
hosts, the type will be 'Sharded'.
With only a single host and no replica set name, the topology type will
start as 'Direct' until the server is contacted the first time, after which
the type will be 'Sharded' for a mongos or 'Single' for standalone server
or direct connection to a replica set member.
=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. It also clears the session
cache. 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 start_session
$client->start_session;
$client->start_session( $options );
Returns a new L<MongoDB::ClientSession> with the supplied options.
will throw a C<MongoDB::ConfigurationError> if sessions are not supported by
the connected MongoDB deployment.
the options hash is an optional hash which can have the following keys:
=over 4
=item *
C<causalConsistency> - Enable Causally Consistent reads for this session. Defaults to true.
=back
for more information see L<MongoDB::ClientSession/options>.
=head2 list_databases
# get all information on all databases
my @dbs = $client->list_databases;
# get only the foo databases
my @foo_dbs = $client->list_databases({ filter => { name => qr/^foo/ } });
Lists all databases with information on each database. Supports filtering by
any of the output fields under the C<filter> argument, such as:
=over 4
=item *
C<name>
=item *
C<sizeOnDisk>
=item *
C<empty>
=item *
C<shards>
=back
=head2 database_names
my @dbs = $client->database_names;
# get only the foo database names
my @foo_dbs = $client->database_names({ filter => { name => qr/^foo/ } });
List of all database names on the MongoDB server. Supports filters in the same
way as L</"list_databases">.
=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.
=head2 watch
Watches for changes on the cluster.
Perform an aggregation with an implicit initial C<$changeStream> stage
and returns a L<MongoDB::ChangeStream> result which can be used to
iterate over the changes in the cluster. This functionality is
available since MongoDB 4.0.
my $stream = $client->watch();
my $stream = $client->watch( \@pipeline );
my $stream = $client->watch( \@pipeline, \%options );
while (1) {
# This inner loop will only run until no more changes are
# available.
while (my $change = $stream->next) {
# process $change
}
}
The returned stream will not block forever waiting for changes. If you
want to respond to changes over a longer time use C<maxAwaitTimeMS> and
regularly call C<next> in a loop.
See L<MongoDB::Collection/watch> for details on usage and available
options.
=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.
NOTE: any non-printable ASCII characters should be UTF-8 encoded and converted
URL-escaped characters.
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
=item *
C<appName>
=item *
C<authMechanism>
=item *
C<authMechanismProperties>
=item *
C<authSource>
=item *
C<compressors>
=item *
C<connect>
=item *
C<connectTimeoutMS>
=item *
C<heartbeatFrequencyMS>
=item *
C<journal>
=item *
C<localThresholdMS>
=item *
C<maxStalenessSeconds>
=item *
C<maxTimeMS>
=item *
C<readConcernLevel>
=item *
C<readPreference>
=item *
C<readPreferenceTags>
=item *
C<replicaSet>
=item *
C<retryReads>
=item *
C<retryWrites>
=item *
C<serverSelectionTimeoutMS>
=item *
C<serverSelectionTryOnce>
=item *
C<socketCheckIntervalMS>
=item *
C<socketTimeoutMS>
=item *
C<ssl>
=item *
C<tls> (an alias for C<ssl>)
=item *
C<tlsAllowInvalidCertificates>
=item *
C<tlsAllowInvalidHostnames>
=item *
C<tlsCAFile>
=item *
C<tlsCertificateKeyFile>
=item *
C<tlsCertificateKeyFilePassword>
=item *
C<tlsInsecure>
=item *
C<w>
=item *
C<wTimeoutMS>
=item *
C<zlibCompressionLevel>
=back
B<NOTE>: Options specified in the connection string take precedence over options
provided as constructor arguments.
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 are 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), application-provided server selector, 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 by filtering with server
selector and then 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 TRANSPORT LAYER SECURITY
B<Warning>: industry best practices, and some regulations, require the use
of TLS 1.1 or newer.
Some operating systems or versions may not provide an OpenSSL version new
enough to support the latest TLS protocols. If your OpenSSL library
version number is less than 1.0.1, then support for TLS 1.1 or newer is not
available. Contact your operating system vendor for a solution or upgrade
to a newer operating system distribution.
See also the documentation for L<Net::SSLeay> for details on installing and
compiling against OpenSSL.
TLS connections in the driver rely on the default settings provided by
L<IO::Socket::SSL>, but allow you to pass custom configuration to it.
Please read its documentation carefully to see how to control your TLS
configuration.
=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:
B<NOTE>: MONGODB-CR was deprecated with the release of MongoDB 3.6 and
is no longer supported by MongoDB 4.0.
=over 4
=item *
MONGODB-CR -- legacy username-password challenge-response (< 4.0)
=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 mechanisms 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
defined in 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>), requires
that a client certificate be configured in the ssl parameters, and requires
specifying 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",
);
B<Note>: Since MongoDB Perl driver v1.8.0, you no longer need to specify a
C<username> parameter for X509 authentication; the username will be
extracted automatically from the certificate.
=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.example.com/?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.
B<NOTE>: Per L<threads> documentation, use of Perl threads is discouraged by the
maintainers of Perl and the MongoDB Perl driver does not test or provide support
for use with threads.
=head1 AUTHORS
=over 4
=item *
David Golden <david@mongodb.com>
=item *
Rassi <rassi@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) 2020 by MongoDB, Inc.
This is free software, licensed under:
The Apache License, Version 2.0, January 2004
=cut
|