MongoDB-basic commands

Basic command

One, database commonly used commands

1. View the command prompt


2. Switch/create database

image-20210325172158613

Switch to the database named mydb. If the database does not exist, it will be created automatically.

The default database in MongoDB is test. If a collection or document operation is performed without creating a new database, the data will be stored in the test database.

Another way to switch databases is to specify the database name when entering the mongo client, for example:

image-20210325172401828

3. Query all databases

image-20210325172518854

4. Delete the currently used database

image-20210329192001913

5. Clone the database from the designated host (applicable to version 4.0 and below)

db.cloneDatabase("192.168.199.9");

Clone the data of the database with the same name on the specified machine to the current database. For example, the current database is the test database. The command will clone the test database in the 192.168.199.9 server to the test database of the local machine.

6. Copy the specified database data from the specified machine to a database (applicable to version 4.0 and below)

db.copyDatabase( "mydb", "temp","192.168.199.9");

Copy the data of mydb at 192.168.199.9 to the local temp database.

7. View the currently used database

image-20210329200323079

8. Display the current db status

image-20210329200505417

This command displays the statistics of the database, including the number of collections, average document size, data size, index number and size, etc.

9. The current db version

image-20210329200718499
image-20210329200843574

11. Clear error records

image-20210329200958576

Second, the collection

1. Create a collection

image-20210329201226876

Or create a fixed set with parameters:

image-20210329201635337

When performing a document writing operation, if the collection does not exist, the collection will be created automatically, so it is generally less to use the create collection command, unless it is to create a fixed collection.

• capped: Whether to enable the collection restriction, there are two choices: true or false, when it is true, the restriction is enabled and created as a fixed collection; if it is enabled, a restriction condition needs to be established, the default is false, and the fixed collection is not enabled.

• size: Limit the size of the space used by the collection, the default is no limit.

• max: The maximum number of items in the collection is limited. The default is no limit.

• autolndexId: Whether to use id as the index, there are two choices: true or false, the default is true, and _id is used as the index.

Note: The priority of size is higher than max.

2. Display the collection in the current database

image-20210329201849024

3. Use collections

image-20210329201947642

Note that the db.mycoll collection method cannot be used when the collection name is all numbers. For example, if db.123 reports an error, you can use db.getCollection(" 123" ).

4. View the collection command help document

image-20210329202158598
image-20210329202219022

5. Query the number of data in the current collection

image-20210329202438514

6. View the size of the collection data

image-20210329202526925

The unit of the displayed number is byte, so if you need to convert the unit to KB, you need to divide by 1024

7. View the collection index size

image-20210329202653367

8. View the amount of space allocated for the collection, including unused space

image-20210329203020593

9. Display the total size of the collection, including the size of the index and data and the size of the allocated space

image-20210329203101088

10. Display the db where the current collection is located

image-20210329203135668

11. Display the status of the current collection

image-20210329203421934

There are too many status data in the current mydb database, which is inconvenient to take a screenshot. Please execute this command after changing the database to test.

12. The shard version information of the collection

image-20210329203541410

13. Rename the collection

image-20210329205317787
image-20210329204010365

When you rename mycoll to users and the collection name is pure numbers, you can only use db.getCollection("mycoll").renameCollection.

14. Display the status information of all collections of the current db

db,printCollectionStats();
image-20210329205403667

15, delete the current collection

image-20210329205619559

Three, the document

(1) Write the document:

db.user.insert({"name":"joe"});
or
db.user.save({"name":"joe"});

​ The difference between save and insert:

​ When using save, if the set already hasSame id fieldIf there is no document, it will be updated; if not, it will be written.

​ When using insert, if the collection already hasSame id fieldIf there is no document, it will report an error E11000 duplicate key error collection; if not, write it.

(2) View documents

db.user.find();

image-20210413160134394

(3) Update documents

You can use save() to update the documents in the collection, match the documents with _id, and replace the existing documents with the newly-incoming documents.

image-20210413160501307

You can also use the update() method to update an existing document. The syntax format is as follows:

image-20210413160547620

Parameter Description:

query : The query condition of update, similar to the content after where in the sql update query.

update : the object of update and some update operators (such as ,, ,inc...), etc., can also be understood as the content after the set in the sql update query.

