当前位置:网站首页>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();
边栏推荐
- 日本政企员工喝醉丢失46万信息U盘,公开道歉又透露密码规则
- PACP学习笔记三:PCAP方法说明
- 信号强度(RSSI)知识整理
- Vscode编辑器ESP32头文件波浪线不跳转彻底解决
- 高瓴投的澳斯康生物冲刺科创板:年营收4.5亿 丢掉与康希诺合作
- Practical example of propeller easydl: automatic scratch recognition of industrial parts
- PACP学习笔记一:使用 PCAP 编程
- Grep of three swordsmen in text processing
- Sample chapter of "uncover the secrets of asp.net core 6 framework" [200 pages /5 chapters]
- LIS 最长上升子序列问题(动态规划、贪心+二分)
猜你喜欢
Go language learning notes - structure
Lingyunguang of Dachen and Xiaomi investment is listed: the market value is 15.3 billion, and the machine is implanted into the eyes and brain
. Net ultimate productivity of efcore sub table sub database fully automated migration codefirst
COSCon'22 社区召集令来啦!Open the World,邀请所有社区一起拥抱开源,打开新世界~
聊聊伪共享
【无标题】
TPG x AIDU|AI领军人才招募计划进行中!
Sed of three swordsmen in text processing
HZOJ #240. Graphic printing IV
Differences between MySQL storage engine MyISAM and InnoDB
随机推荐
HZOJ #235. Recursive implementation of exponential enumeration
MongoDB的用户管理总结
.Net下极限生产力之efcore分表分库全自动化迁移CodeFirst
MongoDB命令汇总
Analysis of DHCP dynamic host setting protocol
Isprs2021/ remote sensing image cloud detection: a geographic information driven method and a new large-scale remote sensing cloud / snow detection data set
记一次 .NET 某新能源系统 线程疯涨 分析
分屏bug 小记
Differences between MySQL storage engine MyISAM and InnoDB
PCAP学习笔记二:pcap4j源码笔记
JNA学习笔记一:概念
Cmu15445 (fall 2019) project 2 - hash table details
ISPRS2021/遥感影像云检测:一种地理信息驱动的方法和一种新的大规模遥感云/雪检测数据集
COSCon'22 社区召集令来啦!Open the World,邀请所有社区一起拥抱开源,打开新世界~
PACP学习笔记一:使用 PCAP 编程
PACP学习笔记三:PCAP方法说明
如何让electorn打开的新窗口在window任务栏上面
Lingyunguang of Dachen and Xiaomi investment is listed: the market value is 15.3 billion, and the machine is implanted into the eyes and brain
TPG x AIDU|AI领军人才招募计划进行中!
Realbasicvsr test pictures and videos