Introduction to MongoDB
There are many competing NoSQL products. They use different methods, but they all solve big data problems well. MongoDB is one of the very good products. MongoDB is a document-oriented, Schema-free storage solution that uses JSON-style documents to display, query, and modify data.
MongoDB has extensive documentation, is simple to install and set up, and is easy to extend. It supports well-known concepts such as replication, sharding, indexing, and Map/Reduce. The MongoDB open source community is large and active. MongoDB is proud to have been deployed in large, high-traffic production environments including Disney, Craigslist, Foursquare, Github, and SourceForge. MongoDB is an open source project created and maintained by 10gen.com, a company founded by former DoubleClick executives. In addition to actively participating in community support, 10gen also provides commercial support.
Advantages and Disadvantages of MongoDB and NoSQL
As a NoSQL solution, MongoDB has the advantage of being easy to get started with. When I first delved into NoSQL databases, and tried many Java-based solutions, I found that it was difficult to figure out what a column family was.
family), what is the relationship between Hadoop and HBase, and what is ZooKeeper? It is very time-consuming. When I finally figured out these issues, I realized that Cassandra, HBase and other products are very complete NoSQL solutions. Compared with other solutions, MongoDB is easier to master, and you don’t need to understand too many concepts before starting to write code.
Obviously, MongoDB, like any software, has flaws. In the process of learning and using MongoDB, I encountered several things that can be regarded as “traps”:
Don’t use it as an RDBMS. This may seem obvious, but it’s so easy to create and execute complex queries in MongoDB that by the time you want to use it for real-time queries, you may find that you’ve overdone it and may run into performance issues. (I’ve made this mistake before)
MongoDB’s index is Binary
Tree. If you’re not familiar with B-Tree, you should look into it. The order of query conditions must match the order in which indexes are created.
Design your index carefully. This is related to the B-Tree mentioned earlier. The first few indexes I created all contained many fields in the document, because I always thought that I might query them later. You should be able to understand this idea. Don’t make this mistake. I once created an index on a very small collection (approximately 10 million records), and the index grew to 17GB, larger than the collection itself. If an array field might contain hundreds or thousands of entries, you might not index it.
MongoDB’s approach to supporting NoSQL is very interesting. It uses BSON storage, JSON representation, and Javascript for management and Map/Reduce. In this way, when MongoDB develops long enough and is as long as more popular big data solutions, MongoDB will inevitably have some strange little problems, such as using the equal operator on NumberLong and it will fail.
MongoDB, console, driver
MongoDB management can usually be performed on a Javascript client console application. The console application can simplify complex tasks such as data migration and operations; you can also use Javascript language programming to implement MongoDB management. In this article, we will demonstrate the use of the console. There are now many MongoDB client products, all of which have the quality to be put into production environments. The MongoDB community also calls them drivers. Generally speaking, each programming language has its own driver, and these drivers can cover all popular programming languages, as well as some not-so-popular programming languages. This article will show how to use the MongoDB Java driver and compare it with using the ORM library (MJORM).
Introduction to MJORM: MongoDB’s ORM solution
There are still many interesting problems that need to be solved in NoSQL data storage. The one that application programmers are more concerned about recently is object-relational mapping (ORM). ORM refers to the mapping between persistent data and objects used by applications. Persistent data used to be stored in relational databases. ORM can make the process of processing data smoother and closer to the language in which applications are written.
MongoDB’s document-oriented architecture makes it easy to ORM because the documents it stores are themselves objects. Unfortunately, Java available for MongoDB
There are not many ORM libraries yet. Currently there are only morphia (a Java library for MongoDB, which is type-safe) and spring-data (Spring
MongoDB implementation of the Data Comprehensive Project).
These ORM libraries use a lot of annotations. I am not inclined to use annotations for many reasons, the most important of which is the portability of annotated objects between multiple projects. So I created the mongo-Java-orm project (MJORM, pronounced me-yorm) which is Java for MongoDB
ORM. MJORM uses the MIT license and is placed on Google Code. The project is built with Maven, and Maven’s artifact library is currently hosted on Google
Code’s Subversion server. At the time of writing this article, the latest stable release version of MJORM is 0.15, and some projects have already been used in production environments.
Getting started with MJORM Add the MJORM library to the project
Maven user firstmjorm.xml”));
We created the XmlDescriptorObjectMapper object and added the mapping file. Next we will create a MongoDao object instance provided by MJORM:
DB db = mongo.getDB(“mjormIsFun”); // 10gen driver
MongoDao dao = new MongoDaoImpl(db, objectMapper);
We first obtained a DB object instance in the 10gen driver. Then create MongoDao with the DB object and the previously created ObjectMapper. Now that we are ready to persist the data, let’s create a Book object and save it to MongoDB.
Book book = new Book();
book.setIsbn(“1594743061”);
book.setTitle(“MongoDB is fun”);
book.setDescription(“…”);
book = dao.createObject(“books”, book);
System.out.println(book.getId()); // 4f96309f762dd76ece5a9595
We first created the Book object, and then called MongoDao’s createObject method after assigning the value. The two parameters are the collection name “books” and the Book object. MJORM will then use the previously created XML mapping file to convert the Book into a DBObject (the basic object type used by the 10gen Java driver) and persist the new document into the “books” collection. MJORM will then return an instance of the Book object with the generated id attribute. It is important to note that MongoDB does not require that a database or collection be created by default before it can be used; MongoDB creates them when needed, which can sometimes cause confusion. The new Book seen from the MongoDB console looks like this:
> db.books.find({_id:ObjectId(“4f96309f762dd76ece5a9595”)}).pretty()
{
“_id”: ObjectId(“4f96309f762dd76ece5a9595”),
“isbn”: “1594743061”,
“title”: “MongoDB is fun”,
“description”: “…”
}
Let’s take a look at what the createObject process looks like if you don’t use MJORM, but directly use the 10gen Java driver:
Book book = new Book();
book.setIsbn(“1594743061”);
book.setTitle(“MongoDB is fun”);
book.setDescription(“…”);
DBObject bookObj = BasicDBObjectBuilder.start()
.add(“isbn”, book.getIsbn())
.add(“title”, book.getTitle())
.add(“description”, book.getDescription())
.get();
// ‘db’ is the DB object we created previously
DBCollection col = db.getCollection(“books”);
col.insert(bookObj);
ObjectId id = ObjectId.class.cast(bookObj.get(“_id”));
System.out.println(id.toStringMongod()); // 4f96309f762dd76ece5a9595
Now let’s query the object:
Book book = dao.readObject(“books”, “4f96309f762dd76ece5a9595”, Book.class);
System.out.println(book.getTitle()); // “MongoDB is fun”
The readObject method reads a file from a specific collection with the specified id, then converts the file into the corresponding class (reusing the previous mapping file) and returns.
If you are keen, you may have noticed that our Book does not have an Author yet, but the Book is still persisted. This is the Schema-less feature of MongoDB. We cannot require the documents in the collection to contain any attributes other than id, so there is no problem creating a Book without Author in MongoDB. Let’s add an Author to the Book and update it:
Author author = new Author();
author.setFirstName(“Brian”);
author.setLastName(“Dilley”);
book.setAuthor(author);
dao.updateObject(“books”, “4f96309f762dd76ece5a9595”, book);
The current Book includes Author and is also persisted to MongoDB. Let’s take a look at the new Book from the MongoDB console:
> db.books.find({_id:ObjectId(“4f96309f762dd76ece5a9595”)}).pretty()
{
“_id”: ObjectId(“4f96309f762dd76ece5a9595”),
“isbn”: “1594743061”,
“title”: “MongoDB is fun”,
“description”: “…”
“author”: {
“firstName”: “Brian”,
“lastName”: “Dilley”
}
}
As you can see, the persistent Book now contains an author. Then let’s look at the situation without using MJORM:
Author author = new Author();
author.setFirstName(“Brian”);
author.setLastName(“Dilley”);
book.setAuthor(author);
DBObject bookObj = BasicDBObjectBuilder.start()
.add(“isbn”, book.getIsbn())
.add(“title”, book.getTitle())
.add(“description”, book.getDescription())
.push(“author”)
.add(“firstName”, author.getFirstName())
.add(“lastName”, author.getLastName())
.pop()
.get();
DBCollection col = db.getCollection(“books”);
col.update(new BasicDBObject(“_id”, bookObj.get(“_id”)), bookObj);
In this article, we will not introduce all methods of MongoDao in depth. If you want to use MJORM in your project, it is recommended that you take a look at the documentation of the MJORM project, or the MongoDao interface provided by the MJORM project.
; }
}
As you can see, the persistent Book now contains an author. Then let’s look at the situation without using MJORM:
Author author = new Author();
author.setFirstName(“Brian”);
author.setLastName(“Dilley”);
book.setAuthor(author);
DBObject bookObj = BasicDBObjectBuilder.start()
.add(“isbn”, book.getIsbn())
.add(“title”, book.getTitle())
.add(“description”, book.getDescription())
.push(“author”)
.add(“firstName”, author.getFirstName())
.add(“lastName”, author.getLastName())
.pop()
.get();
DBCollection col = db.getCollection(“books”);
col.update(new BasicDBObject(“_id”, bookObj.get(“_id”)), bookObj);
In this article, we will not introduce all methods of MongoDao in depth. If you want to use MJORM in your project, it is recommended that you take a look at the documentation of the MJORM project, or the MongoDao interface provided by the MJORM project.