upsert : Optional. This parameter means whether to insert a new document if there is no document in the collection that meets the query conditions. True means insert, and the default is false, not insert.

multi : Optional, the default is false, only the first record found will be updated. If this parameter is true, all multiple records found according to the conditions will be updated.

writeConcern : Optional, the level of the exception thrown.

WriteConcern parameter description: Use the NORMAL mode parameter (default) to make the write operation very efficient. However, if the server makes an error at this time, it will not return an error to the client, and the client will mistakenly believe that the operation was successful. Therefore, it is necessary to use WriteConcern.SAFE mode in many important write operations to ensure that this error can be sensed, and to ensure that the client and server have the same understanding of the correctness of an operation.

db.user.update({"myName":"joe"},{$set:{"age":20,"company":"google"}},true,{multi:true}, ,WriteConcern.SAFE);

image-20210413162624764

db.user.update({"myName":"joe"},{$set:{"age":20,"company":"google"}},true,true, WriteConcern.SAFE);

image-20210414223621863

db.user.update({"myName":"joe"},{$set:{"age":20,"company":"google"}},true, WriteConcern.SAFE);

image-20210414223912008

db.user.update({"myName":"Joe"},{$set:{"age":20,"company":"google"}},{multi:1});

image-20210414224043496

(4) Delete documents

image-20210414224250620
image-20210414225028351

The syntax of the findAndModify method is as follows:

image-20210414224830744

The runCommand() function can execute special functions in mongoDB, but it can only process one document at a time. findAndModify as a special function, it returns the document after update or remove. The syntax of the runCommand method is as follows:

image-20210414225704393

(5) Update and return the document:

​ findAndModify() method

image-20210414230423790

​ runCommand() method

image-20210414230807711

(6) Delete the document and return to the document

findAndModify method, runCommand() method

image-20210414231421770

(7) Query the number of documents that meet the conditions

image-20210414231920681

Four, index

(1) Create an index

MongoDB uses the createIndex() method to create an index.

note: Before version 3.0.0, the index creation method was db.collection.ensureIndex(), and later versions used the db.collection.createIndex() method. EnsureIndex() can still be used, but it is just an alias of createIndex().

grammar

The basic syntax format of createIndex() method is as follows:

db.collection.createIndex(keys, options)

The Key value in the grammar is the index field you want to create. 1 specifies that the index is created in ascending order. If you want to create the index in descending order, specify -1.

First use the save method to put the following data into the user collection

{"MyName":"joe",age:14},

{"MyName":"ad",age:14},

{"MyName":"ad",age:38},

{"MyName":"ad",age:24},

{"MyName":"ab",age:14}

image-20210416220126344

Use db.user.createIndex({myName: 1, age: -1}) to create a composite index, which means that all documents in the collection are sorted in ascending order according to myName, and documents in the same group as myName are sorted in descending order by age.

image-20210416220307693

The arrangement of the index documents is as follows:

{"MyName":"ab",age:14}

{"MyName":"ad",age:38}

{"MyName":"ad",age:24}

{"MyName":"ad",age:14}

{"MyName":"joe",age:14}

-------------------------Dividing line-----------------------

Background parameter, true means to create an index in the background, the default is false

image-20210416220536255

Reasons for creating indexes in the background:

The database is locked during index creation in the foreground, which will cause other operations to be unable to read and write data; index creation in the background will periodically release write locks to ensure the operation of other operations, but background operations will take longer, especially in frequent On the server where the write is performed.

-------------------------Dividing line-----------------------

Unique parameter, true means a unique index, the corresponding key is required to be uniquely identified in the collection, and the default is false. If there are duplicate entries in the corresponding key when the unique index is created, the command will report an error.

image-20210416220925419

dropDups parameters,3.0+ version is obsolete. Whether to delete duplicate records when creating a unique index, specify true to create a unique index, and the default value is false.

image-20210416221110232

The Sparse parameter does not enable indexing of field data that does not exist in the document; this parameter requires special attention, if it is set to true, documents that do not contain the corresponding field and the document with the corresponding field value will not enter the index. The default value is false.

image-20210416221136948

(2) Query all indexes of the collection

image-20210416221238599

(3) View the total index record size of the collection

image-20210416221330130

(4) Read all index information of the current collection

image-20210416221429379

(5) Delete the specified index

image-20210416221626956

(6) Delete all indexes of the collection

