当前位置:网站首页>Mongodb command summary

Mongodb command summary

2022-07-07 13:12:00 cui_ yonghua

The basic chapter ( Can solve the problem of 80% The problem of ):

  1. MongoDB Overview 、 Application scenarios 、 Download mode 、 Connection mode and development history, etc

  2. MongoDB data type 、 Key concepts and shell Commonly used instructions

  3. MongoDB Various additions to documents 、 to update 、 Delete operation summary

  4. MongoDB Summary of various query operations

  5. MongoDB Summarize the various operations of the column

  6. MongoDB Summary of index operations in

Advanced :

  1. MongoDB Summary of aggregation operations

  2. MongoDB Import and export of 、 Backup recovery summary

  3. MongoDB Summary of user management

  4. MongoDB Copy ( Replica set ) summary

  5. MongoDB Slice summary

  6. MongoDB meet spark( Integration )

  7. MongoDB Internal storage principle

Other :

  1. python3 operation MongoDB Various cases of

  2. MongoDB Command summary

One . MongoDB Command help system

See the detailed usage of each command , have access to :db.listCommands()

More detailed help commands for database operation :db.help()

Operate on the collection of the specified database 、 Management and monitoring :db.mycoll.help()

Two . Basic commands and examples

( One ) Basic commands

Displays the databases on the current database server :show dbs

Switch to the specified database pagedb Of :use pagedb

Show all collections in the database :show collections or show tables

View the status of the database server :db.serverStatus()

Query the specified database statistics :db.stats()

( Two ) Commonly used DDL and DML

understand DML、DDL、DCL、DQL Major name

SQL Language can be divided into four categories : Data manipulation language DML, Data definition language DDL, Data control language DCL, Data query language DQL.

DML(data manipulation language  Data manipulation language ):  They are UPDATE、INSERT、DELETE, Just like its name , this 4 Command is the language used to operate the data in the database 

DDL(data definition language  data ( library ) Define language ):  The main orders are CREATE、ALTER、DROP etc. ,DDL Mainly used to define or change tables (TABLE) Structure , data type , The initialization of links and constraints between tables , Most of them use it when creating tables 

DCL(Data Control Language  Data control language ):  It's the database control function . Is used to set or change the database user or role permissions statement , Include (grant,deny,revoke etc. ) sentence . By default , Only sysadmin,dbcreator,db_owner or db_securityadmin Wait for the people to have the power to carry out DCL

DQL(Data Query Language  Data query language ): It's the database control function , Include  select  sentence 

Create database mydatabase: use mydatabase; ( Just switch , Not to create )

Create set student
db.createCollection("student",{capped:true,size:1000,max:100})

Have a try ,max be equal to 3 perhaps size be equal to 1, What will happen? ?

Insert update record

surface (student)

 {
    _id:1,classid:1,age:18,name:"little1",love:["football","swing","cmpgame"]}
 {
    _id:2,classid:2,age:19,name:"little2",love:["play"]}
 {
    _id:3,classid:2,age:20,name:"little3"}
 {
    _id:4,classid:1,age:21,name:"little4"}
 {
    _id:5,classid:1,age:22,name:"little5",opt:"woshisheia"}
 {
    _id:6,classid:2,age:23,name:"23little4"}

Insert

 db.student.save({
    _id:1,classid:1,age:18,name:"little1",love:["football","swing","cmpgame"]})
 db.student.save({
    _id:2,classid:2,age:19,name:"little2",love:["play"]})
 db.student.save({
    _id:3,classid:2,age:20,name:"little3"})
 db.student.save({
    _id:4,classid:1,age:21,name:"little4"})
 db.student.save({
    _id:5,classid:1,age:22,name:"little5",opt:"woshisheia"})
 db.student.save({
    _id:6,classid:2,age:23,name:"23little4"})

perhaps

