当前位置:网站首页>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();
边栏推荐
猜你喜欢
单片机原理期末复习笔记
Sample chapter of "uncover the secrets of asp.net core 6 framework" [200 pages /5 chapters]
Introduce six open source protocols in detail (instructions for programmers)
日本政企员工喝醉丢失46万信息U盘,公开道歉又透露密码规则
自定义线程池拒绝策略
靠卖概念上市,认养一头牛能走多远?
AUTOCAD——大于180度的角度标注、CAD直径符号怎么输入?
[untitled]
通过Keil如何查看MCU的RAM与ROM使用情况
Awk of three swordsmen in text processing
随机推荐
日本政企员工喝醉丢失46万信息U盘,公开道歉又透露密码规则
. Net ultimate productivity of efcore sub table sub database fully automated migration codefirst
Aosikang biological sprint scientific innovation board of Hillhouse Investment: annual revenue of 450million yuan, lost cooperation with kangxinuo
聊聊伪共享
OSI 七层模型
《ASP.NET Core 6框架揭秘》样章[200页/5章]
Go language learning notes - structure
[learning notes] agc010
《开源圆桌派》第十一期“冰与火之歌”——如何平衡开源与安全间的天然矛盾?
货物摆放问题
How to reset Google browser? Google Chrome restore default settings?
MongoDB命令汇总
MongoDB 分片总结
.Net下極限生產力之efcore分錶分庫全自動化遷移CodeFirst
Ip2long and long2ip analysis
JS中为什么基础数据类型可以调用方法
基于鲲鹏原生安全,打造安全可信的计算平台
[QNX Hypervisor 2.2用户手册]6.3.4 虚拟寄存器(guest_shm.h)
【无标题】
ORACLE进阶(五)SCHEMA解惑