image-20210416221701187

Five, basic query

1. Introduction to find

Use find() to query the document toUnstructuredWay to display the returned document

Add the pretty() method after the query statement toStructuredWay to display the returned document

image-20210422194044403

Find() syntax:find(查询条件,指定返回字段)

The two parameters must be documents, and the _id field is returned by default

image-20210422195244661

new method:

image-20210422195406082
From the Internet

2. Cursor

A cursor is a container, generally used to traverse a data set, and can store the results of find execution. No matter how many pieces of data are put into the cursor,Only one piece of data can be extracted at a time. Judge whether there is the next piece of data through hasNext(), and next() gets the next piece of data.

image-20210422200010249

The cursor implements the iterator interface and can use the forEach function

image-20210422200340013

Six, conditional query

The first parameter of find() is the query condition document. The query condition document needs to meet the BSON format. MongoDB provides a variety of query methods.

1. And operation

image-20210422201141439
  • No documents that satisfy both myName as ad and age as 16 are not found
  • Query documents that satisfy both myName as ad and age as 14

2. Or operate $or

image-20210422201513332
  • Find the documents whose age is 38 or 24, and only one of the conditions is satisfied.

3. Greater than $gt (greater than)

image-20210422201939801
  • Query documents with age greater than 20.

4. Less than $lt (less than)

image-20210422202057297
  • Query documents whose age is less than 20.

5. Greater than or equal to $gte (greater than or equal to)

image-20210422202807901
  • Query documents with age greater than or equal to 20

6, less than or equal to $lte (less than or equal to)

image-20210422203001873
  • Query documents whose age is less than or equal to 20

7. Type query $type

The $type operator is used to query the data that matches the specified type in the field in the document and return the result.

image-20210422203222339

The usage is as follows:

image-20210422203456581
  • Query documents whose myName field is string type

8. Does $exists exist?

image-20210422205818958
  • Query documents that exist in the age field

9. Take the modulus $mod

image-20210422210104006
  • Query the data whose age field modulo 10 is equal to 0

10. Not equal to $ne (not equal to)

image-20210422210242257
  • Query data whose age is not equal to 20

11. Contains $in

image-20210422210412686
  • Query the documents in which the value of myName is contained in the ["tutu", "little fairy", "ab"] array

12. Does not contain $nin

image-20210422210643652
  • Query documents where the value of myName is not included in the ["Tutu","Little Fairy","ab"] array

13. Anti-matching $not:

All of the above field query operations can be negated, such as:

image-20210422210757215

Seven, specific types of queries

1, null

Note that no quotation marks are added before and after null! !

image-20210425115528112

2. Regular query (fuzzy query)

Regular expressions are used to retrieve and replace text that == matches a certain pattern (rule) ==.

See the textbook for the basic symbols and codes of commonly used regular expressions

example:

\S+

<a[^>]+>

^J

of despair$

^abc$

notice

,[a-zA-Z0-9]$

The usage of regular expressions in MongoDB:

image-20210425155112517

3. Nested documents

3.1 Exact matching query
image-20210425161544101
3.2 Point query
image-20210425161640883

4. Array

Use the update method to update the document and insert the favorite_number field

image-20210425162214963
image-20210425162250744
4.1 Array single element query
image-20210425162510900
4.2 $all array multi-element query
image-20210425162614965
4.3 $size array length query
image-20210425162659954
4.4 $slice returns a subset of the array
image-20210426162831326

$slice needs to be placed in the second parameter as a limited parameter

image-20210426162959217

Because the first parameter is the query condition

image-20210426163422991

What does it mean to set the value of $slice to -2?

image-20210426163540945

-2 means: intercept 2 elements from the right side of the array

4.5 Exact match query
image-20210426164023877
4.6 Point query
image-20210426164739638

Used to query complex arrays, such as the array contains subdocuments.

image-20210426164712900
4.7 Index query*

Note that the index field should be enclosed in double quotes, such as "favorite_number.0"

image-20210426165102641
image-20210426165233759

*Number.0 represents the subscript of the array

4.8 Element query $elemMatch

method 1:

image-20210426170108366

Method 2:

image-20210426170615370

Eight, advanced query $where

