Proficient in NoSQL databases, but have never used MongoDB?

1. What is MongoDB?

MongoDB is a database management system designed for web applications and Internet infrastructure. Yes, MongoDB is a database, a NoSQL type database.

2. Why use MongoDB?

(1) MongoDB proposes the concepts of documents and collections, and uses BSON (JSON-like) as its data model structure. Its structure is object-oriented rather than a two-dimensional table. This is how a user is stored in MongoDB.

{
    username:'123',
    password:'123'
}

Using such a data model enables MongoDB to provide high read and write capabilities in a production environment, and its throughput is greatly enhanced compared to SQL databases such as mysql.

(2) Easy to scale and automatically failover. Easy scalability means that it provides sharding capabilities, which can shard data sets and distribute the data storage pressure to multiple servers. Automatic failover is the concept of a replica set. MongoDB can detect whether the master node is alive. When it becomes inactive, it can automatically promote the slave node to the master node to achieve failover.

(3) Because the data model is object-oriented, it can represent rich and hierarchical data structures. For example, in the blog system, “comments” can be directly added to the “article” document without having to create three documents like myqsl. table to describe this relationship.

3. Main features

(1) Document data type

SQL-type databases are normalized and can ensure the integrity and uniqueness of data through primary key or foreign key constraints. Therefore, SQL-type databases are often used in systems with high data integrity. MongoDB is inferior to SQL-type databases in this aspect, and MongoDB does not have a fixed Schema. Precisely because MongoDB lacks some such constraints, it can make the data storage data structure more flexible and the storage speed faster.

(2) Instant query capability

MongoDB retains the ability to query on-the-fly in relational databases and retains the ability to index (the bottom layer is based on B tree). This draws on the advantages of relational databases. Compared with the same type of NoSQL redis, it does not have the above capabilities.

(3) Copying ability

MongoDB itself provides a replica set to distribute data on multiple machines to achieve redundancy, with the purpose of providing automatic failover and expanded read capabilities.

(4) Speed and durability

MongoDB’s driver implements a write semantic fire and forget, that is, when the driver calls write, a successful result can be returned immediately (even an error is reported), which makes the write faster, but of course there is a certain degree of insecurity. Sex, completely dependent on the Internet. MongoDB provides the concept of Journaling log, which is actually like mysql’s bin-log log. When insertion is needed, records will be written to the log first, and then the actual data operation will be completed. In this way, if there is a power outage or the process is suddenly interrupted, It can ensure that the data will not be erroneous, and the Journaling log can be read through the repair function for repair.

(5) Data expansion

MongoDB uses sharding technology to expand data. MongoDB can automatically shard and transfer the data blocks in the shards, so that the data stored in each server is the same size.

4. C/S service model

The MongoDB core server is mainly started through the mongod program, and there is no need to configure the memory used by MongoDB at startup, because its design philosophy is that memory management is best left to the operating system. The lack of memory configuration is the design highlight of MongoDB. In addition, , you can also use the sharding function through the mongos routing server. The main client of MongoDB is an interactive js shell started by mongo. Using js shell, you can use js to communicate directly with MongoDB. You can use js syntax to query MongoDB data just like you use sql statements to query mysql data. In addition, various languages are also provided. The driver package facilitates access in various languages.

5. Complete command line tools

mongodump and mongorestore, standard tools for backing up and restoring databases. Output in BSON format and migrate the database.

mongoexport and mongoimport are used to import and export JSON, CSV and TSV data. They are useful when the data needs to support multiple formats. mongoimport can also be used for the initial import of large data sets, but before importing, please note that in order to make full use of mongoDB, you usually need to make some adjustments to the data model.

mongosniff, a network sniffing tool, is used to observe operations sent to the database. Basically, it converts BSON transmitted on the network into shell statements that are easy for people to read. Therefore, it can be concluded that MongoDB combines the best features of key-value storage and relational databases. Because of its simplicity, the data is extremely fast, and it is relatively easy to scale and also provides a database with complex query mechanisms. MongoDB needs to run on a 64-bit server, and it is best to deploy it separately. Because it is a database, it also needs to be hot and cold standby.

