{"id":7468,"date":"2023-11-04T00:33:22","date_gmt":"2023-11-03T19:03:22","guid":{"rendered":"https:\/\/pheonixsolutions.com\/blog\/?p=7468"},"modified":"2023-11-04T00:35:46","modified_gmt":"2023-11-03T19:05:46","slug":"how-to-clear-the-data-from-mongo-db-collection","status":"publish","type":"post","link":"https:\/\/pheonixsolutions.com\/blog\/how-to-clear-the-data-from-mongo-db-collection\/","title":{"rendered":"How to clear the data from a Mongo DB collection"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Introduction<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A collection is&nbsp;a grouping of&nbsp;MongoDB&nbsp;documents. Documents within a collection can have different fields. A collection is the equivalent of a table in a relational database system. A collection exists within a single database<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Prerequisites<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Database name<\/li>\n\n\n\n<li>Database user name<\/li>\n\n\n\n<li>Database password <\/li>\n\n\n\n<li>A user with sudo privileges for SSH connection<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">Implementation<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Step 1: SSH the server where the Mongo database is running<br><br>$ ssh user@IP<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Step 2: Log in to the Mongo database<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">$ mongo<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2023\/11\/image.png\"><img fetchpriority=\"high\" decoding=\"async\" width=\"1024\" height=\"49\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2023\/11\/image-1024x49.png\" alt=\"\" class=\"wp-image-7469\" srcset=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2023\/11\/image-1024x49.png 1024w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2023\/11\/image-300x14.png 300w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2023\/11\/image-768x36.png 768w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2023\/11\/image-850x40.png 850w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2023\/11\/image.png 1182w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Step 3: List the databases and switch to the required database where we need to clear the data<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">$ show dbs;<br>$ use &lt;DB-name&gt;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Step 4:  List the collections<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">$ show collections<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2023\/11\/image-1.png\"><img decoding=\"async\" width=\"406\" height=\"124\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2023\/11\/image-1.png\" alt=\"\" class=\"wp-image-7471\" srcset=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2023\/11\/image-1.png 406w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2023\/11\/image-1-300x92.png 300w\" sizes=\"(max-width: 406px) 100vw, 406px\" \/><\/a><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Step 5: Clear the data from the collection with the below query without deleting that collection from the  database<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">$ db.collection-name.deleteMany({})<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction A collection is&nbsp;a grouping of&nbsp;MongoDB&nbsp;documents. Documents within a collection can have different fields. A collection is the equivalent of [&hellip;]<\/p>\n","protected":false},"author":495,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1022],"tags":[983],"class_list":["post-7468","post","type-post","status-publish","format-standard","hentry","category-web-architecture","tag-mongo-db-collection","psol-cat-web-architecture"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/phn2x7-1Ws","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/7468","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/users\/495"}],"replies":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=7468"}],"version-history":[{"count":0,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/7468\/revisions"}],"wp:attachment":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=7468"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=7468"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=7468"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}