The previous query conditions are all BSON format of key-value pairs, and the BSON format is not adequate for complex query conditions. MongoDB provides $whereoperators,Any JavaScript can be executed as part of the query conditions, To achieve complex queries that the BSON format cannot handle.

image-20210426171328816

JavaScript combined with $where

There are several ways to query documents with age>18

image-20210426171611960
image-20210426171718192
image-20210426171929834

The most typical application of $where: For a document, if the values ​​of two keys are equal, select it, otherwise do not select:

image-20210426171959821
image-20210426172122610

Nine, query assistance

1. Conditions limit

image-20210515170502280

2. Starting position skip

image-20210515170851716

3. Sort sort

image-20210515171309017

Ten, modifier

1. $set

$set is used to specify the modified fields and values.

image-20210518114540910
image-20210518114844342

Change the age field value of the document whose myName is joe to 18, and change the value of the company field to Huawei

{multi:1}Indicates that multiple records are updated, and can also be used{multi:true}

2. $unset

$unset is used to cancel a field, that is, delete a field in the document.

image-20210518115553734

Remove the company field of the document whose myName is joe.

3. $inc

$inc is used to increase or decrease the value

image-20210520115046466

Increase the age of the document whose myName is joe by 50.

4. $push

Append element toArray fieldin. If the field does not exist, it will be added, and the value of the field will be an array.

image-20210520142710251

5. $pushAll

image-20210520143600316

p u s h A l l has been connected with pushAll p u s h A l l has been withpush merge, $pushAll is no longer used

image-20210520144346272

Use $push to append multiple elements to the array, and the added elements will be inserted into the array as a sub-array. If you want to append multiple elements directly to the array, you need to use it with $each.

image-20210522161131470
image-20210522161408577
image-20210522161933241
image-20210522162308590

$push is used with ​$each to add multiple elements directly to the array.

image-20210522163049544

6, $pull

Delete the elements that meet the conditions in the array. If there are multiple elements that meet the condition, they are all deleted. And $push is the opposite operation.

image-20210522163812384

7, $addToSet

Append an element not in the original array to the array. Note the difference with $push.

image-20210522164103143

8. $pop

Delete the first element (-1) or the last element (1) in the array

image-20210522164248428

9. $rename

Modify field name

image-20210522164421488

10. $bit

Bitwise operation, convert decimal to binary, and get the return value through bitwise AND, or bitwise OR, and xor bitwise XOR.

Common scenarios:

and 1: Take the last number of the binary number, which can be used to judge the parity (the end of an even number is 0, and the end of an odd number is 1)

Or 1: Force the mantissa of the binary number to 1

XOR the same number twice, the result is equal to itself, that is (a xor b) xor b=a: can be used for simple encryption

image-20210522164808806

Eleven, native aggregation operations

1. Query count

Count the number of documents that meet the conditions

The number of count statistics in the sharded cluster is inaccurate

image-20210523124731814

2. Different value distinct

Used to find all the different values ​​of the specified field

Need to specify collections and fields

image-20210523125144476

3. Group group

image-20210523125913305

PuTTy: Option> encoding> UTF-8

image-20210523130119422

Group method is deprecated

image-20210523130148733

4. Flexible statistical MapReduce

Some advanced aggregation functions, such as sum, average, max, min, variance, standard deviation, etc. need MapReduce to implement

image-20210523130226762

MapReduce syntax

image-20210523130453035
image-20210523130507574

Example: Use mapreduce to complete the 26-page example of the textbook

  • Step 1: Insert the document
image-20210523130621200
image-20210523130718624

General writing

image-20210523132626069

Source code:

db.student.mapReduce(
 function(){
 emit(
 this.class,
 {age:this.class,count:1}
 );
 },
 function (key,values){
 var count = 0;
 values.forEach(function(val){
 count += val.count;
 });
 return {class:key,count:count};
 },
 {
 out:{inline:1},
 finalize:function(key,reduced){
 return{"班级":reduced.class,"人数":reduced.count};
 }
 }
 )

Simple writing

image-20210523133122830

Source code

db.student.mapReduce(
 function(){emit(this.class,1);},
 function(key,values){return Array.sum(values);},
 {out:{inline:1}}
)

练习1:运用MapReduce统计user集合中,每个年龄有多少人?

image-20210523133423700

练习2:运用MapReduce统计student集合中,男生女生各有多少人?

image-20210523133541754

十二、聚合管道