6. Several shell operations

Because this article is not an API manual, the use of the shell here is also a basic introduction to what functions and what statements can be used. It is mainly to show the convenience of using the MongoDB shell. If you need to know the specific MongoDB shell syntax, you can check the official documentation.

1. Switch database

use dba

Creating a database is not a necessary operation. Databases and collections will only be created when a document is inserted for the first time, which is consistent with the dynamic processing of data. Simplifies and speeds up the development process and facilitates dynamic allocation of namespaces. If you are worried about the database or collection being accidentally created, you can turn on strict mode

2. Insert syntax

db.users.insert({username:"smith"})
db.users.save({username:"smith"})

3. Find syntax

db.users.find()
db.users.count()

4. Update grammar

 db.users.update({username:"smith"},{$set:{country:"Canada"}})
 //Change the country of the user named smith to Canada
 
 db.users.update({username:"smith"},{$unset:{country:1}})
 //Remove the country field of the user named smith
 
 db.users.update({username:"jones"},{$set:{favorites:{movies:["casablance","rocky"]}}})
 //This mainly reflects multi-value modification, adding multiple values in the favorites field
 
 db.users.update({"favorites.movies":"casablance"},{$addToSet:{favorites.movies:"the maltese"}},false,true)
 //Multiple updates

5. Delete grammar

db.foo.remove() //Delete all data
db.foo.remove({favorties.cities:"cheyene"}) //Delete based on conditions
db.drop() //Delete the entire collection

6. Index related syntax

db.numbers.ensureIndex({num:1})
//Create an ascending index
db.numbers.getIndexes()
//Get all indexes

7. Basic management syntax

 show dbs
 //Query all databases
 show collections
 //Show all tables
 db.stats()
 //Display database status information
 db.numbers.stats()
 //Display collection table status information
 db.shutdownServer()
//Stop the database
 db.help()
//Get the database operation command
 db.foo.help()
//Get table operation commands
 The tab key //can automatically help us complete commands

The above commands are just simple examples. Suppose you have not learned any database syntax before and start learning SQL query syntax and MongoDB query syntax at the same time. Which one will you find easier? If you are using the java driver to operate MongoDB, you will find that any query is the same as the query method provided by Hibernate. As long as you build a query condition object, you can easily query (examples will be given next), blogger I was familiar with ES6 before, so it was no problem to start with MongoDB js shell. It is precisely because of such simplicity and perfect query mechanism that I fell in love with MongoDB deeply.

7. Using MongoDB in Java

1. Use maven to introduce the jar package. The latest driver package is quoted here, which provides a new access connection method.

<dependency>
  <groupId>org.mongodb</groupId>
  <artifactId>mongodb-driver-sync</artifactId>
  <version>3.8.0-beta3</version>
</dependency>

2. Create an access client

MongoClient client = MongoClients.create("mongodb://10.201.76.94:27017");

3. Get the number of collections

public long count() {
        MongoClient client = this.getClient();
        MongoCollection<Document> collections= client.getDatabase("mongodb_db_name").getCollection("mongodb_collection_name");
        return collections.count();
    }

4. Query collection

 public List<Document> find(Document params,Bson sort,int skip,int limit) {
         MongoClient client = this.getClient();
         MongoCollection<Document> collections= client.getDatabase("mongodb_db_name").getCollection("mongodb_collection_name");
         List<Document> list = new ArrayList<Document>(Integer.valueOf(config.getPro("sync_limit")));
     collections.find(params).sort(sort).skip(skip).limit(limit).forEach(new Block<Document>() {
             @Override
             public void apply(Document document) {
                 list.add(document);
             }
       });
        return list;
    }

Here are only examples of simple links and simple MongoDB operations, which shows the ease of operation. When using the driver, communication with MongoDB is based on TCP sockets. If there are too many query results and they just cannot be put into the first server, a getmore command will be sent to the server to obtain the next batch of query results. Insert data into the server time without waiting for the server’s response. The driver will assume that the write is successful and actually use the client to generate the object ID. However, this behavior can be configured through configuration and can be enabled through safe mode. The safe mode can be verified. Server-side inserted errors.