Java Reference
In-Depth Information
...
}
In addition to the data shown, the JSON object also has links to the complete cast list, re-
views, and more. Another reason to use a database like MongoDB for this data is that not
every field appears in each movie. For example, some movies contain a critic's score and
some do not. This fits with the whole idea of a schema-less database based on JSON.
First, to populate the MongoDB I'll use an instance of the com.gmongo.GMongo
class. This class wraps the Java API directly. In fact, if you look at the class in
GMongo.groovy , you'll see that it consists of
class GMongo {
@Delegate
Mongo mongo
// ... Constructors and other methods ...
}
There follow various constructors and simple patch methods. The @Delegate annotation
from Groovy is an Abstract Syntax Tree (AST) transformation [ 18 ] that exposes the methods
in the com.mongodb.Mongo class, which comes from the Java API, through GMongo.
The AST transformation means you don't need to write all the delegate methods by hand.
18 Discussed in chapter 4 on integration and in appendix B , “Groovy by feature,” and used in many other places in
this topic.
Initializing a database is as simple as
GMongo mongo = new GMongo()
def db = mongo.getDB('movies')
db.vampireMovies.drop()
MongoDB uses movies as the name of the database, and collections inside it, like vam-
pireMovies , are properties of the database. The drop method clears the collection.
Searching Rotten Tomatoes consists of building a GET request with the proper parameters.
In this case, the following code searches for vampire movies:
 
Search WWH ::




Custom Search