Chapter 1 Add, delete, change and check MongoDB database

1-2 Basic operations of the Mongodb database

Chapter 3 Document operation 1

use Testdb3   // Enter the Testdb3 database
document = {
    _id: 1.name: "Zhang Xiaohua".sex: "Male".phone: "12356986594".hobbies: ["Play basketball"."Play football"."Sing"]}; db.stu1.insert(document);
db.stu2.insert(document);
db.stu3.insert(document);
db.stu2.update({ phone: "12356986594" }, { $set: { phone: "18356971462"}}); db.stu2.find().pretty();/ / check stu2
db.stu3.save({
_id: 1.name: "Zhang Xiaoxiao".sex: "Female".phone: "12365498704".hobbies: ["Dance"."Badminton"."Sing"]});Copy the code

Chapter four document operation two

#########begin#########
echo 'the document = [{" _id ": 1," name ":" cc ", "sex" : "female", "age" : 23, "national" : "han"}, {" _id ": 2," name ": "East", "sex", "male", "age" : 20, "national" : "miao"}, {3, "_id" : "name" : "north north", "sex", "male", "age" : 19, "national" : "Han"}, {" _id ": 4," name ":" south-south ", "sex", "female", "age" : 15, "national" : "dai"}; db.stu1.insert(document); db.stu2.insert(document); Db.stu1. find({age:{$gte: 15},sex: "female"}); Db.stu1.find ({national:" miao "}).pretty(); Db.stu1. find({age:{$lt: 20}, sex: "male"}); db.stu2.remove({}) '
#########end#########
Copy the code

Chapter 2 Permission setting of MongoDB database

2-1 MongoDB Database security

The first step is to create an administrator user

use admin
db.createUser({user:"admin".pwd:"123456",roles:[{role:"root",db:"admin"}]})
Copy the code

The second step is to create common users as required

use firstdb 
db.createUser({user:"people".pwd:"people",roles:[{role:"read",db:"firstdb"}]})
Copy the code

The third level restricts access to the database

use admin Enter the admin database
db.shutdownServer() # service shutdown
exit # exit databaseMongod -port 20018 --dbpath /data/db --logpath/TMP /mongodb.log --bind_ip 127.0.0.1 --forkCopy the code

Chapter 3 Advanced query of MongoDB database

3-1 Aggregation function query statistics of MongoDB

The first step is to aggregate the pipe operator to output the document in custom format (1)

Insert data first
use test1 
document = [
        {
                "_id" : 1,
                "course" : "Python expression problem solving training"."author" : "Li Tun"."tags" : [
                        "Python basis"."Solving"]."learning_num": 1882}, {"_id": 2."course" : "Basic Syntax for the Java language"."author" : "Yu yue"."tags" : [
                        "Java foundation"."Grammar"]."learning_num": 814}, {"_id": 3."course" : "Python Object-oriented Programming Training"."author" : "Li Tun"."tags" : [
                        "Python basis".Object oriented]."learning_num": 143}, {"_id": 4."course" : "Internet of Things Mobile Application Development for Android Comprehensive Training (1)"."author" : "prophet5"."tags" : [
                        "Android"."Internet of Things"."Mobile development"]."learning_num" : 207
        }
]
db.educoder.insert(document)

#********* Begin *********#
echo "
db.educoder.aggregate({\$project:{_id:0,course:1,learning_num:1}});
db.educoder.aggregate({\$match:{learning_num:1882}});
"
#********* End *********#
Copy the code

The second level aggregates the pipe operator to output the document in custom format (2)

