Introduction to MongoDB
There are many strong competitors in the NoSQL field today who deal with massive data problems in a variety of ways. One of the important solutions is MongoDB. MongoDB is a document-oriented weakly structured storage solution that uses JSON format to display, query and modify data.
MongoDB is well documented and scaling is as easy as installing. It provides conceptual support such as redundancy, slicing, indexing, and map/reduce. MongoDB’s open source community is very large and active. MongoDB is actually used in many large products, such as: Disney,
Craigslist, Foursquare, Github
and SourceForge. MongoDB is an open source project built and maintained by 10gen.com, a company founded by former DoubleClick executives. At the same time, 10gen also provides excellent business support and participation in construction.
MongoDB vs. NoSQL: Pitfalls and Advantages
MongoDB has many advantages as an available NoSQL solution. I just started to get in touch with nosql database and learned about a series of Java-based solutions, and spent a lot of time understanding what a column family is, the relationship between Hadoop and HBase, and what ZooKeeper is. When I finally understood everything, I found that Cassandra and HBase are indeed very reliable and trustworthy solutions for the NoSQL field. But compared to other solutions, MongoDB allows me to understand less concepts before I can start writing code.
Similar to other software, MongoDB has flaws. After using MongoDB for a while, I became a “Gotchas” by listing some of the things I have experienced and need to be aware of:
●
Don’t think in terms of relational databases. It’s obvious that MongoDB makes it very easy to build and execute complex queries. When it comes to practical use, you’ll be primarily concerned with efficiency issues (like me).
●
MongoDB indexes are binary trees. If you are not very familiar with B-tree, you may want to learn about it. These all involve building indexes that meet the needs of providing query conditions.
●
Design your index structure carefully. This involves the B-tree mentioned above. Initially my index contained many fields from the document in case I would need them. Don’t make the same mistake. I have an index on a very small collection (about 10 million records) that grew to over 17GB of space, larger than the collection itself. You wouldn’t want to index a list field that contains hundreds or thousands of entities.
●
MongoDB adopts a very interesting way to implement NoSQL: using BSON as storage, JSON as presentation, and Javascript for management and Map/Reduce. This also caused some minor problems such as this
(Destroying the equality operation of Number and Long), after MongoDB gradually becomes popular, it may continue to be displayed.
MongoDB, command line and driver
MongoDB basically uses Javascript client command line programs to manage complex tasks, such as data integration and simple information processing. Programming is entirely done using the Javascript language. In this article, we will show examples of command line usage. There are a large number of MongoDB client products available and are supported and driven by the MongoDB community. There are usually drivers for every programming language, and all the popular ones are included, as well as some of the less popular ones. This article demonstrates using the Java driver for MongoDB and comparing it with an ORM library (MJORM).
Introducing MJORM: MongoDB’s ORM solution
Among the many interesting problems being solved, the main problem trend among NoSQL data storage developers recently is object-relational mapping. Object-relational mapping is to map persistent data stored in traditional relational databases to objects used in applications. This makes the programming language more fluid and natural to use.
MongoDB’s document-oriented architecture makes it well-suited for object-relational mapping because documents themselves are stored as objects. Unfortunately, there are not many Java object-relational mapping libraries for MongoDB, but there are still some, such as morphia-(A
type-safe Java library for MongoDB),
spring-data (MongoDB implementation of SpringData project)
These ORM libraries make extensive use of annotations, which are not suitable for me for some reasons, the most important of which is the compatibility issue of these annotated objects in multiple projects. This got me started with mongo-Java-orm
or “MJORM” (pronounced
me-yorm) project, a Java object-relational mapping project for MongoDB. MJORM is under the MIT license and is released on google code
project. The project is built using Maven, and the Maven component warehouse is hosted on Google
code version control server. The latest available release version of MJORM is 0.15, which is already used by some projects in production environments.
Get started with ORM
Add to MJORM library
Maven users should first add the MJORM maven repository in pom.xml to make the MJORM components available.
mjorm-webdav-maven-repo
mjorm maven repository
http:
default
Then add dependencies:
</pnbsp;
book = dao.createObject(“books”, book);
System.out.println(book.getId());
First create a Book object and fill in the value, then call MongoDao’s createObject method and pass the Book object into “books”
in the collection. MJORM will convert Book to DBObject according to the previous xml mapping file
(This is the basic type used by the 10gen Java driver) and save a new document into “books”
gather. When MJORM returns the Book object, the id attribute will be populated. Please note that MongoDB does not require the creation of a database or collection before use by default. The system will automatically create it when needed, which may cause some confusion. Viewing the Book object on the MongoDB command line is roughly as follows:
> db.books.find({_id:ObjectId(“4f96309f762dd76ece5a9595”)}).pretty()
{
“_id”: ObjectId(“4f96309f762dd76ece5a9595”),
“isbn”: “1594743061”,
“title”: “MongoDB is fun”,
“description”: “…”
}
Let’s take a look at how to use the createObject method if we don’t use MJORM and use the 10gen Java driver directly:
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();
DBCollection col = db.getCollection(“books”);
col.insert(bookObj);
ObjectId id = ObjectId.class.cast(bookObj.get(“_id”));
System.out.println(id.toStringMongod());
The following is the object query:
Book book = dao.readObject(“books”, “4f96309f762dd76ece5a9595”, Book.class);
System.out.println(book.getTitle());
The readObject method reads a document from the specified collection based on the given document’s id, converts it to an object (again using a mapping file) and returns it.
Astute readers will notice that the Book has not yet been assigned an Author and is still saved. This is attributed to the structure-insensitive nature of MongoDB. We cannot require that the documents in the collection contain all attributes (the id attribute is required), and all Books without Author in MongoDB are OK. We now 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 Book now contains the Author and is persisted in MongoDB. Now view the Book on the command line:
> db.books.find({_id:ObjectId(“4f96309f762dd76ece5a9595”)}).pretty()
{
“_id”: ObjectId(“4f96309f762dd76ece5a9595”),
“isbn”: “1594743061”,
“title”: “MongoDB is fun”,
“description”: “…”
“author”: {
“firstName”: “Brian”,
“lastName”: “Dilley”
}
}
You can see that the author is already included in the persistent Book. Do it again 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”, .
.add(“lastName”, author.getLastName())
.pop()
.get();
DBCollection col = db.getCollection(“books”);
col.update(new BasicDBObject(“_id”, bookObj.get(“_id”)), bookObj);
For MongoDao
An in-depth discussion of methods is beyond the scope of this article. For users who are interested in using MJORM in actual projects, it is strongly recommended to learn about the relevant documents provided by the MJORM project, or MongoDao
Related usage provided by the interface.
Summary
I hope this article has shown the highlights of MongoDB and MJORM. MongDB is an excellent NoSQL data storage with a lot of excellent features and will be a long-term competitor in the NoSQL market. If you will use MongoDB in a Java project, I hope you can also consider using MJORM as your ORM framework. Everyone is welcome to submit feature requests, error and exception reports, documentation and source code corrections.
Author Bio
Brian Dilley is an experienced senior engineer and project leader in Java/Java EE/Spring
Over 13 years of experience in understanding and managing Framework/Linux internal structures. Brian has a lot of experience with startups, bringing to market, building/maintaining products, etc. He is an expert in Iaas, cloud, PHP and Linux. He is familiar with product procurement, installation and configuration definition, as well as the company’s software and hardware architecture including load balancing, database, Weibo, etc. can follow
Brian’s Twitter .
p;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”, .
.add(“lastName”, author.getLastName())
.pop()
.get();
DBCollection col = db.getCollection(“books”);
col.update(new BasicDBObject(“_id”, bookObj.get(“_id”)), bookObj);
For MongoDao
An in-depth discussion of methods is beyond the scope of this article. For users who are interested in using MJORM in actual projects, it is strongly recommended to learn about the relevant documents provided by the MJORM project, or MongoDao
Related usage provided by the interface.
Summary
I hope this article has shown the highlights of MongoDB and MJORM. MongDB is an excellent NoSQL data storage with a lot of excellent features and will be a long-term competitor in the NoSQL market. If you will use MongoDB in a Java project, I hope you can also consider using MJORM as your ORM framework. Everyone is welcome to submit feature requests, error and exception reports, documentation and source code corrections.
Author Bio
Brian Dilley is an experienced senior engineer and project leader in Java/Java EE/Spring
Over 13 years of experience in understanding and managing Framework/Linux internal structures. Brian has a lot of experience with startups, bringing to market, building/maintaining products, etc. He is an expert in Iaas, cloud, PHP and Linux. He is familiar with product procurement, installation and configuration definition, as well as the company’s software and hardware architecture including load balancing, database, Weibo, etc. can follow
Brian’s Twitter .