One place for hosting & domains

      July 2021

      How To Use the MongoDB Shell


      The author selected the Open Internet/Free Speech Fund to receive a donation as part of the Write for DOnations program.

      Introduction

      Database systems such as MongoDB are typically used with an external application that connects to the database server and performs operations, such as reading and processing the data or writing new entries. In cases like this, you are not interacting with the database server directly. To perform administrative tasks on the database or execute ad-hoc database queries yourself, though, direct access might be needed.

      That’s where the MongoDB shell comes in. The MongoDB shell is an interactive console you can use to connect to the database server and execute commands on it, allowing you to perform administrative tasks and read, write, or manipulate data directly. The MongoDB shell enables you to connect to the database from a command line prompt and interactively work with it from a terminal window. It also allows you to run external scripts to perform repeated tasks with greater convenience.

      In this tutorial, you’ll use the MongoDB shell to connect to a MongoDB database and query the database interactively. You will also use the built-in help system and autocompletion features included in the shell.

      Prerequisites

      To follow this tutorial, you will need:

      Note: The linked tutorials on how to configure your server, install, and then secure MongoDB installation refer to Ubuntu 20.04. This tutorial concentrates on MongoDB itself, not the underlying operating system. It will generally work with any MongoDB installation regardless of the operating system as long as authentication has been enabled.

      Step 1 — Connecting to the MongoDB Server

      To open up the MongoDB shell, run the mongo command from your server prompt. By default, the mongo command opens a shell connected to a locally-installed MongoDB instance running on port 27017.

      Try running the mongo command with no additional parameters:

      This will print a welcome message with some information about the server the shell is connected to, as well as what version of MongoDB is installed. The following example indicates that the MongoDB server is running on 127.0.0.1 (a loopback interface representing localhost) on MongoDB’s default port (27017) and running version 4.4.6.

      Output

      MongoDB shell version v4.4.6 connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb Implicit session: session { "id" : UUID("b9a48dc7-e821-4b09-a753-429eedf072c5") } MongoDB server version: 4.4.6

      Below this message, the MongoDB shell’s prompt — indicated by a greater-than sign — will appear:

      Try listing all the databases available on the server. Type show dbs after the shell prompt and press ENTER:

      Assuming you followed the prerequisite tutorial on How To Secure MongoDB, this command won’t return any output. The reason for this is that, even though the MongoDB server is running and the shell was able to connect to it, you didn’t provide any authentication information. Because of this, you have no access rights to work with any of the server databases and the show dbs command returns nothing.

      Exit the shell by typing:

      The Mongo shell will print a brief goodbye message and return you to the system shell:

      Output

      bye

      Note: Instead of typing the exit command, an alternative way to close the shell is to press CTRL + C instead.

      Now try reconnecting the MongoDB shell to the database server, but this time provide a username and password to properly authenticate into your MongoDB instance. To do so, you’ll need to provide additional command line parameters, as in the following example:

      • mongo -u AdminSammy -p --authenticationDatabase admin

      This command consists of several parts:

      • -u: this flag sets the username used to authenticate into the MongoDB server. This example specifies the administrative user created in the prerequisite MongoDB security tutorial, AdminSammy. For this tutorial, you can replace this with your own administrative user’s username if different.
      • -p: this flag tells the MongoDB shell to use a password when connecting to the database. You will be prompted to provide a password in the terminal window after you press ENTER.
      • --authenticationDatabase: this option specifies the authentication database of the user you’re logging in as. Typically, administrative accounts are managed in the admin database, but if your user’s authentication database is different, enter that database in place of admin.

      Note: To connect to a MongoDB server running on a different machine than localhost, you can add the -h flag followed by your server’s IP address to the shell command.

      Enter the password set during installation and you’ll once again get access to the shell.

      Now try executing the show dbs command once again:

      This time, the command will return a list of all available databases in the system:

      Output

      admin 0.000GB config 0.000GB local 0.000GB

      Because you’ve authenticated as a privileged user, the shell will allow you to run commands on any of these databases.

      Now that you have successfully connected to the MongoDB server using a MongoDB shell, you can move on to learning how to execute commands in the shell.

      Step 2 — Executing Commands

      As with other command line interfaces, the MongoDB shell accepts commands and returns the desired results to standard output. As mentioned previously, in MongoDB shell, all commands are typed into the command prompt denoted with the greater-than sign (>). Pressing ENTER after the command immediately executes it and returns the command output to the screen.

      Most commands in MongoDB database are executed on a database or on a collection in a selected database. The currently-selected database is represented by the db object accessible through the shell. You can check which database is currently selected by typing db into the shell:

      On a freshly-connected shell instance, the selected database is always called test:

      Output

      test

      You can safely use this database to experiment with MongoDB and the MongoDB shell. To switch to another database, you can run the use command followed by the new database name. Try switching to a database called fruits:

      The shell will inform you that you’re now using the new database:

      Output

      switched to db fruits

      You can verify this by typing db again to find the name of the currently-selected database:

      This time, the output will reflect the new database:

      Output

      fruits

      Notice that you haven’t explicitly created the fruits database. MongoDB allows you to run commands on databases and collections that don’t yet exist; it only creates these structures when an object is first inserted into them. Even though you have successfully changed the current database to fruits, this database does not exist yet.

      Try creating this database by inserting an object into it. The following example outlines how to insert an object into a collection within the database called apples. By adding this object, the operation will create both the fruits database and the apples collection.

      Type the following line into the MongoDB shell and press ENTER. Notice the highlighted collection name (apples):

      Pressing ENTER after an open parenthesis will start a multi-line command prompt, allowing you to enter longer commands in more than one line. The insert command won’t register as complete until you enter a closing parenthesis. Until you do, the prompt will change from the greater-than sign to an ellipsis (...).

      You don’t need to break up MongoDB commands into multiple lines like this, but doing so can make long commands easier to read and understand.

      On the next line, enter the object within a pair of curly brackets ({ and }). This example document has only one field and value pair:

      When you once again press ENTER, another line prompt will be shown, allowing you to add further command parameters, like other documents or any specifications allowed by MongoDB’s insert method. For this example, though, you can end your input and run the operation by entering a closing parenthesis and pressing ENTER:

      This time, the Mongo shell will register the ending of the insert command and execute the whole statement.

      Output

      WriteResult({ "nInserted" : 1 })

      After inserting this new object into the database, both the fruits database and apples collection will exist. Check the list of available databases with show dbs command:

      Again, this returns a list of all the available databases, but this time the list includes the fruits database:

      Output

      admin 0.000GB config 0.000GB fruits 0.000GB local 0.000GB

      To retrieve a list of collections available in the currently-selected database, the show collections command comes in handy:

      Since the fruits database is selected, it will only return the newly-created apples collection:

      Output

      apples

      With that, you’ve learned how to execute commands in the MongoDB shell. You also created a sample object, which in turn created a new database and a new collection that is now persisted on the server.

      In the final step of this guide, you’ll learn how to invoke the MongoDB shell’s help features to better understand commands and execute them with greater ease.

      Step 3 — Getting Interactive Help from the Shell

      The MongoDB shell has a built-in help system that you can use to get information on database system’s available commands and the objects stored within it. This introductory help screen can be accessed with the help command directly in the shell prompt:

      The MongoDB shell will return a list of more detailed help entries you can use to learn about more specific parts of the shell, as well as with a few examples of most commonly used commands:

      Output

      db.help() help on db methods db.mycoll.help() help on collection methods sh.help() sharding helpers rs.help() replica set helpers help admin administrative help help connect connecting to a db help help keys key shortcuts help misc misc things to know help mr mapreduce show dbs show database names show collections show collections in current database show users show users in current database show profile show most recent system.profile entries with time >= 1ms show logs show the accessible logger names show log [name] prints out the last segment of log in memory, 'global' is default use <db_name> set current database db.mycoll.find() list objects in collection mycoll db.mycoll.find( { a : 1 } ) list objects in mycoll where a == 1 it result of the last line evaluated; use to further iterate DBQuery.shellBatchSize = x set default number of items to display on shell exit quit the mongo shell

      The first two entries, highlighted in this example output, illustrate how to execute a help command for the current database and a collection within the current database, respectively. The example collection name given is mycoll, but any valid collection name can be used.

      The last highlighted line — db.mycoll.find() — is an example of one way to retrieve an object from a collection using the find command, which lists objects in a given collection. Because collections are some of the most commonly used structures in a Mongo database, this step will go over how to use Mongo’s find() and help() collection-level methods.

      First, access the apples collection’s help screen to find the commands available for this collection:

      Note: While the initial help command was just help, when executing a help method on database and collection objects you must follow the command with a pair of parentheses so it reads as help() and not just help.

      The output for this command will be lengthy list of available commands you can perform on the apples collection:

      Output

      DBCollection help db.apples.find().help() - show DBCursor help db.apples.bulkWrite( operations, <optional params> ) - bulk execute write operations, optional parameters are: w, wtimeout, j db.apples.count( query = {}, <optional params> ) - count the number of documents that matches the query, optional parameters are: limit, skip, hint, maxTimeMS db.apples.countDocuments( query = {}, <optional params> ) - count the number of documents that matches the query, optional parameters are: limit, skip, hint, maxTimeMS . . . db.apples.find([query],[fields]) - query is an optional query filter. fields is optional set of fields to return. e.g. db.apples.find( {x:77} , {name:1, x:1} ) db.apples.find(...).count() db.apples.find(...).limit(n) db.apples.find(...).skip(n) db.apples.find(...).sort(...) . . .

      In addition to the pure list of available commands you can execute on the db.apples collection (each of which are followed by short descriptions of what the given command does), this help screen also gives usage examples for frequently-used commands such as find().

      This bundled help system serves as a useful reference of available commands. You can use it to check your syntax in case you don’t remember the exact spelling or the acceptable fields for a command.

      Since you’ve already learned from the help screens that find() can be used to retrieve objects from a collection, you can now try retrieving the object you created in the apples collection in the previous step.

      However, this example will highlight another of MongoDB’s interactive help aids known as command completion. The MongoDB shell follows the pattern found in other popular shells such as Bash or zsh in which pressing the keyboard’s TAB key will automatically complete whatever command you’re in the process of typing.

      Start by typing the following, but do not press ENTER yet:

      Instead of typing the collection name in full, press the TAB key on the keyboard. The MongoDB shell will respond with the list of all the available possibilities that begin with a:

      Output

      > db.a db.adminCommand( db.aggregate( db.apples db.auth(

      This output lists three commands, indicated by an opening parenthesis, as well as the apples collection.

      Type one more letter into the shell:

      Press TAB once more. This time, there are no other possibilities starting with ap and the MongoDB shell will automatically complete the entry and type db.apples for you. Follow the same principle and complete the find() command using TAB completion. Type fi but do not press ENTER:

      Pressing TAB will automatically complete the command name to db.apples.find. At this point, pressing TAB again would cause the shell to list further possibilities, but you can manually add a closing parenthesis to execute the find command:

      The shell will show a list of all the objects found in the apples collection. There will be only one object, the one you inserted previously:

      Output

      { "_id" : ObjectId("60f31447f5643f739b0276e9"), "name" : "Red Delicious" }

      With that, you’ve learned how to use the built-in help system and the Mongo shell’s autocomplete feature.

      Conclusion

      By reading this article, you will have familiarized yourself with the MongoDB shell. The shell makes it possible to insert new objects into the database, query the existing collections, and perform administrative tasks for managing the database, its data, and its users.

      The MongoDB shell exposes direct access to most of Mongo’s features, such as database and collection objects and methods, making it the primary tool to interactive with a database server. However, the same set of functions and methods can be used when working with MongoDB programmatically, either through MongoDB shell scripting or through the drivers MongoDB has made available for many programming languages.

      You can execute commands from other articles in this series in the MongoDB shell following the principles described in this tutorial. We encourage you to learn more about MongoDB Shell in the official MongoDB documentation.



      Source link

      How To Use docker exec to Run Commands in a Docker Container


      Introduction

      Docker is a containerization tool that helps developers create and manage portable, consistent Linux containers.

      When developing or deploying containers you’ll often need to look inside a running container to inspect its current state or debug a problem. To this end, Docker provides the docker exec command to run programs in containers that are already running.

      In this tutorial we will learn about the docker exec command and how to use it to run commands and get an interactive shell in a running Docker container.

      Prerequisites

      This tutorial assumes you already have Docker installed, and your user has permission to run docker. If you need to run docker as the root user, please remember to prepend sudo to the commands in this tutorial.

      For more information on using Docker without sudo access, please see the Executing the Docker Command Without Sudo section of our How To Install Docker tutorial.

      Starting a Test Container

      To use the docker exec command, you will need a running Docker container. If you don’t already have a container, start a test container with the following docker run command:

      • docker run -d --name container-name alpine watch "date >> /var/log/date.log"

      This command creates a new Docker container from the official alpine image. This is a popular Linux container image that uses Alpine Linux, a lightweight, minimal Linux distribution.

      We use the -d flag to detach the container from our terminal and run it in the background. --name container-name will name the container container-name. You could choose any name you like here, or leave this off entirely to have Docker automatically generate a unique name for the new container.

      Next we have alpine, which specifies the image we want to use for the container.

      And finally we have watch "date >> /var/log/date.log". This is the command we want to run in the container. watch will repeatedly run the command you give it, every two seconds by default. The command that watch will run in this case is date >> /var/log/date.log. date prints the current date and time, like this:

      Output

      • Fri Jul 23 14:57:05 UTC 2021

      The >> /var/log/date.log portion of the command redirects the output from date and appends it to the file /var/log/date.log. Every two seconds a new line will be appended to the file, and after a few seconds it will look something like this:

      Output

      Fri Jul 23 15:00:26 UTC 2021 Fri Jul 23 15:00:28 UTC 2021 Fri Jul 23 15:00:30 UTC 2021 Fri Jul 23 15:00:32 UTC 2021 Fri Jul 23 15:00:34 UTC 2021

      In the next step we’ll learn how to find the names of Docker containers. This will be useful if you already have a container you’re targeting, but you’re not sure what its name is.

      Finding the Name of a Docker Container

      We’ll need to provide docker exec with the name (or container ID) of the container we want to work with. We can find this information using the docker ps command:

      This command lists all of the Docker containers running on the server, and provides some high-level information about them:

      Output

      CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 76aded7112d4 alpine "watch 'date >> /var…" 11 seconds ago Up 10 seconds container-name

      In this example, the container ID and name are highlighted. You may use either to tell docker exec which container to use.

      If you’d like to rename your container, use the docker rename command:

      • docker rename container-name new-name

      Next, we’ll run through several examples of using docker exec to execute commands in a running Docker container.

      Running an Interactive Shell in a Docker Container

      If you need to start an interactive shell inside a Docker Container, perhaps to explore the filesystem or debug running processes, use docker exec with the -i and -t flags.

      The -i flag keeps input open to the container, and the -t flag creates a pseudo-terminal that the shell can attach to. These flags can be combined like this:

      • docker exec -it container-name sh

      This will run the sh shell in the specified container, giving you a basic shell prompt. To exit back out of the container, type exit then press ENTER:

      If your container image includes a more advanced shell such as bash, you could replace sh with bash above.

      Running a Non-interactive Command in a Docker Container

      If you need to run a command inside a running Docker container, but don’t need any interactivity, use the docker exec command without any flags:

      • docker exec container-name tail /var/log/date.log

      This command will run tail /var/log/date.log on the container-name container, and output the results. By default the tail command will print out the last ten lines of a file. If you’re running the demo container we set up in the first section, you will see something like this:

      Output

      Mon Jul 26 14:39:33 UTC 2021 Mon Jul 26 14:39:35 UTC 2021 Mon Jul 26 14:39:37 UTC 2021 Mon Jul 26 14:39:39 UTC 2021 Mon Jul 26 14:39:41 UTC 2021 Mon Jul 26 14:39:43 UTC 2021 Mon Jul 26 14:39:45 UTC 2021 Mon Jul 26 14:39:47 UTC 2021 Mon Jul 26 14:39:49 UTC 2021 Mon Jul 26 14:39:51 UTC 2021

      This is essentially the same as opening up an interactive shell for the Docker container (as done in the previous step with docker exec -it container-name sh) and then running the tail /var/log/date.log command. However, rather than opening up a shell, running the command, and then closing the shell, this command returns that same output in a single command and without opening up a pseudo-terminal.

      Running Commands in an Alternate Directory in a Docker Container

      To run a command in a certain directory of your container, use the --workdir flag to specify the directory:

      • docker exec --workdir /tmp container-name pwd

      This example command sets the /tmp directory as the working directory, then runs the pwd command, which prints out the present working directory:

      Output

      /tmp

      The pwd command has confirmed that the working directory is /tmp.

      Running Commands as a Different User in a Docker Container

      To run a command as a different user inside your container, add the --user flag:

      • docker exec --user guest container-name whoami

      This will use the guest user to run the whoami command in the container. The whoami command prints out the current user’s username:

      Output

      guest

      The whoami command confirms that the container’s current user is guest.

      Passing Environment Variables into a Docker Container

      Sometimes you need to pass environment variables into a container along with the command to run. The -e flag lets you specify an environment variable:

      • docker exec -e TEST=sammy container-name env

      This command sets the TEST environment variable to equal sammy, then runs the env command inside the container. The env command then prints out all the environment variables:

      Output

      PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin HOSTNAME=76aded7112d4 TEST=sammy HOME=/root

      The TEST variable is set to sammy.

      To set multiple variables, repeat the -e flag for each one:

      • docker exec -e TEST=sammy -e ENVIRONMENT=prod container-name env

      If you’d like to pass in a file full of environment variables you can do that with the --env-file flag.

      First, make the file with a text editor. We’ll open a new file with nano here, but you can use any editor you’re comfortable with:

      We’re using .env as the filename, as that’s a popular standard for using these sorts of files to manage information outside of version control.

      Write your KEY=value variables into the file, one per line, like the following:

      .env

      TEST=sammy
      ENVIRONMENT=prod
      

      Save and close the file. To save the file and exit nano, press CTRL+O, then ENTER to save, then CTRL+X to exit.

      Now run the docker exec command, specifying the correct filename after --env-file:

      • docker exec --env-file .env container-name env

      Output

      PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin HOSTNAME=76aded7112d4 TEST=sammy ENVIRONMENT=prod HOME=/root

      The two variables in the file are set.

      You may specify multiple files by using multiple --env-file flags. If the variables in the files overlap each other, whichever file was listed last in the command will override the previous files.

      Common Errors

      When using the docker exec command, you may encounter a few common errors:

      Error: No such container: container-name
      

      The No such container error means the specified container does not exist, and may indicate a misspelled container name. Use docker ps to list out your running containers and double-check the name.

      Error response from daemon: Container 2a94aae70ea5dc92a12e30b13d0613dd6ca5919174d73e62e29cb0f79db6e4ab is not running
      

      This not running message means that the container exists, but it is stopped. You can start the container with docker start container-name

      Error response from daemon: Container container-name is paused, unpause the container before exec
      

      The Container is paused error explains the problem fairly well. You need to unpause the container with docker unpause container-name before proceeding.

      Conclusion

      In this tutorial we learned how to execute commands in a running Docker container, along with some command line options available when doing so.

      For more information on Docker in general, please see our Docker tag page, which has links to Docker tutorials, Docker-related Q&A pages, and more.

      For help with installing Docker, take a look at How To Install and Use Docker on Ubuntu 20.04.



      Source link