db.student.insert([
 {
    _id:1,classid:1,age:18,name:"little1",love:["football","swing","cmpgame"]},
 {
    _id:2,classid:2,age:19,name:"little2",love:["play"]},
 {
    _id:3,classid:2,age:20,name:"little3"},
 {
    _id:4,classid:1,age:21,name:"little4"},
 {
    _id:5,classid:1,age:22,name:"little5",opt:"woshisheia"},
 {
    _id:6,classid:2,age:23,name:"23little4"},
 ])

summary : Once the set starts capping mode , When max Or is it size beyond , New data over old data

Look up a record / Multiple records

Query out name by little1 The data of : db.student.find({name:"little1"})

From the second search , Find three : db.student.find().skip(1).limit(3)

The query is greater than 19 Less than or equal to 21:db.student.find({age:{$gt:19,$lte:21}})

Query out age Odd data ( Yes 2 The rest is 1 Is an odd number ):db.student.find({age:{$mod:[2,1]}})

Query the existence of opt Field data :db.student.find({opt:{$exists:true}})

The query does not exist opt Field data :db.student.find({opt:{$exists:false}})

Query out name Not for little2 The data of :db.student.find({name:{$ne:"little2"}})

Query out age by 16,18,19 The data of :db.student.find({age:{$in:[16,18,19]}})

Query out age Not for 16,18,19 The data of :db.student.find({age:{$nin:[16,18,19]}})

Query out love There are three data :db.student.find({love:{$size:3}})

Query out name Not in litt Initial data :db.student.find({name:{$not:/^litt.*/}})

Inquire about age=“”>20 The data of :db.student.find({age:{$gt:20}})

Press age Ascending :db.student.find().sort({age:1})

Press age Descending :db.student.find().sort({age:-1})

Number of query data :db.student.find().count()

Delete record

Delete age by 20 The data of :db.student.remove({age:20});

Create index

Create index age:db.student.ensureIndex({"age":1})

Query index :db.student.getIndexes()

Delete index :db.student.dropIndex({"age" : 1})

Count the number of set records :db.student.count()

Delete the collection student:db.student.drop()

Delete database mydatabase:db.dropDatabase()

( 3、 ... and ) security management

Start in secure authentication mode

Add users

Safety certification

( Four ) The data backup 、 Recovery and migration management

Back up all databases

Back up the specified database

Back up a collection in a database

Restore all databases

Recover data from a database

Recover data from a collection of a database

towards MongoDB Import data

from MongoDB Derived data

( 5、 ... and ) Remote connection management

be based on mongo Realize remote connection
mongo —host 192.168.17.129 —port 27017

( 6、 ... and ) polymerization
Calculate the average age of each class

db.student.aggregate([
 {
    
    $group: {
    
      _id: "$classid",
      avg_age: {
    
        $avg: "$age"
      }
    }
  }
])

Calculate the average age of the whole class

db.student.aggregate([
 {
    
    $group: {
    
      _id: null,
      avg_age: {
    
        $sum: "$age"
      }
    }
  }
])

( 7、 ... and ) MapReduce

Calculate the average age of each class

Grouping statistics (MapReduce):

var mapfun = function(){
    emit(this.classid,this.age);}
var reducefun = function(key, values){
    
    var total=0; 
    for(var i=0;i<values.length;i++){
    
        total += values[i];
    }
    return {
     classid: key, age_avg: total/values.length };
}
db.student.mapReduce(
      mapfun,
      reducefun,
      {
     out: "student_res" }
)

Query statistical results ( Count the number of people by class ):db.student_res.find()

Count the number of students in each class
Grouping statistics (MapReduce):

var mapfun = function(){
    emit(this.classid,1);}
var reducefun = function(key, values){
    
     return Array.sum(values);
}
db.student.mapReduce(
      mapfun,
      reducefun,
      {
     out: "student_sta" }
)

Query statistical results ( Count the number of people by class ):db.student_sta.find();

原网站

版权声明
本文为[cui_ yonghua]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207071117314035.html