当前位置:网站首页>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 ):
MongoDB data type 、 Key concepts and shell Commonly used instructions
MongoDB Various additions to documents 、 to update 、 Delete operation summary
Advanced :
Other :
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 studentdb.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();
边栏推荐
猜你喜欢
随机推荐
[learning notes] agc010
MATLAB中polarscatter函数使用
MySQL入门尝鲜
ESP32构解工程添加组件
ESP32系列专栏
. Net ultimate productivity of efcore sub table sub database fully automated migration codefirst
Ip2long and long2ip analysis
Grep of three swordsmen in text processing
MongoDB 分片总结
Japanese government and enterprise employees got drunk and lost 460000 information USB flash drives. They publicly apologized and disclosed password rules
【黑马早报】华为辟谣“军师”陈春花;恒驰5预售价17.9万元;周杰伦新专辑MV 3小时播放量破亿;法华寺回应万元月薪招人...
regular expression
Why can basic data types call methods in JS
Milkdown 控件图标
How to reset Google browser? Google Chrome restore default settings?
DHCP 动态主机设置协议 分析
JS determines whether an object is empty
Cinnamon 任务栏网速
ClickHouse(03)ClickHouse怎么安装和部署
线程池拒绝策略最佳实践
![[Presto profile series] timeline use](/img/c6/83c4fdc5f001dab34ecf18c022d710.png)






![[untitled]](/img/6c/df2ebb3e39d1e47b8dd74cfdddbb06.gif)
