In our previous tutorial, we have learnt about MongoDB Shell. There is a little difference how Mongo Script execution behaves from that of a Mongo Shell. We shall learn about these differences in next tutorial.
Example – Mongo Script
Let us quickly look into an example of Mongo Script.
1. Start Mongo Daemon
Run the following command in terminal.
sudo mongod --port 27017 --dbpath /var/lib/mongodb
Now Mongo Daemon would be waiting for connections on port 27017.
// equivalent for "use <db>" command in mongo shell db = db.getSiblingDB('tutorialkart') // print the collections present in tutorialkart db print(db.getCollectionNames())
~/workspace/mongo$ mongo mongo-script-example.js MongoDB shell version v3.4.10 connecting to: mongodb://127.0.0.1:27017 MongoDB server version: 3.4.10 customers,myNewCollection,people
First three lines in the output mention about the mongo shell version, and the URL of the mongo server it is trying to connect to and finally once the connection is successful, it prints the mongo server version.