Insert data first
use test2 
document = [
        {
                "_id" : 1,
                "course" : "Python expression problem solving training"."author" : "Li Tun"."tags" : [
                        "Python basis"."Solving"]."learning_num": 1882}, {"_id": 2."course" : "Basic Syntax for the Java language"."author" : "Yu yue"."tags" : [
                        "Java foundation"."Grammar"]."learning_num": 814}, {"_id": 3."course" : "Python Object-oriented Programming Training"."author" : "Li Tun"."tags" : [
                        "Python basis".Object oriented]."learning_num": 143}, {"_id": 4."course" : "Internet of Things Mobile Application Development for Android Comprehensive Training (1)"."author" : "prophet5"."tags" : [
                        "Android"."Internet of Things"."Mobile development"]."learning_num" : 207
        }
]
db.educoder.insert(document)

#********* Begin *********#
echo " db.educoder.aggregate({\$limit:3}); db.educoder.aggregate({\$sort:{learning_num:1}}); db.educoder.aggregate([{\$skip:2}]); "
#********* End *********#

Copy the code

The third level of the aggregate expression of the document data statistics

Insert data first (insert by yourself)
#********* Begin *********#
echo " db.educoder.aggregate([{\$group:{_id:'\$author',first_course:{\$first:'\$course'}}}]); db.educoder.aggregate([{\$group:{_id:'\$author',learning_avg:{\$avg:'\$learning_num'}}}]); db.educoder.aggregate([{ \$unwind:'\$tags'} , { \$group:{_id:'\$tags',course_num:{\$sum:1} } }] "
#********* End *********#

Copy the code

3-2 Index used by Didi and Mobike of MongoDB

The first step is to learn and create a simple index

use test 
document = [
        {
                "_id" : "1"."name" : "Wang Xiaoming"."age" : "15"."score" : "90"
        },
        {
                "_id" : "2"."name" : "Zhou Xiaoxiao"."age" : "18"."score" : "86"
        },
        {
                "_id" : "3"."name" : "Min"."age" : "20"."score" : "96"
        },
        {
                "_id" : "4"."name" : "Li Xiaoliang"."age" : "15"."score" : "74"
        },
        {
                "_id" : "5"."name" : "Zhang Qingqing"."age" : "21"."score" : "88"
        }
]
db.student.insert(document)
db.student.createIndex({score:-1})
Copy the code

The second level is the creation of common indexes

document = [
        {
                "_id" : "1"."title" : "6 Tools to Improve Programmer Productivity."."tags" : "Alfred, the curtain"."follwers": 543}, {"_id" : "2"."title" : "How I learned the front end from scratch."."tags" : "HTML,Html5,CSS"."follwers": 1570}, {"_id" : "3"."title" : "20 Very Useful JAVA Snippets"."tags" : "Java programming"."follwers" : 1920
        }
]
use test2
db.article.insert(document)
# create a compound ascending index with follwers and title;
db.article.createIndex({ follwers: 1, title: 1 });
# create a descending index with multi-key tags;
db.article.createIndex({ tags: -1 });
Create hash index with _id;
db.article.createIndex({ _id: "hashed" });
Create a text index with the fields title and tags.db.article.createIndex( ... {... title:"text". tags:"text". }...).Copy the code

The third level has the location index of NM fun

#**********Begin**********#
echo 'db.people.insert({_id: 1, name: "A", personloc: {type: "Point", coordinates: [116.403981, 39.914935]},}); Db.people. insert({_id: 2, name: "B", Personloc: {type: "Point", coordinates: [116.433733, 39.909511]},}); Db.people. insert({_id: 3, name: "C", personloc: {type: "Point", coordinates: [116.488781, 39.949901]},}); Db.people. insert({_id: 4, name: "D", Personloc: {type: "Point", coordinates: [116.342609, 39.948021]},}); Db.people. insert({_id: 5, name: "E", personloc: {type: "Point", coordinates: [116.328236, 39.901098]},}); Db.people. insert({_id: 6, name: "F", Personloc: {type: "Point", coordinates: [116.385728, 39.871645]},}); db.people.createIndex({ personloc: "2dsphere" }); Db. runCommand({geoNear: "people", near: {type: "Point", coordinates: [116.403981, 39.914935]}, Spherical: true, minDistance: 100, maxDistance: 3000, }); Db. runCommand({geoNear: "people", near: {type: "Point", coordinates: [116.433733, 39.909511]}, Spherical: true, minDistance: 100, maxDistance: 5000, }); Db. runCommand({geoNear: "people", near: {type: "Point", coordinates: [116.488781, 39.949901]}, Spherical: true, minDistance: 3000, maxDistance: 8000, }); Db. runCommand({geoNear: "people", near: {type: "Point", coordinates: [116.342609, 39.948021]}, Spherical: true, minDistance: 3000, maxDistance: 8000, }); '
#**********End**********#
Copy the code