管道操作器

管道阶段(pipeline stages)需要使用管道操作器/符识别。管道操作器类型很多。

1、$project

修改输入文档的结构

image-20210604150103756
image-20210604150149321
image-20210604150420044
image-20210604150812761

Aggregate使用0值表示结果集中排除字段,1值表示结果集中包含字段,可直接给字段赋值来修改字段值,可直接给新字段赋值来新建字段 ,赋值时可用==$==引用原字段的值。

image-20210604151224334
image-20210604151327464

2、$match

用于过滤数据,只输出符合条件的文档。

image-20210604151831764
image-20210604151849850

3、$limit

用来限制MongoDB聚合管道返回的文档

image-20210604152233659

4、$skip

跳过指定数量的文档,返回余下的文档。

image-20210604152457070

5、$unwind

将文档中的某个数组字段拆分成多条,每条包含数组中的一个元素。

image-20210605090737888

6、$group

将集合中的文档分组,可用于统计结果,一般与管道表达式$sum等组合使用。

image-20210605091422068

把数据根据age字段进行分组:

db.user.aggregate([{$group:{_id:"$age"}}])
image-20210605091848616
db.user.aggregate([{$group:{_id:"$age",num:{$sum:1}}}])
image-20210605091827024

7、$sort

将输入文档排序后输出,1为升序,-1为降序

image-20210605092519255

8、$lookup

用于多表连接返回关联数据。执行左连接到一个集合(非分片集合),两个集合必须在同一数据库中。

image-20210605093453545

​ $lookup语法

{
    $lookup:
        {
            from:<collection to join>,
            localField:<field from the input documents>,
			foreignField:<field from the documents of the "from" collection>,
			as:<output array field>
        }
}
image-20210605094513743

示例:有product产品信息集合、order订单信息集合,order中的pid与product中的_id关联。

image-20210605095740451
image-20210605100045727
image-20210605101341876
image-20210605101529166

练习1:查询出订单中购买的产品价格大于90的订单信息?

image-20210605101804182

练习2:查询出产品1的产品信息和对应的订单信息

image-20210608111749087

9、$geoNear

用于输出接近某地理位置的有序文档,返回距离信息。

表面类型:平面和球面

表面类型会影响数据的存储、建立的索引的类型及查询的语法的形式。

1)平面

按照正常坐标对的形式(数组或内嵌文档)存放位置数据,使用2d索引。解决短距离的距离搜索场景。

可用的坐标对书写方式:

{loc:[60,30]}
{loc:{x:90,y:32}}
{loc:{foo:70,y:80}}
{loc:{lng:40,739037,lat:73.992964}}

示例:

创建places集合,并存入坐标信息

db.places.save(
	[{name:"肯德基",loc:{lng:40.739037,lat:73.992964},category:"餐饮"},
	{name:"麦当劳",loc:{lng:42.739037,lat:73.992964},category:"餐饮"},
	{name:"农行",loc:{lng:41.739037,lat:73.992685},category:"银行"},
	{name:"地铁站",loc:{lng:51.739037,lat:83.992685},category:"交通"}])
image-20210609111050103

需要给坐标对字段创建空间索引才可使用地理位置查询。

db.places.createIndex({"loc":"2d"})
image-20210609111118444

用find 查询从近到远的点

db.places.find({loc:{$near:{lng:40.639037,lat:73.992964}}})
image-20210609111230638

使用aggregate和$geoNear能指定范围。

示例:查询在坐标值相差2度(平面单位)以内的文档

db.places.aggregate([
	{
		$geoNear:{
			spherical:false,
			distanceMultiplier:1,
			near:{lng:40.639037,lat:73.992964},
			distanceField:"dist.distance",
			maxDistance:2,
			query:{category:"餐饮"},
			includeLocs:"dist.location",
			num:1
		}
	}
])
image-20210609111823181
image-20210609112037010
image-20210609112056273
db.places.aggregate([
	{
		$geoNear:{
			spherical:false,
			distanceMultiplier:1,
			near:{lng:40.739037,lat:73.992964},
			distanceField:"dist.distance",
			maxDistance:2,
			query:{category:"餐饮"},
			includeLocs:"dist.location",
		}
	},
	{$limit:1}
]).pretty()
image-20210609114637430
2)球面

按照普通坐标对或GeoJSON对象存储数据,使用2dphere索引。解决远距离的距离搜索场景。

GeoJSON对象可以有单点、线段、多边形、多点、多线段、多个多边形、几何体集合,如:

单点:

{type:"Point",coordinates:[40,5]}

线段:

{type:"LineString",coordinates:[[40,5],[41,6]]}

多边形:

{
	type:"Polygon",
	coordinates:[[[0,0],[3,6],[6,1],[0,0]]]
}

type表示类型,coordinates表示几何点。注意几何点的顺序是Ing, lat。

以单点为示例:

db.places.save(
	[{name:"肯德基",loc:{type:"Point",coordinates:[40.739037,73.992964]},category:"餐饮"},
	{name:"麦当劳",loc:{type:"Point",coordinates:[42.739037,73.992964]},category:"餐饮"},
	{name:"农行",loc:{type:"Point",coordinates:[41.739037,73.992685]},category:"银行"},
	{name:"地铁站",loc:{type:"Point",coordinates:[51.739037,83.992685]},category:"交通"}])
image-20210609113557392

创建places集合并存储坐标信息

image-20210609113747906

创建2dphere索引:

db.places2.createIndex({loc:"2dsphere"})
image-20210609113905402
db.places2.aggregate([
	{
		$geoNear:{
			spherical:true,
			near:{type:"Point",coordinates:[40.739037,73.992964]},
			distanceField:"dist.distance",
			maxDistance:2000,
			query:{category:"餐饮"},
			includeLocs:"dist.location",
		}
	},
	{$limit:5}
]).pretty()
image-20210609114408946

管道表达式

是管道操作器的值

是一个文档结构,由字段名、字段值和一些表达式操作符组成

跟find中使用的表达式类似

测试数据准备:

db.products.insert([
	{"_id":1,"name":"产品1","price":99,type:"服装"},
	{"_id":2,"name":"产品2","price":88,type:"服装"},
	{"_id":3,"name":"产品3","price":29,type:"饰品"},
	{"_id":4,"name":"产品4","price":78,type:"服装"},
	{"_id":5,"name":"产品5","price":9,type:"饰品"},
	{"_id":6,"name":"产品6","price":18,type:"饰品"},
])
image-20210609115257070

求和 s u m , 平 均 值 sum,平均值 sum,平均值avg,最小值 m i n, the maximum value min, the maximum value m I n- , most large valuemax

db.products.aggregate([{$group:{_id:"$type",price:{$sum:"$price"}}}]);
db.products.aggregate([{$group:{_id:"$type",price:{$avg:"$price"}}}]);
db.products.aggregate([{$group:{_id:"$type",price:{$min:"$price"}}}]);
db.products.aggregate([{$group:{_id:"$type",price:{$max:"$price"}}}]);
image-20210609115749247

Array add $push, $addToSet

db.products.aggregate([{$group:{_id:"$type",tags:{$push:"$name"}}}]);
db.products.aggregate([{$group:{_id:"$type",tags:{$addToSet:"$name"}}}]);
image-20210609120115105


The difference between $addToSet and ​$push is that duplicate values ​​will not enter the array.

First element $first, last element $last

db.products.aggregate([{$group:{_id:"$type",products:{$first:"$name"}}}]);
db.products.aggregate([{$group:{_id:"$type",products:{$last:"$name"}}}]);
image-20210609120355510

Compound usage example

The data is as follows. Use aggregate to count the number of documents with age greater than 13 in each group after being grouped by age.

db.user.aggregate([
	{$match:{age:{"$gt":13}}},
	{$group:{_id:"$age","人数":{$sum:1}}}
])
image-20210609120731967

Exercise: Explain the following two aggregate statistics codes and explain the differences

db.user.aggregate([
	{$match:{age:{"$gt":13}}},
	{$sort:{age:1}},
	{$limit:2},
	{$group:{_id:"$age","人数":{$sum:1}}}
])
db.user.aggregate([
	{$match:{age:{"$gt":13}}},
	{$group:{_id:"$age","人数":{$sum:1}}},
	{$sort:{_id:1}},
	{$limit:2},
])
image-20210609121313624

the difference:

- 前者是先根据年龄排序,取前2位,最后根据年龄分组;
- 后者是先根据年龄分组,在根据_id排序,取前2组。