[{"data":1,"prerenderedAt":583},["ShallowReactive",2],{"\u002Fdocs\u002Fintegrations\u002Fmongodb":3},{"id":4,"title":5,"body":6,"description":576,"extension":577,"meta":578,"navigation":67,"path":579,"seo":580,"stem":581,"__hash__":582},"docs\u002Fdocs\u002F3.integrations\u002F2.mongodb.md","MongoDB",{"type":7,"value":8,"toc":569},"minimark",[9,13,29,34,94,106,110,228,235,239,245,308,315,319,325,515,519,522,565],[10,11,5],"h1",{"id":12},"mongodb",[14,15,16,20,21,24,25,28],"p",{},[17,18,19],"code",{},"MongoRunner"," is the built-in ",[17,22,23],{},"NoSQLRunner"," implementation for MongoDB. It uses ",[17,26,27],{},"pymongo"," under the hood and exposes only read-only operations.",[30,31,33],"h2",{"id":32},"connect","Connect",[35,36,41],"pre",{"className":37,"code":38,"language":39,"meta":40,"style":40},"language-python shiki shiki-themes github-dark","from mango.integrations.mongodb import MongoRunner\n\ndb = MongoRunner()\ndb.connect(\"mongodb:\u002F\u002Flocalhost:27017\u002Fmydb\")\n","python","",[17,42,43,62,69,81],{"__ignoreMap":40},[44,45,48,52,56,59],"span",{"class":46,"line":47},"line",1,[44,49,51],{"class":50},"snl16","from",[44,53,55],{"class":54},"s95oV"," mango.integrations.mongodb ",[44,57,58],{"class":50},"import",[44,60,61],{"class":54}," MongoRunner\n",[44,63,65],{"class":46,"line":64},2,[44,66,68],{"emptyLinePlaceholder":67},true,"\n",[44,70,72,75,78],{"class":46,"line":71},3,[44,73,74],{"class":54},"db ",[44,76,77],{"class":50},"=",[44,79,80],{"class":54}," MongoRunner()\n",[44,82,84,87,91],{"class":46,"line":83},4,[44,85,86],{"class":54},"db.connect(",[44,88,90],{"class":89},"sU2Wk","\"mongodb:\u002F\u002Flocalhost:27017\u002Fmydb\"",[44,92,93],{"class":54},")\n",[14,95,96,97,101,102,105],{},"The database name ",[98,99,100],"strong",{},"must"," be part of the URI. Mango calls ",[17,103,104],{},"get_default_database()"," to extract it.",[30,107,109],{"id":108},"connection-string-formats","Connection string formats",[35,111,113],{"className":37,"code":112,"language":39,"meta":40,"style":40},"# Local\ndb.connect(\"mongodb:\u002F\u002Flocalhost:27017\u002Fmydb\")\n\n# With auth\ndb.connect(\"mongodb:\u002F\u002Fuser:password@localhost:27017\u002Fmydb\")\n\n# MongoDB Atlas\ndb.connect(\"mongodb+srv:\u002F\u002Fuser:password@cluster.mongodb.net\u002Fmydb\")\n\n# With options\ndb.connect(\n    \"mongodb:\u002F\u002Flocalhost:27017\u002Fmydb\",\n    serverSelectionTimeoutMS=5000,\n    connectTimeoutMS=10000,\n)\n",[17,114,115,121,129,133,138,148,153,159,169,174,180,186,195,210,223],{"__ignoreMap":40},[44,116,117],{"class":46,"line":47},[44,118,120],{"class":119},"sAwPA","# Local\n",[44,122,123,125,127],{"class":46,"line":64},[44,124,86],{"class":54},[44,126,90],{"class":89},[44,128,93],{"class":54},[44,130,131],{"class":46,"line":71},[44,132,68],{"emptyLinePlaceholder":67},[44,134,135],{"class":46,"line":83},[44,136,137],{"class":119},"# With auth\n",[44,139,141,143,146],{"class":46,"line":140},5,[44,142,86],{"class":54},[44,144,145],{"class":89},"\"mongodb:\u002F\u002Fuser:password@localhost:27017\u002Fmydb\"",[44,147,93],{"class":54},[44,149,151],{"class":46,"line":150},6,[44,152,68],{"emptyLinePlaceholder":67},[44,154,156],{"class":46,"line":155},7,[44,157,158],{"class":119},"# MongoDB Atlas\n",[44,160,162,164,167],{"class":46,"line":161},8,[44,163,86],{"class":54},[44,165,166],{"class":89},"\"mongodb+srv:\u002F\u002Fuser:password@cluster.mongodb.net\u002Fmydb\"",[44,168,93],{"class":54},[44,170,172],{"class":46,"line":171},9,[44,173,68],{"emptyLinePlaceholder":67},[44,175,177],{"class":46,"line":176},10,[44,178,179],{"class":119},"# With options\n",[44,181,183],{"class":46,"line":182},11,[44,184,185],{"class":54},"db.connect(\n",[44,187,189,192],{"class":46,"line":188},12,[44,190,191],{"class":89},"    \"mongodb:\u002F\u002Flocalhost:27017\u002Fmydb\"",[44,193,194],{"class":54},",\n",[44,196,198,202,204,208],{"class":46,"line":197},13,[44,199,201],{"class":200},"s9osk","    serverSelectionTimeoutMS",[44,203,77],{"class":50},[44,205,207],{"class":206},"sDLfK","5000",[44,209,194],{"class":54},[44,211,213,216,218,221],{"class":46,"line":212},14,[44,214,215],{"class":200},"    connectTimeoutMS",[44,217,77],{"class":50},[44,219,220],{"class":206},"10000",[44,222,194],{"class":54},[44,224,226],{"class":46,"line":225},15,[44,227,93],{"class":54},[14,229,230,231,234],{},"Any extra kwargs are passed directly to ",[17,232,233],{},"MongoClient",".",[30,236,238],{"id":237},"allowed-operations","Allowed operations",[14,240,241,244],{},[17,242,243],{},"MongoRunner.execute_query()"," only accepts these operations:",[246,247,248,261],"table",{},[249,250,251],"thead",{},[252,253,254,258],"tr",{},[255,256,257],"th",{},"Operation",[255,259,260],{},"Description",[262,263,264,275,285,298],"tbody",{},[252,265,266,272],{},[267,268,269],"td",{},[17,270,271],{},"find",[267,273,274],{},"Filter, project, sort, limit",[252,276,277,282],{},[267,278,279],{},[17,280,281],{},"aggregate",[267,283,284],{},"Full aggregation pipeline",[252,286,287,292],{},[267,288,289],{},[17,290,291],{},"count",[267,293,294,297],{},[17,295,296],{},"count_documents()"," with optional filter",[252,299,300,305],{},[267,301,302],{},[17,303,304],{},"distinct",[267,306,307],{},"Distinct values for a field",[14,309,310,311,314],{},"Any other operation raises ",[17,312,313],{},"ValidationError"," before hitting the database.",[30,316,318],{"id":317},"methods","Methods",[14,320,321,322,324],{},"All methods inherited from ",[17,323,23],{},":",[35,326,328],{"className":37,"code":327,"language":39,"meta":40,"style":40},"# Execute a query\nresult: pd.DataFrame = db.execute_query(QueryRequest(\n    operation=\"count\",\n    collection=\"users\",\n    filter={\"active\": True},\n))\n\n# List collections\ncollections: list[str] = db.list_collections()\n\n# Get schema for all collections\nschema: dict[str, SchemaInfo] = db.introspect_schema()\n\n# Sample documents\ndocs: list[dict] = db.get_sample_documents(\"orders\", n=5)\n\n# Index info\nindexes: list[dict] = db.get_indexes(\"orders\")\n",[17,329,330,335,345,357,369,391,396,400,405,421,425,430,445,449,454,485,490,496],{"__ignoreMap":40},[44,331,332],{"class":46,"line":47},[44,333,334],{"class":119},"# Execute a query\n",[44,336,337,340,342],{"class":46,"line":64},[44,338,339],{"class":54},"result: pd.DataFrame ",[44,341,77],{"class":50},[44,343,344],{"class":54}," db.execute_query(QueryRequest(\n",[44,346,347,350,352,355],{"class":46,"line":71},[44,348,349],{"class":200},"    operation",[44,351,77],{"class":50},[44,353,354],{"class":89},"\"count\"",[44,356,194],{"class":54},[44,358,359,362,364,367],{"class":46,"line":83},[44,360,361],{"class":200},"    collection",[44,363,77],{"class":50},[44,365,366],{"class":89},"\"users\"",[44,368,194],{"class":54},[44,370,371,374,376,379,382,385,388],{"class":46,"line":140},[44,372,373],{"class":200},"    filter",[44,375,77],{"class":50},[44,377,378],{"class":54},"{",[44,380,381],{"class":89},"\"active\"",[44,383,384],{"class":54},": ",[44,386,387],{"class":206},"True",[44,389,390],{"class":54},"},\n",[44,392,393],{"class":46,"line":150},[44,394,395],{"class":54},"))\n",[44,397,398],{"class":46,"line":155},[44,399,68],{"emptyLinePlaceholder":67},[44,401,402],{"class":46,"line":161},[44,403,404],{"class":119},"# List collections\n",[44,406,407,410,413,416,418],{"class":46,"line":171},[44,408,409],{"class":54},"collections: list[",[44,411,412],{"class":206},"str",[44,414,415],{"class":54},"] ",[44,417,77],{"class":50},[44,419,420],{"class":54}," db.list_collections()\n",[44,422,423],{"class":46,"line":176},[44,424,68],{"emptyLinePlaceholder":67},[44,426,427],{"class":46,"line":182},[44,428,429],{"class":119},"# Get schema for all collections\n",[44,431,432,435,437,440,442],{"class":46,"line":188},[44,433,434],{"class":54},"schema: dict[",[44,436,412],{"class":206},[44,438,439],{"class":54},", SchemaInfo] ",[44,441,77],{"class":50},[44,443,444],{"class":54}," db.introspect_schema()\n",[44,446,447],{"class":46,"line":197},[44,448,68],{"emptyLinePlaceholder":67},[44,450,451],{"class":46,"line":212},[44,452,453],{"class":119},"# Sample documents\n",[44,455,456,459,462,464,466,469,472,475,478,480,483],{"class":46,"line":225},[44,457,458],{"class":54},"docs: list[",[44,460,461],{"class":206},"dict",[44,463,415],{"class":54},[44,465,77],{"class":50},[44,467,468],{"class":54}," db.get_sample_documents(",[44,470,471],{"class":89},"\"orders\"",[44,473,474],{"class":54},", ",[44,476,477],{"class":200},"n",[44,479,77],{"class":50},[44,481,482],{"class":206},"5",[44,484,93],{"class":54},[44,486,488],{"class":46,"line":487},16,[44,489,68],{"emptyLinePlaceholder":67},[44,491,493],{"class":46,"line":492},17,[44,494,495],{"class":119},"# Index info\n",[44,497,499,502,504,506,508,511,513],{"class":46,"line":498},18,[44,500,501],{"class":54},"indexes: list[",[44,503,461],{"class":206},[44,505,415],{"class":54},[44,507,77],{"class":50},[44,509,510],{"class":54}," db.get_indexes(",[44,512,471],{"class":89},[44,514,93],{"class":54},[30,516,518],{"id":517},"error-handling","Error handling",[14,520,521],{},"All MongoDB errors are wrapped in Mango's exception hierarchy:",[246,523,524,534],{},[249,525,526],{},[252,527,528,531],{},[255,529,530],{},"Exception",[255,532,533],{},"When raised",[262,535,536,546,556],{},[252,537,538,543],{},[267,539,540],{},[17,541,542],{},"BackendError",[267,544,545],{},"Connection failure, collection not found",[252,547,548,553],{},[267,549,550],{},[17,551,552],{},"QueryError",[267,554,555],{},"MongoDB error during query execution",[252,557,558,562],{},[267,559,560],{},[17,561,313],{},[267,563,564],{},"Operation not in allowlist, missing required field",[566,567,568],"style",{},"html pre.shiki code .snl16, html code.shiki .snl16{--shiki-default:#F97583}html pre.shiki code .s95oV, html code.shiki .s95oV{--shiki-default:#E1E4E8}html pre.shiki code .sU2Wk, html code.shiki .sU2Wk{--shiki-default:#9ECBFF}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html pre.shiki code .sAwPA, html code.shiki .sAwPA{--shiki-default:#6A737D}html pre.shiki code .s9osk, html code.shiki .s9osk{--shiki-default:#FFAB70}html pre.shiki code .sDLfK, html code.shiki .sDLfK{--shiki-default:#79B8FF}",{"title":40,"searchDepth":64,"depth":64,"links":570},[571,572,573,574,575],{"id":32,"depth":64,"text":33},{"id":108,"depth":64,"text":109},{"id":237,"depth":64,"text":238},{"id":317,"depth":64,"text":318},{"id":517,"depth":64,"text":518},"Connecting and configuring the MongoDB backend.","md",{},"\u002Fdocs\u002Fintegrations\u002Fmongodb",{"title":5,"description":576},"docs\u002F3.integrations\u002F2.mongodb","Nz5zPYHU2wmNCFemZc-T-xDDEkJZX4BcDa9MfHo8_mw",1776189332075]