Your MongoDB instance for here or to go?

Published: 2013-07-15
In this post, I'll show you how to install MongoDB either on your local machine or in the cloud.

Let's look at the local installation first. This is as easy as it gets. Here's the Linux/MacOS version:

$ curl http://downloads.mongodb.org/osx/mongodb-osx-x86_64-2.4.5.tgz > mongodb.tgz
$ tar xvfz mongodb.tgz
$ ln -s mongodb-osx-x86_64-2.4.5 mongodb
$ sudo mkdir -p /data/db
$ sudo chown `id -u` /data/db


Done! Really.

Starting MongoDB requires a single command:

$ ./mongodb/bin/mongod

Now, let's open the MongoDB shell and type some commands from the MongoDB tutorial:

$ ./mongodb/bin/mongo
MongoDB shell version: 2.4.5
connecting to: test
> db
test
> use mydb 
switched to db mydb 
> j = {name:"name"} { "name" : "name" }
> k = {x:3} { "x" : 3 }
> db.testData.insert(j) 
> db.testData.insert(k)
> show collections 
system.indexes 
testData
> db.testData.find()
{ "_id" : ObjectId("51e3c14987d8afffb5a6f97c"), "name" : "name" } 
{ "_id" : ObjectId("51e3c15487d8afffb5a6f97d"), "x" : 3 }
> exit 

Now, let's do the same thing in the cloud with MongoLab. You can get a single instance for free, as long as it doesn't exceed 500 MB of data. Pretty cool. Another option is MongoHQ who offer a similar service. I'm not affiliated with any, so feel free to check out both :)

  1. Sign up at www.mongolab.com
  2. Create a database ('mongolab-test' in my case)
  3. Pick your hosting location: AWS, Google, etc. If you're simply testing, this doesn't really matter, but when you'll be deploying for real, you'll want to make sure your DB is the same cloud as your app, I suppose ;)
  4. Select the 'Sandbox' plan
  5. Create a first user (or you won't be able to connect to your DB, duh)
Done. You should now see something similar to this:

Technical illustration






 Click on the database name to view the connection information:

Technical illustration




















Add a collection, 'collection1' in my case. Obviously, it will be empty. 

Let's use the MongoDB shell again and add some data:

$ ./mongo ds051067.mongolab.com:51067/mongolab-test -u DBUSER -p DBPASSWORD
MongoDB shell version: 2.4.5
connecting to: ds051067.mongolab.com:51067/mongolab-test
> use mongolab-test
switched to db mongolab-test
> show collections
collection1
system.indexes
system.users
> for (var i = 1; i <= 25; i++) db.collection1.insert( { x : i } )
> db.collection1.find()
{ "_id" : ObjectId("51e3ce08915082db3df32bf0"), "x" : 1 }
{ "_id" : ObjectId("51e3ce08915082db3df32bf1"), "x" : 2 }
{ "_id" : ObjectId("51e3ce08915082db3df32bf2"), "x" : 3 }
[Output removed for brevity]
> db.collection1.find({x:18})
{ "_id" : ObjectId("51e3ce08915082db3df32c01"), "x" : 18 }

All right, we wrote some stuff. Let's create an index:

> db.collection1.ensureIndex({"x":1})
> db.collection1.getIndexes()

[

{

"v" : 1,

"key" : {

"_id" : 1

},
"ns" : "mongolab-test.collection1",
"name" : "_id_"
},
{
"v" : 1,
"key" : {
"x" : 1
},
"ns" : "mongolab-test.collection1",
"name" : "x_1"
}
]
> db.collection1.find({x:18}).explain()
{
"cursor" : "BtreeCursor x_1",
"isMultiKey" : false,
"n" : 1,
"nscannedObjects" : 1,
"nscanned" : 1,
"nscannedObjectsAllPlans" : 1,
"nscannedAllPlans" : 1,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 0,
"nChunkSkips" : 0,
"millis" : 0,
"indexBounds" : {
"x" : [
[
18,
18
]
]
},
"server" : "h000432.mongolab.com:51067"
}

The index works as intended, that's a relief :-P

Going back to the MongoLab console, you should see the right number of documents:

Technical illustration










That's it for today. I've just scratched the surface but it should get you started. See how easy this was? Happy learning!


About the Author

Julien Simon is the Chief Evangelist at Arcee AI , specializing in Small Language Models and enterprise AI solutions. Recognized as the #1 AI Evangelist globally by AI Magazine in 2021, he brings over 30 years of technology leadership experience to his role.

With 650+ speaking engagements worldwide and 350+ technical blog posts, Julien is a leading voice in practical AI implementation, cost-effective AI solutions, and the democratization of artificial intelligence. His expertise spans open-source AI, Small Language Models, enterprise AI strategy, and edge computing optimization.

Previously serving as Principal Evangelist at Amazon Web Services and Chief Evangelist at Hugging Face, Julien has helped thousands of organizations implement AI solutions that deliver real business value. He is the author of "Learn Amazon SageMaker," the first book ever published on AWS's flagship machine learning service.

Julien's mission is to make AI accessible, understandable, and controllable for enterprises through transparent, open-weights models that organizations can deploy, customize, and trust.