Simple use of MongoDB

Source: Internet
Author: User

Mongo is a high-performance, open-source, and non-pattern document-based database. It can be used in many scenarios to replace traditional relational databases or key/value storage methods. Mongo is developed using C ++ and provides the following functions:

◆ Set-oriented storage: Suitable for storing objects and JSON data.

◆ Dynamic query: Mongo supports a wide range of Query expressions. Query commands use JSON tags to easily query embedded objects and arrays in a document.

◆ Complete indexing support: Includes embedded object and array of documents. The Query Optimizer of Mongo analyzes the query expression and generates an efficient query plan.

◆ Query monitoring: Mongo provides a monitoring tool to analyze the performance of database operations.

◆ Replication and automatic failover: The Mongo Database supports data replication between servers and master-slave mode and mutual replication between servers. The primary objective of replication is to provide redundancy and automatic failover.

◆ Efficient traditional storage: supports binary data and large objects (such as photos or images ).

◆ Automatic sharding to support cloud-level Scalability (in the early alpha stage): the automatic sharding function supports horizontal database clusters and allows you to dynamically add additional machines.

MongoDB's main goal is to build a bridge between key/value storage (providing high performance and high scalability) and traditional RDBMS systems (rich functions, combines the advantages of both. Mongo is applicable to the following scenarios:

◆ Website data: Mongo is ideal for real-time insertion, update, and query, as well as the replication and high scalability required for real-time website data storage.

◆ Cache: because of its high performance, Mongo is also suitable for serving as the cache layer of information infrastructure. After the system is restarted, the persistent cache layer established by Mongo can avoid data source overload at the lower layer.

◆ Large-sized and low-value data: traditional relational databases may be expensive to store some data. Previously, programmers often choose traditional files for storage.

◆ High scalability: Mongo is ideal for databases consisting of dozens or hundreds of servers. The Mongo roadmap contains built-in support for the MapReduce engine.

◆ Used for storage of objects and JSON data: The BSON data format of Mongo is very suitable for storing and querying document-based data.

Naturally, there are some restrictions on the use of MongoDB, for example, it is not suitable:

◆ Highly transactional systems: such as banking or accounting systems. Traditional relational databases are still more suitable for applications that require a large number of atomic complex transactions.

◆ Traditional business intelligence applications: BI databases for specific problems will produce highly optimized query methods. For such applications, data warehouse may be a more appropriate choice.

◆ SQL Problems

Java:

1. Create SimpleTest. java to complete simple mongoDB database operations

Mongo mongo = new Mongo ();

In this way, a MongoDB database connection object is created, which is connected to the localhost address of the current machine by default, and the port is 27017.

DB db = mongo. getDB ("test ");

In this way, a database named test is obtained. If this database is not created in mongoDB, it can run normally. MongoDB can add data without creating this database. When this database is not added, mongoDB automatically creates the current database.

After obtaining the db, we will get a "clustered collection DBCollection" and use the getCollection method of the db object.

DBCollection users = db. getCollection ("users ");

In this way, a DBCollection is obtained, which is equivalent to the "table" of our database ".

Query all data

DBCursor cur = users. find ();

While (cur. hasNext ()){

System. out. println (cur. next ());

}

This time, because the system generates a large number of documents, images, and other file information, we used Oracle and mongoDB databases. I found that the combination of this type of relational database and non-relational database is really very efficient. You only need to store a key value in a relational database and access documents and data in a non-relational database. this is convenient, fast, and efficient.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.