当前位置:网站首页>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();
边栏推荐
- 通过Keil如何查看MCU的RAM与ROM使用情况
- Sample chapter of "uncover the secrets of asp.net core 6 framework" [200 pages /5 chapters]
- Blog recommendation | Apache pulsar cross regional replication scheme selection practice
- 一文读懂数仓中的pg_stat
- 云检测2020:用于高分辨率遥感图像中云检测的自注意力生成对抗网络Self-Attentive Generative Adversarial Network for Cloud Detection
- MongoDB 遇见 spark(进行整合)
- 【学习笔记】zkw 线段树
- 共创软硬件协同生态:Graphcore IPU与百度飞桨的“联合提交”亮相MLPerf
- 聊聊伪共享
- JS function 返回多个值
猜你喜欢
随机推荐
Grep of three swordsmen in text processing
leecode3. 无重复字符的最长子串
Unity build error: the name "editorutility" does not exist in the current context
File operation command
API query interface for free mobile phone number ownership
达晨与小米投的凌云光上市:市值153亿 为机器植入眼睛和大脑
.Net下极限生产力之efcore分表分库全自动化迁移CodeFirst
我那“不好惹”的00后下属:不差钱,怼领导,抵制加班
Cinnamon 任务栏网速
MongoDB的导入导出、备份恢复总结
php——laravel缓存cache
LIS 最长上升子序列问题(动态规划、贪心+二分)
DETR介绍
Vscode编辑器ESP32头文件波浪线不跳转彻底解决
shell 批量文件名(不含扩展名)小写改大写
Pay close attention to the work of safety production and make every effort to ensure the safety of people's lives and property
Differences between MySQL storage engine MyISAM and InnoDB
【Presto Profile系列】Timeline使用
What are the benefits of ip2long?
Practical example of propeller easydl: automatic scratch recognition of industrial parts