{"id":29,"date":"2023-04-26T16:29:39","date_gmt":"2023-04-26T16:29:39","guid":{"rendered":"http:\/\/77interactive.com\/?p=29"},"modified":"2023-05-10T22:06:49","modified_gmt":"2023-05-10T22:06:49","slug":"python-record-counts","status":"publish","type":"post","link":"http:\/\/77interactive.com\/?p=29","title":{"rendered":"Python Record Counts"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>import pymongo\nfrom pymongo import MongoClient\nfrom datetime import datetime\n\n# Replace the credentials and host with your own\nsource_uri = \"mongodb:\/\/source_username:source_password@source_host:source_port\/\"\ndest_uri = \"mongodb:\/\/dest_username:dest_password@dest_host:dest_port\/\"\n\nsource_client = MongoClient(source_uri)\ndest_client = MongoClient(dest_uri)\n\n# Connect to the DBA database and CollectionCounts collection in the destination MongoDB server\ndba = dest_client&#91;\"DBA\"]\ncollection_counts = dba&#91;\"CollectionCounts\"]\n\n# Remove all existing documents in the CollectionCounts collection\ncollection_counts.delete_many({})\n\n# Get the current local timestamp\ntimestamp = datetime.now()\n\nfor db_name in source_client.list_database_names():\n    if db_name not in &#91;\"admin\", \"local\", \"config\"]:\n        db = source_client&#91;db_name]\n        for collection_name in db.list_collection_names():\n            collection = db&#91;collection_name]\n            count = collection.count_documents({})\n            document = {\n                \"database\": db_name,\n                \"collection\": collection_name,\n                \"count\": count,\n                \"lastUpdated\": timestamp\n            }\n            collection_counts.insert_one(document)\n\nprint(\"Collection counts with the lastUpdated field have been stored in the DBA.CollectionCounts collection.\")\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Version 2<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>import pymongo\nfrom pymongo import MongoClient\nfrom datetime import datetime\n\n# Replace the credentials and host with your own\nsource_uri = \"mongodb:\/\/source_username:source_password@source_host:source_port\/\"\ndest_uri = \"mongodb:\/\/dest_username:dest_password@dest_host:dest_port\/\"\n\nsource_client = MongoClient(source_uri)\ndest_client = MongoClient(dest_uri)\n\n# Connect to the DBA database and CollectionCounts collection in the destination MongoDB server\ndba = dest_client&#91;\"DBA\"]\ncollection_counts = dba&#91;\"CollectionCounts\"]\n\n# Remove all existing documents in the CollectionCounts collection\ncollection_counts.delete_many({})\n\n# Get the current local timestamp\ntimestamp = datetime.now()\n\nfor db_name in source_client.list_database_names():\n    if db_name not in &#91;\"admin\", \"local\", \"config\"]:\n        db = source_client&#91;db_name]\n        \n        # Filter collections to exclude views\n        collections = &#91;coll_info&#91;'name'] for coll_info in db.list_collections() if 'viewOn' not in coll_info&#91;'options']]\n        \n        for collection_name in collections:\n            collection = db&#91;collection_name]\n            count = collection.count_documents({})\n            document = {\n                \"database\": db_name,\n                \"collection\": collection_name,\n                \"count\": count,\n                \"lastUpdated\": timestamp\n            }\n            collection_counts.insert_one(document)\n\nprint(\"Collection counts with the lastUpdated field have been stored in the DBA.CollectionCounts collection.\")\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Version 2<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[5,4,3],"class_list":["post-29","post","type-post","status-publish","format-standard","hentry","category-python","tag-mongodb","tag-pymongo","tag-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python Record Counts - 77 Interactive<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/77interactive.com\/?p=29\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Record Counts - 77 Interactive\" \/>\n<meta property=\"og:description\" content=\"Version 2\" \/>\n<meta property=\"og:url\" content=\"http:\/\/77interactive.com\/?p=29\" \/>\n<meta property=\"og:site_name\" content=\"77 Interactive\" \/>\n<meta property=\"article:published_time\" content=\"2023-04-26T16:29:39+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-05-10T22:06:49+00:00\" \/>\n<meta name=\"author\" content=\"Rudy\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Rudy\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"http:\/\/77interactive.com\/?p=29#article\",\"isPartOf\":{\"@id\":\"http:\/\/77interactive.com\/?p=29\"},\"author\":{\"name\":\"Rudy\",\"@id\":\"http:\/\/77interactive.com\/#\/schema\/person\/0e61d2a984b8304618026b207e6121e9\"},\"headline\":\"Python Record Counts\",\"datePublished\":\"2023-04-26T16:29:39+00:00\",\"dateModified\":\"2023-05-10T22:06:49+00:00\",\"mainEntityOfPage\":{\"@id\":\"http:\/\/77interactive.com\/?p=29\"},\"wordCount\":4,\"keywords\":[\"mongodb\",\"pymongo\",\"python\"],\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"http:\/\/77interactive.com\/?p=29\",\"url\":\"http:\/\/77interactive.com\/?p=29\",\"name\":\"Python Record Counts - 77 Interactive\",\"isPartOf\":{\"@id\":\"http:\/\/77interactive.com\/#website\"},\"datePublished\":\"2023-04-26T16:29:39+00:00\",\"dateModified\":\"2023-05-10T22:06:49+00:00\",\"author\":{\"@id\":\"http:\/\/77interactive.com\/#\/schema\/person\/0e61d2a984b8304618026b207e6121e9\"},\"breadcrumb\":{\"@id\":\"http:\/\/77interactive.com\/?p=29#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/77interactive.com\/?p=29\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/77interactive.com\/?p=29#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/77interactive.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python Record Counts\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/77interactive.com\/#website\",\"url\":\"http:\/\/77interactive.com\/\",\"name\":\"77 Interactive\",\"description\":\"Rudy&#039;s Code snippets\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/77interactive.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"http:\/\/77interactive.com\/#\/schema\/person\/0e61d2a984b8304618026b207e6121e9\",\"name\":\"Rudy\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/77interactive.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/e336b9aecd39b40691ff8ccfcd68506415072dbe8caffc0485b94a1bc22b774d?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/e336b9aecd39b40691ff8ccfcd68506415072dbe8caffc0485b94a1bc22b774d?s=96&d=mm&r=g\",\"caption\":\"Rudy\"},\"url\":\"http:\/\/77interactive.com\/?author=1\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Record Counts - 77 Interactive","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/77interactive.com\/?p=29","og_locale":"en_US","og_type":"article","og_title":"Python Record Counts - 77 Interactive","og_description":"Version 2","og_url":"http:\/\/77interactive.com\/?p=29","og_site_name":"77 Interactive","article_published_time":"2023-04-26T16:29:39+00:00","article_modified_time":"2023-05-10T22:06:49+00:00","author":"Rudy","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Rudy","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"http:\/\/77interactive.com\/?p=29#article","isPartOf":{"@id":"http:\/\/77interactive.com\/?p=29"},"author":{"name":"Rudy","@id":"http:\/\/77interactive.com\/#\/schema\/person\/0e61d2a984b8304618026b207e6121e9"},"headline":"Python Record Counts","datePublished":"2023-04-26T16:29:39+00:00","dateModified":"2023-05-10T22:06:49+00:00","mainEntityOfPage":{"@id":"http:\/\/77interactive.com\/?p=29"},"wordCount":4,"keywords":["mongodb","pymongo","python"],"articleSection":["Python"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"http:\/\/77interactive.com\/?p=29","url":"http:\/\/77interactive.com\/?p=29","name":"Python Record Counts - 77 Interactive","isPartOf":{"@id":"http:\/\/77interactive.com\/#website"},"datePublished":"2023-04-26T16:29:39+00:00","dateModified":"2023-05-10T22:06:49+00:00","author":{"@id":"http:\/\/77interactive.com\/#\/schema\/person\/0e61d2a984b8304618026b207e6121e9"},"breadcrumb":{"@id":"http:\/\/77interactive.com\/?p=29#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/77interactive.com\/?p=29"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/77interactive.com\/?p=29#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/77interactive.com\/"},{"@type":"ListItem","position":2,"name":"Python Record Counts"}]},{"@type":"WebSite","@id":"http:\/\/77interactive.com\/#website","url":"http:\/\/77interactive.com\/","name":"77 Interactive","description":"Rudy&#039;s Code snippets","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/77interactive.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"http:\/\/77interactive.com\/#\/schema\/person\/0e61d2a984b8304618026b207e6121e9","name":"Rudy","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/77interactive.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/e336b9aecd39b40691ff8ccfcd68506415072dbe8caffc0485b94a1bc22b774d?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/e336b9aecd39b40691ff8ccfcd68506415072dbe8caffc0485b94a1bc22b774d?s=96&d=mm&r=g","caption":"Rudy"},"url":"http:\/\/77interactive.com\/?author=1"}]}},"_links":{"self":[{"href":"http:\/\/77interactive.com\/index.php?rest_route=\/wp\/v2\/posts\/29","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/77interactive.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/77interactive.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/77interactive.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/77interactive.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=29"}],"version-history":[{"count":0,"href":"http:\/\/77interactive.com\/index.php?rest_route=\/wp\/v2\/posts\/29\/revisions"}],"wp:attachment":[{"href":"http:\/\/77interactive.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=29"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/77interactive.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=29"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/77interactive.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=29"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}