Using Groovy to access MongoDB

MongoDB (http://www.mongodb.org) is a document-oriented database written in C++ with RDBMS-like features such as indexing and replication. It is developed and supported by 10gen.

MongoDB is very popular mainly for its simplicity: documents are created as JSON-like records (key/value pairs with a rich data type model) and the interface is simple enough to be used directly from JavaScript. It has been designed for scalability in mind. Its document-oriented data model allows it to automatically split up data across multiple servers, letting developers focus on application logic instead of scaling up the data store.

Other very useful features of MongoDB are built-in support for Map/Reduce-style aggregation and geospatial indexes.

This recipe will show you how to execute CRUD-like operations on a MongoDB instance and how to search for data.

Getting ready

MongoDB installation is quite simple, especially if you are on Linux or OS X, where you can use a package manager such as Yum or Homebrew. Detailed installation instructions are available for every operating system on the MongoDB website (http://docs.mongodb.org/manual/installation).

Before we get into the details of this recipe, we will give a quick crash course on the main MongoDB's entities.

MongoDB has databases, collections, and documents. MongoDB stores a database in its own file. A database has specific permissions and can be conceptually mapped to a relational database.

A collection is a group of documents stored in a database. A collection has no schema, meaning that each document in a collection is free to have any kind of structure (it is recommended to keep similar documents in the same collection).

Finally, the document is an ordered set of key/value pairs. Each programming language has a data structure to represent a key/pair values, such as maps or dictionaries.

This is a Javascript representation of a MongoDB document.

{ "name": "Oscar", "lastName": "Wilde" }

The Java driver (https://github.com/mongodb/mongo-java-driver/downloads) is the oldest MongoDB driver. It is very stable and a popular choice for enterprise developers. The driver is enough to get started, but as it often happens with Java libraries, it is very verbose. The verbosity is quite evident when building documents to be sent to the server.

Fortunately, there is a Groovy wrapper for the MongoDB Java driver, named GMongo, developed and actively maintained by Paulo Poiati (http://blog.paulopoiati.com/). The wrapper is maintained at the same pace as the main Java driver, making it an excellent option for Groovy developers.

How to do it...

Ensure that your MongoDB instance is running; the server usually listens for connections on port 27017.

  1. As usual, it is necessary to import the third-party library that will be used throughout this recipe, GMongo.
    @Grab('com.gmongo:gmongo:1.2')
    import com.gmongo.GMongo
  2. Then, the GMongo class instance is created to access the database groovy-book. The getDB method does actually create a database, if that does not exist.
    def mongo = new GMongo()
    def db = mongo.getDB('groovy-book')
  3. Now, let's insert some documents into a collection named todos.
    db.todos.insert([name: 'buy milk'])
    db.todos.insert(name: 'pay rent')
    db.todos << [name: 'read document']

    The previous snippet inserts three to-do documents into MongoDB. You can notice that the syntax for inserting data is slightly different in each statement. In the first two cases we are actually calling the same method; Groovy allows omitting square brackets ([]) when the method's only parameter is a Map. The last statement is making use of the leftShift method, which under the hood results in the same call to the insert method.

  4. So, you can now select a document from the collection using for example the findOne method:
    println db.todos.findOne()

    The output will be as follows:

    > [_id:50b299e81a88da0e521db954, name:buy milk]
    

How it works...

The findOne method with no arguments returns the first document in the collection. MongoDB automatically adds an id, named ObjectID, to a new document. The ObjectID is a 12-byte value consisting of a 4-byte timestamp (seconds since epoch), a 3-byte machine id, a 2-byte process id, and a 3-byte counter.

findOne can be also used with an argument, very much like an SQL SELECT statement.

println db.todos.findOne(name: 'pay rent')

The output will be as follows:

> [_id:50b29b091a88d361473e6b46, name:pay rent]

The find method allows searching for documents using regular expressions. Groovy supports regular expressions natively with the help of the ~ operator, which creates a compiled Pattern object from the given string:

db.todos.find([name: ~/^pay/]).each {
    println it
}

The output will be as follows:

> [_id:50b29b091a88d361473e6b46, name:pay rent]

Updating documents is also as easy as inserting them. A MongoDB collection is schemaless, so each document can have its own shape.

db.todos.update(
  name: 'pay rent',
  [$set: [name: 'pay December rent']]
)
assert db.todos.count(
  name: 'pay December rent') == 1

The update method changes the value of the name field using the $set operator. This operator appends the new field or fields to the document or replaces the existing value of the field(s) in case they are already present in the document.

db.todos.update(name: 'pay December rent', [$set:[priority:1]])

The second update example adds a new attribute to the document, the priority field. Please note that the value of priority is an Integer and not a String. MongoDB supports several data types internally through the BSON format, which includes dates and binary.

Another way to update a document is the following:

def todo = db.todos.findOne(name: 'buy milk')
todo.priority = 2
db.todos.save todo
println todo

The output will be as follows:

> [_id:50b2a3aa1a8848c02c604025, name:buy milk, priority:2]

The syntax to remove documents is very similar to the one for updating.

db.todos.insert([name: 'call John', priority: 2])
assert db.todos.count(priority: 2) == 2

db.todos.remove(priority: 2, name: 'call John')
assert db.todos.count(priority: 2) == 1

There's more...

Similar to the Using Groovy to access Redis recipe, we are going to show a simple way to store Groovy objects (POGO) to MongoDB. Let's define a Todo class and instantiate it:

class Todo {
    def name
    def priority
    def context
}

def td = new Todo(name: 'open account',
    priority: 1,
    context: 'finance')

Now, the getProperties method in Groovy returns a Map of the object's properties, including some properties we don't want to store in MongoDB, such as class or metaClass.

db.todos << td.properties.findAll {
  !['class', 'metaClass'].contains(it.key)
}
println db.todos.findOne(name:'open account')

The output will be as follows:

> [_id:50b2a8b21a8820042e85bc6b, context:finance,
   priority:1, name:open account]

In the previous snippets, the properties method is invoked on the Todo instance and some properties are filtered out. The resulting Map is simply added to the todos collection.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.225.255.178