Apart from running a command from within a shell, there are different ways MongoDB commands can be executed. So for example, let me just exit from the Mongo shell. And then I'm back in the C drive users, you know, folder command bound. So I can say Mongo, and then followed by a dash dash eval, followed by a command DB dot, contacts dot find one, and then the name of the database that it has to connect to, when I want to issue this command, so which is test two DB in this case, and you can see that it connects to the local host, and to 7017 that is the default port connects to test DB and then issues that command. Obviously, this would be a very difficult mechanism. If you want to issue more than one command.
If I type Mongo and a dash dash event, and use test to dB semicolon, that's it. DB dot contracts dot find one, you will see that it's going to result into an error. So what we can do is alternately, we can create a script file and then execute the script file using the Mongo command. This would be a good option, especially when you're learning. So in order to test various commands, let's create a work folder for ourselves. So let's go to our C Drive, create a new folder called MongoDB.
Examples and then use the same thing in Sublime Text. So now we have a folder and files. Let's also get into that directory. So let's go CD At MongoDB dash examples, and we can issue the command from here now. So let's create a new file, let's call this as x 01 dot j s. So let's say we want to issue more than one command here, for example, I want to connect to a database called test two dB, and then semicolon. And then I can say, db dot contacts dot find one.
And then I can say here, war count equals two DB dot contacts dot count. And then I can even say print. There are plus count contacts. So now I have a script that actually can do some work. And then I want to execute the same thing here. So I can come back to the command prompt, and then I'll say Mongo, and then a less than symbol you I can input redirection and then I'll say x 01 dot j s. Now this should connect to the localhost 27017 use the test DB and then issue the rest of the commands over there.
And as you can see that the commands have been successfully executed. So in order to test various commands, let us follow this approach