3-3 Advanced query operations for MondoDB documents

The first step is data import and export

Select * from student.csv; select * from student.csv; select * from student.csv;
mongoimport -d mydb1 -c test --type csv  --headerline --ignoreBlanks --file /home/example/student.csv
# the/home/example/person. Json file into the database mydb2 test collection.
mongoimport -d mydb2 -c test  --type json --file /home/example/person.json
/home/test1.json; /home/test1.json;
mongoexport -d mydb1 -c test  -o /home/test1.json  --type json
/home/test1.csv; /home/test1.csv; /home/test1.csv
mongoexport -d mydb1 -c test  -o /home/test1.csv  --type csv -f "_id,name,age,sex,major"
Copy the code

Level 2 Advanced Query (1)

// Run the query command to find all people who like to sing and dance, and sort by _id; db.test.find({ hobbies: {$all: ["Sing"."Dance"] } }).sort({ _id: 1 }); // Run the query command to find all people who like badminton and dancing, and sort by _id ascending; db.test.find({ hobbies: {$all: ["Badminton"."Dance"] } }).sort({ _id: 1 }); // Run the query command to find information about people who have 3 hobbies, and sort them by _id ascending; db.test.find({ hobbies: {$size: 3 } }).sort({ _id: 1 }); // Execute the query command to find the information of the people with hobbies field in the document, and sort by the _id in ascending order; db.test.find({ hobbies: {$exists: true} }).sort({ _id: 1 }); // Run the query command to find the information of 19 and 23 years old, and sort by _id ascending; db.test.find({ age: {$in: [19, 23] } }).sort({ _id: 1 }); // Run the query command to find information about people who are not 20 years old, and sort by _id ascending; db.test.find({ age: {$nin: [20] } }).sort({ _id: 1 }); // Run the query command to find information about people whose age is mod 9 = 2, and sort by _id in ascending order. db.test.find({ age: {$mod: [9, 2] } }).sort({ _id: 1 });
Copy the code
#********* Begin *********#
echo "Db. The test. The find ({hobbies: {\ $all: [' singing ', 'dancing']}}), sort ({1} _id:); Db. The test. The find ({hobbies: {\ $all: [' badminton ', 'dancing']}}), sort ({1} _id:); db.test.find({ hobbies: { \$size: 3 } }).sort({ _id: 1 }); db.test.find({ hobbies: { \$exists: true } }).sort({ _id: 1 }); db.test.find({ age: { \$in: [19, 23] } }).sort({ _id: 1 }); db.test.find({ age: { \$nin: [20] } }).sort({ _id: 1 }); db.test.find({ age: { \$mod: [9, 2] } }).sort({ _id: 1 }); "
#********* End *********#
Copy the code

Level 3 Advanced Query (2)

