Skip to content

Instantly share code, notes, and snippets.

@edcote
Last active June 21, 2018 15:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edcote/6b50e43c91c5f4f3558859f73e68e042 to your computer and use it in GitHub Desktop.
Save edcote/6b50e43c91c5f4f3558859f73e68e042 to your computer and use it in GitHub Desktop.
MongoDB

MongoDB

MongoDB stores data in flexible, JSON-like documents. The document model maps to object in your application code, making data easy to work with. MongoDB is a distributed database at its core.

Here. Be careful, need 3.4 or later.are notes on how to install MongoDB on RHEL7.

Spark and MongoDB

MongoDB is a document store (and essentially a database). Spark is a computing engine and not a store. When used together, Spark jobs can be executed directly on operational data sitting in MongoDB.

Installing MongoDB

On Ubuntu 17.10: sudo apt install monogodb

Make sure to start mongod service. Nothing will work otherwise: sudo service mongodb start

To change location of data store:

sudo vi /etc/mongodo.conf
# modify the following line --> dbpath=/new/db/path
# update permissions
sudo chown mongodb:sudo /new/db/path
sudo chmod 770 /new/db/path 
sudo service mongodb restart

Troubleshooting information: https://stackoverflow.com/questions/45691032/mongod-service-start-exits-with-code-100

Check ownership of /path/to/db should be owned by user mongodb

Firewall issues

Check that port 27017 is allowed. If not: (ubuntu) ufw allow from 127.0.0.1 to 127.0.0.1 port 80 proto tcp.

Access MongoDB from command line

Link to official documentation

# connect to MongoDB instance running on port 27017
mongo
# display all databases
show dbs
# to switch databases, issue the use command
use <database>
show collections # it will show "dispatch"
# find operation
db.dispatch.find().pretty()
# drop, everything!
db.dispatch.drop()

Setup

Add this class in the package in which you will access MongoDB. Easy!

** Casbah has been deprecated. Use https://docs.mongodb.com/ecosystem/drivers/scala/ **

import com.mongodb.casbah.MongoClient

object MongoFactory {
  private val SERVER = "localhost"
  private val PORT = 27017
  private val DATABASE = "plus"
  private val COLLECTION = "dispatch"
  val connection = MongoClient(SERVER)
  val collection = connection(DATABASE)(COLLECTION)
}

Basic operations

  • Insert new document
val builder: mutable.Builder[(String, Any), DBObject] = MongoDBObject.newBuilder
builder += "hello" -> "world"
builder += "foo" -> "bar"
val o = builder.result()
MongoFactory.collection.insert(o)
  • Iterate over all documents, optionally remove
for (i <- MongoFactory.collection.find()) {
  println(i) // use
  //MongoFactory.collection.remove(i) // remove
}

Database tools

MongoBooster is a lightweight GUI for visualizing your data store. The application is distributed using AppImage format (which is v. cool)

To run: ./mongobooster-4.1.3-x86_64.AppImage

MongoDB Compass is helpful for database visualization and manipulation.

Here are some query examples:

{ uuid: 'ut001' }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment