Should I learn the MongoDB shell commands first or my language specific drivers?

Have a Database Problem? Speak with an Expert for Free
Get Started >>

Introduction

If you’re a developer beginning to learn MongoDB you’ll find that a lot of the training materials out there teach you about MongoDB through shell commands. Let’s say you’re working with Javascript or PHP, well these shell commands aren’t immediately applicable to what you’re doing. This can be a little discouraging to learn something that you can’t immediately implement. In this article we’ll talk about why tutorials do that, why you might consider learning directly through drivers, and which option we think is best for different scenarios.

Why so many online tutorials only teach the MongoDB shell?

Most tutorials and classes online will teach the MongoDB shell because it is universal to anybody using MongoDB. Wherever you are running the MongoDB shell, the syntax is the same no matter what environment you are running it on. So whether you intend to interact with MongoDB through NodeJS, PHP, Python, C++, or Ruby, the shell is common ground because it comes built into MongoDB wherever you install it or however you intend to interact with it.

How do I use MongoDB for my programming language?

As we just stated, you can always interact with MongoDB through the shell, but let’s say you are writing an application in Javascript, well then how would you interact with MongoDB through Javascript? You’d use a driver. A driver is a library of code that let’s you perform all the same actions you can do in the MongoDB shell but in the syntax of your programming language. So if you go to MongoDB’s website you’ll find that they have drivers for every major programming language.

Image from Gyazo

You can click on the link and download the driver for your programming language and it will provide you detailed instructions on how to install the driver, typically via a package manager like npm (NodeJS), pip (Python), or gem (Ruby).

Image from Gyazo

Once you have the driver installed you can start interacting with MongoDB by importing the driver and using the functions it provides

For a quick example here’s how you’d start using the driver for NodeJS:

1
2
npm init
npm install mongod --save

And here’s the code you’d actually use in Javascript to connect to MongoDB:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');

// Connection URL
const url = 'mongodb://localhost:27017';

// Database Name
const dbName = 'myproject';

// Create a new MongoClient
const client = new MongoClient(url);

// Use connect method to connect to the Server
client.connect(function(err) {
  assert.equal(null, err);
  console.log("Connected successfully to server");

  const db = client.db(dbName);

  client.close();
});

Why they teach the shell?

So this is why tutorials commonly teach how to interact with MongDB through the shell over through drivers because it’s easier for them to create one tutorial that applies to everyone than to create one for each driver. This is not to say that what they are teaching is not valuable because the syntax difference between shell and drivers is slim so you can still learn a lot from these tutorials but you will have to modify what you know once you jump into Javascript, Python, or whatever programming language you are using.

So should I learn the MongoDB shell commands or learn my language specific syntax?

We highly recommend starting to interacting with MongoDB through one of the drivers. You quickly get your feet wet and closer to the finish line quicker. The syntax from the shell to the driver functions does not change much and it shouldn’t be a hard adjustment if you have to ever have to switch back to the shell or another programming language.

When things go wrong though knowing the shell is invaluable. So if you have time to get acquainted with connecting and interacting with MongoDB through the shell, we recommend it.

Conclusion

There’s tons of learning materials out there that teach MongoDB from shell commands and while this is not the use case for most developers, it does still teach the basic concepts and syntax that can easily be translated to the specific language driver. If you are in the middle of a project though using Javascript, Python, or another programming language we recommend you jump straight into the documentation for the drivers and start interacting with through the drivers instead. Development is one of those areas where you need to getting your hands dirty early pays dividends. Let us know what you think and if you have any database needs don’t hesitate to reach out to us at Object Rocket.

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.