mongoimport -d mydb3 -c test --type json --file /home/example/person.json
#********* Begin *********#
echo "Db. The test. The find ({\ $and: [{age: 20}, {sex: 'male'}]}), sort ({1} _id:); Db. The test. The find ({$or: \ [{age: 20}, {sex: 'male'}]}), sort ({1} _id:); Db. The test. The find ({name: ^ Korea. /}), sort ({1} _id:); db.test.find({\$and:[{age:{\$gte:19}},{age:{\$lt:22}}]}).sort({_id:1}); db.test.find({\$or:[{age:{\$lt:19}},{age:{\$gt:21}}]}).sort({_id:1}); Db. The test. The find ({name: {\ $not: / ^ Korea. /}}), sort ({1} _id:); Db. The test. The find ({name: {\ $not: / ^ Korea. * /}}), sort ({1} _id:). The count (); db.test.find({\$and:[{age:{\$gte:19}},{age:{\$lt:22}}]}).sort({_id:1}).count(); "
#********* End *********#
Copy the code

The fourth level is cursor

use mydb4
for(var i=0; i<10000; i++)db.test.insert({_id:i,title:"MongoDB"+i,content:"hello"+i})

mongoexport -d mydb4 -c test -o /home/test/test4.csv --type csv -f "_id,title,content"
Copy the code

Chapter 4 MongoDB distributed cluster

4-1 Mongodb Replication Sets & Fragments

Level one: ACD, ABD

The second part is MongoDB replication set construction

mkdir -p /data/test/db1 /data/test/db2 /data/test/db3
mkdir -p /logs/test
touch { /logs/test/mongod1.log /logs/test/mongod2.log /logs/test/mongod3.log }
mkdir -p /etc/test
touch { /etc/test/mongod1.conf /etc/test/mongod2.conf /etc/test/mongod3.conf }

Create three configuration files
port=20001 Configure the port number
dbpath=/data/test/db1 Configure the location where data is stored
logpath=/logs/test/mongod1.log Configure the location for storing logs
logappend=true # append logs
fork=true # Settings run in the background
replSet=CHANG Set the name of the replication set to be consistent across all servers

mongod -f /etc/test/mongod1.conf
mongod -f /etc/test/mongod2.conf
mongod -f /etc/test/mongod3.conf

config = {
  _id:"CHANG",
  members:[
      {_id:0,host:'127.0.0.1:20001'},
      {_id:1,host:'127.0.0.1:20002',arbiterOnly:true},
      {_id:2,host:'127.0.0.1:20003'},
  ]
}
rs.initiate(config)
Copy the code

The third chapter is MongoDB sharding set construction

mkdir -p /data/test2/shard1/db
mkdir -p /logs/test2/shard1/log
mkdir -p /data/test2/shard2/db
mkdir -p /logs/test2/shard2/log
mkdir -p /data/test2/shard3/db
mkdir -p /logs/test2/shard3/log
mkdir -p /data/test2/config/db
mkdir -p /logs/test2/config/log
mkdir -p /logs/test2/mongs/log
mkdir -p /etc/test2

root@evassh-6088960:/etc/test2# cat mongod1.conf mongod2.conf mongod3.conf 
# mongod1.conf
dbpath=/data/test2/shard1/db
logpath=/logs/test2/shard1/log/mongodb.log
port=21001
shardsvr=true
fork=true
# mongod2.conf
dbpath=/data/test2/shard2/db
logpath=/logs/test2/shard2/log/mongodb.log
port=21002
shardsvr=true
fork=true
# mongod3.conf
dbpath=/data/test2/shard3/db
logpath=/logs/test2/shard3/log/mongodb.log
port=21003
shardsvr=true
fork=true

# start the mongo
mongod -f /etc/mongo/mongod1.conf
mongod -f /etc/mongo/mongod2.conf
mongod -f /etc/mongo/mongod3.conf

# configuration config
 mongod --dbpath /data/test2/config/db --logpath /logs/test2/config/log/mongodb.log --port 21004 --configsvr --replSet cs --fork
21004 # link
mongo localhost:21004

use admin
cfg = {
  _id:'cs',
  configsvr:true,
  members:[
      {_id:0,host:'localhost:21004'}
   ]
}
rs.initiate(cfg)

# configuration route
mongos --configdb cs/localhost:21004 --logpath /logs/test2/mongs/log/mongodb.log --port 21005 --fork
# connect 21005
mongo localhost:21005

