# Mongo example ## Overview * mongo-shell lets you talk directly to mongo * command is `mongo` * mongo has multiple databases * pulp_database is the one we want * you choose your default-database with `use pulp_database` * mongo stores documents in collections * you can show the available collections using `show collections` * command format is `db.COLLECTION.command` * example: `db.iso_units.find().count()` * useful commands * `.find()` * `.limit(1)` * `.count()` * `.update( {FILTER}, {OPERATION} )` ## Specific annotated example to change the name of one ISO unit: * Connect ``` [root@dhcp-3-121 ~]# mongo MongoDB shell version v3.4.9 connecting to: mongodb://127.0.0.1:27017 MongoDB server version: 3.4.9 Server has startup warnings: 2022-10-31T10:25:24.788-0400 I CONTROL [initandlisten] 2022-10-31T10:25:24.788-0400 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. 2022-10-31T10:25:24.788-0400 I CONTROL [initandlisten] ** We suggest setting it to 'never' 2022-10-31T10:25:24.788-0400 I CONTROL [initandlisten] 2022-10-31T10:25:24.788-0400 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. 2022-10-31T10:25:24.789-0400 I CONTROL [initandlisten] ** We suggest setting it to 'never' 2022-10-31T10:25:24.789-0400 I CONTROL [initandlisten] ``` * Choose db ``` > use pulp_database switched to db pulp_database ``` * What collections do we have to work with? ``` > show collections celery_beat_lock consumer_bindings consumer_history consumer_unit_profiles content_types deferred_download erratum_pkglists event_listeners lazy_content_catalog migration_trackers orphaned_profile_hash permissions repo_content_units repo_distributors repo_group_publish_results repo_importers repo_profile_applicability repo_publish_results repo_sync_results repos reserved_resources resource_manager_lock roles scheduled_calls task_status units_distribution units_docker_blob units_docker_image units_docker_manifest units_docker_manifest_list units_docker_tag units_drpm units_erratum units_iso units_modulemd units_modulemd_defaults units_ostree units_package_category units_package_environment units_package_group units_package_langpacks units_puppet_module units_rpm units_srpm units_yum_repo_metadata_file users workers > ``` * How many File units are there? ``` > db.units_iso.count() 2 ``` * Show just one so we know what document-fields look like: ``` > db.units_iso.find().limit(1) { "_id" : "1f162d5c-2df0-4106-8962-871c60ce4b6e", "pulp_user_metadata" : { }, "_last_updated" : 1667316670, "_storage_path" : "/var/lib/pulp/content/units/iso/05/5fb578119dbf971e7310e6ac06d639bb5af1155089be7ab3c9ad121dd5f6c7", "downloaded" : true, "name" : "2.iso", "checksum" : "d34e9a108c6ee81e094cd6e26955686c9ba9eb036b4f0d64c82ebe1bebb73b5d", "size" : 10485760, "_ns" : "units_iso", "_content_type_id" : "iso" } ``` * Find specific ISO ("1.iso") ``` > db.units_iso.find( {name: { $eq: "1.iso" } }) { "_id" : "8d939e4f-c743-4bb3-a365-809cdfbb36f8", "pulp_user_metadata" : { }, "_last_updated" : 1667316670, "_storage_path" : "/var/lib/pulp/content/units/iso/07/b4c128c58e7e1bd281fb03575a71c2371ff0b8656e157d35a6b0aa5b3ecba0", "downloaded" : true, "name" : "1.iso", "checksum" : "1c4ef7df278004ece0ba14e0ed69c0a4e69762dc7e12cf7ee150b21ad612f42c", "size" : 10485760, "_ns" : "units_iso", "_content_type_id" : "iso" } ``` * Change the name of 2.iso to 1.iso ``` > db.units_iso.update( {name: { $eq: "2.iso" } }, { $set: {name: "1.iso"}} ) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) ``` * Find 1.iso ``` > db.units_iso.find( {name: { $eq: "1.iso" } }) { "_id" : "8d939e4f-c743-4bb3-a365-809cdfbb36f8", "pulp_user_metadata" : { }, "_last_updated" : 1667316670, "_storage_path" : "/var/lib/pulp/content/units/iso/07/b4c128c58e7e1bd281fb03575a71c2371ff0b8656e157d35a6b0aa5b3ecba0", "downloaded" : true, "name" : "1.iso", "checksum" : "1c4ef7df278004ece0ba14e0ed69c0a4e69762dc7e12cf7ee150b21ad612f42c", "size" : 10485760, "_ns" : "units_iso", "_content_type_id" : "iso" } { "_id" : "1f162d5c-2df0-4106-8962-871c60ce4b6e", "pulp_user_metadata" : { }, "_last_updated" : 1667316670, "_storage_path" : "/var/lib/pulp/content/units/iso/05/5fb578119dbf971e7310e6ac06d639bb5af1155089be7ab3c9ad121dd5f6c7", "downloaded" : true, "name" : "1.iso", "checksum" : "d34e9a108c6ee81e094cd6e26955686c9ba9eb036b4f0d64c82ebe1bebb73b5d", "size" : 10485760, "_ns" : "units_iso", "_content_type_id" : "iso" } > ``` * **Rename that actually reproduced the problem we were going after:** ``` db.units_iso.update( { name: { $eq: "2.iso" } }, { $set: {name: "1.iso/22.iso"} } } ) ```