1024programmer Mongodb MongoDB, Java, and ORMs

MongoDB, Java, and ORMs

Introduction to MongoDB

There are many competing NoSQL products that are used in different ways, but all of them can solve big data problems very 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 is well documented, easy to install and set up, and easy to scale. It supports the well-known concepts of replication, sharding, indexing, and Map/Reduce. The MongoDB open source community is large and active. MongoDB is proud that large, high-traffic production environments including Disney, Craigslist, Foursquare, Github, and SourceForge have deployed MongoDB. 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, the advantage of MongoDB is that it is easy to use. During my first deep dive into NoSQL databases, after trying a lot of Java-based solutions, I found that figuring out what a column family is
family), what is the relationship between Hadoop and HBase, and what exactly is ZooKeeper is very time-consuming. When I finally figured out these problems, I realized that Cassandra, HBase and other products are very complete NoSQL solutions. Compared with other solutions, MongoDB is easier to grasp, and you don’t need to understand too many concepts before you start writing code.

Obviously, MongoDB, like any software, has flaws. In the process of learning and using MongoDB, I have 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 try to use it for real-time queries, you may find yourself overdoing it and running into performance issues. (I’ve made this mistake before)

MongoDB’s index is Binary
Tree. If you’re not very familiar with B-Tree, you should look into it. The order of the query conditions must match the order in which the indexes are created.

Carefully design indexes. This is related to the B-Tree mentioned earlier. The first few indexes I created included many fields in the document, because I always thought that they might be queried later, and you should 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 is likely to contain hundreds or thousands of entries, you probably won’t index it.

MongoDB’s approach to supporting NoSQL is very interesting. It uses BSON for storage, JSON representation, and Javascript for management and Map/Reduce. In this way, when MongoDB has been developed for a long enough time, as long as more popular big data solutions, MongoDB will inevitably have some strange little problems, such as using the equal operator on NumberLong to fail.

MongoDB, Console, Driver

MongoDB management can usually be performed on a Javascript client console application, which can simplify complex tasks such as data migration and operation; you can also use Javascript language programming to implement MongoDB management. In this article, we will demonstrate the use of the console. There are so many MongoDB client products out there, and they all have the quality to be put into a production environment. 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 less popular programming languages. This article will show how to use MongoDB’s Java driver, and compare it with the way of using the ORM library (MJORM).

Introduction to MJORM: ORM solution for MongoDB

There are still many interesting problems to be solved for NoSQL data storage, and the one that has attracted application programmers more 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 the application is written.

MongoDB’s document-oriented architecture makes it easy to ORM, because the documents it stores are themselves objects. Unfortunately, Java for MongoDB
There are not many ORM libraries, currently only morphia (Java library for MongoDB, type-safe) and spring-data (Spring
MongoDB implementation of the Data Synthesis 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 on the Subversion server. At the time of writing this article, the latest stable release version of MJORM is 0.15, and individual projects have been used in the production environment.

Getting Started with MJORM Add the MJORM library to the project

Maven User Preferencesmjorm.xml”));

We created the XmlDescriptorObjectMapper object and added the mapping file. Next we will create an instance of the MongoDao object 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 DB object and 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 the createObject method of MongoDao after assignment. The two parameters are the collection name “books” and the Book object. MJORM will then convert the Book to a DBObject (the base object type used by the 10gen Java driver) using the previously created XML mapping file, and persist the new document into the “books” collection. MJORM then returns an instance of the Book object with the generated id attribute. It is important to note that MongoDB does not require databases or collections to be created by default; MongoDB creates them when needed, which can sometimes cause confusion. The new Book seen from the MongoDB console is 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 the process of createObject if you don’t use MJORM, but 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();
// ‘db’ is the DB object we created earlier
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 the file from the specified collection with the specified id, then converts the file to the corresponding class (the previous mapping file will be used again) and returns.

If you are keen, you may have noticed that our Book is not yet an Author, but the Book is still persisted. This is exactly the Schema-free feature of MongoDB. In addition to the id, we cannot require the documents in the collection to contain any attributes, so there is no problem in creating a Book without an Author in MongoDB. Let’s add an Author to Book and update:

Author author = new Author();
author.setFirstName(“Brian”);
author.setLastName(“Dilley”);
book.setAuthor(author);
dao.updateObject(“books”, “4f96309f762dd76ece5a9595”, book);

The current Book contains Author and is 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 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 the methods of MongoDao in depth. If you want to use MJORM in your project, I recommend you to 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 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 the methods of MongoDao in depth. If you want to use MJORM in your project, I recommend you to look at the documentation of the MJORM project, or the MongoDao interface provided by the MJORM project.

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/mongodb-java-and-orms/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索