sh.addShard('localhost:21001')
sh.addShard('localhost:21002')
sh.addShard('localhost:21003')
Copy the code

Chapter 5 Data Backup and Recovery

The first step is data backup

# command line
mongoimport -d test1 -c person --type json --file /home/example/person.json

mongoimport -d test2 -c student --type csv --headerline --ignoreBlanks --file /home/example/student.csv

/opt/mongodbMongodump -h 127.0.0.1:27017 -o /opt/mongodbMongodb_1 /opt/mongodb_1Mongodump -h 127.0.0.1:27017 -d test1 -o /opt/mongodb_1Backup person collection to /opt/collection_1;Mongodump -h 127.0.0.1:27017 -d test1 -c person -o /opt/collection_1Student = /opt/collection_2;Mongodump -h 127.0.0.1:27017 -d test2 -c student -o /opt/collection_2 --gzipMongodb_2 /opt/mongodb_2Mongodump -h 127.0.0.1:27017 -d test2 -o /opt/mongodb_2 --gzip mongodump -h 127.0.0.1:27017 -d test2 -o /opt/mongodb_2 --gzipCopy the code

The second part is data recovery

Restore data from /opt/mongodb to mongodb;Mongorestore -h 127.0.0.1:27017 / opt/mongoMongodb_1 /opt/mongodb_1 restore mytest1;Mongorestore -h 127.0.0.1:27017 -d mytest1 --drop /opt/mongodb_1/test1/Mytest2; /opt/collection_1;Mongorestore -h 127.0.0.1:27017 -d mytest2 -c person --drop /opt/collection_1/test1/person.bsonAlter table student select * from mytest3; alter table student select * from /opt/collection_2;Mongorestore -h 127.0.0.1:27017 -d mytest3 --drop -c student --gzip /opt/collection_2/test2/student.bson.gzMongodb_2 /opt/mongodb_2 restore mytest4 database /opt/mongodb_2 restore mytest4 database /opt/mongodb_2Mongorestore -h 127.0.0.1:27017 -d mytest4 --drop -c student --gzip /opt/mongodb_2/test2/student.bson.gzCopy the code

Chapter vi

The first chapter optimizes the query principle

CD AD AC ABD BCD ABCD CD AD

Profiling tool of MongoDB (1)

Chapter 3 MongoDB Profiling Tool (ii)

Use mydb3 db. SetProfilingLevel (1, 5)for(var i=0; i<100000; i++)db.items1.insert({_id:i,text:"Hello MongoDB"+i})
for(var i=0; i<100000; i++)db.items2.insert({_id:i,text:"Hello MongoDB"+i})

Copy the code

Chapter vii.

Java operation MongoDB database (1)

package step1;
import java.util.function.Consumer;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.ArrayList;
import java.util.List;
import org.bson.Document;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.FindIterable;
import com.mongodb.Block;
public class Mongoconnect{
    public static void main( String args[] ){
        Logger log = Logger.getLogger("org.mongodb.driver");
        log.setLevel(Level.OFF);          // Mask time output
        try{
            // Add code below to connect to the mongodb service
            /********* Begin *********/
            MongoClient mongoClient = new MongoClient("localhost".27017);     // Start the local service on port 27020
            MongoDatabase mongoDatabase = mongoClient.getDatabase("databaseName");     // Connect to the database named databaseName

            /********* End *********/
            // Add the code below to create collection test1
            /********* Begin *********/
            mongoDatabase.createCollection("test1");     // create set test
            /********* End *********/
            // Add code below to get collection test1
            /********* Begin *********/
            MongoCollection<Document> collection = mongoDatabase.getCollection("test1"); 
            /********* End *********/
            // Add code below to insert the data from the programming requirements into collection test1
            /********* Begin *********/
            Document document1 = new Document();     // Create a document document1
            document1.append("_id"."1");
            document1.append("name"."Xiaoming");
            document1.append("sex"."man");
            document1.append("age".21);
            List<Document> documents = new ArrayList<Document>();     // Pack the above documents and store them in preparation for insertion
            documents.add(document1);
            collection.insertMany(documents);     // Insert multiple documents into the collection
            /********* End *********/
            // Add code between Begin and End. Do not modify the original framework of the code
            FindIterable<Document> iter = collection.find();
            iter.forEach(new Consumer<Document>() {
                @Override
                public void accept(Document document) { System.out.println(document.toJson()); }}); Document doc = collection.find().first(); collection.deleteOne(doc); }catch (Exception e) {
            System.err.println( e.getClass().getName() + ":"+ e.getMessage() ); }}}}Copy the code

Chapter 2 Java operation MongoDB database (2)

package step2;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import org.bson.Document;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.FindIterable;
import com.mongodb.Block;  
import com.mongodb.client.model.Filters;
import com.mongodb.client.MongoCursor;
public class Mongo{
    public static void main( String args[] ){
        Logger log = Logger.getLogger("org.mongodb.driver");  
        log.setLevel(Level.OFF);          // Mask time output
        try{ 
            // As in the first level, connect to database myDB2 and select collection test2
            /********* Begin *********/ 
            MongoClient mongoClient = new MongoClient("localhost".27017);     // Start the local service on port 27017
            MongoDatabase mongoDatabase = mongoClient.getDatabase("mydb2");     // Connect to a database named myDB2
            MongoCollection<Document> collection = mongoDatabase.getCollection("test2"); 

            /********* End *********/
            // Add the code below to insert the document into collection test2
            /********* Begin *********/ 
            Document document1 = new Document();      
            document1.append("_id"."1");
            document1.append("name"."Xiaoming");
            document1.append("sex"."man");
            document1.append("age".21);

            Document document2 = new Document();      
            document2.append("_id"."2");
            document2.append("name"."Xiaohong");
            document2.append("sex"."woman");
            document2.append("age".20);

            Document document3 = new Document();      
            document3.append("_id"."3");
            document3.append("name"."Xiaoliang");
            document3.append("sex"."man");
            document3.append("age".22);

            List<Document> documents = new ArrayList<Document>();     // Pack the above documents and store them in preparation for insertion
            documents.add(document1);
            documents.add(document2);
            documents.add(document3);

            collection.insertMany(documents);     // Insert multiple documents into the collection
            /********* End *********/
            // Add code between Begin and End. Do not modify the original framework of the code
            FindIterable<Document> iter = collection.find();
            System.out.println("Document insert result is as follows:");
            iter.forEach(new Block<Document>() {
              public void apply(Document _doc) { System.out.println(_doc.toJson()); }});// Add code below to update Xiaohong to be 23 years old
            /********* Begin *********/ 
            collection.updateMany(Filters.eq("name"."Xiaohong"), new Document("$set".new Document("age".23)));

            /********* End *********/
            // Add code between Begin and End. Do not modify the original framework of the code
            FindIterable<Document> findIterable = collection.find();  
            MongoCursor<Document> mongoCursor = findIterable.iterator(); 
            System.out.println("The updated document reads as follows:");
            while(mongoCursor.hasNext()){  
            System.out.println(mongoCursor.next());  
            };
            // Add code below to delete information about Xiaoliang
            /********* Begin *********/ 
            collection.deleteOne(Filters.eq("name"."Xiaoliang"));
            /********* End *********/ 
            // Add code between Begin and End. Do not modify the original framework of the code
            FindIterable<Document> iter1 = collection.find();
            System.out.println("The content of the document after deleting the information is:");
            
            iter1.forEach(new Consumer<Document>() {
                @Override
                public void accept(Document document) { System.out.println(document.toJson()); }}); collection.drop(); }catch (Exception e) {
            System.err.println( e.getClass().getName() + ":"+ e.getMessage() ); }}}Copy the code