[Study notes] mongoDB preliminary (1) MongoDB installation, additions, deletions, and basic grammar, usage and commands

MongoDB is suitable for storing large amounts of data that is not strongly related.
The data in MongoDB is stored in the structure of "library"-"collection"-"document"-"field"
. At first glance, this structure looks very similar to the "library"-"table"-"row"-"column" structure of a traditional relational database. However, MongoDB does not need to define the table structure in advance, the fields of the data can be changed arbitrarily, and the concurrent writing speed far exceeds that of traditional relational databases.

1. Installation

Download the 4.0
bin directory from the official website :

Insert picture description here


cfg is the configuration file

Create a new data folder under the c drive, and then create a new db:
enter the mongod.exe

Insert picture description here


Insert picture description here


graphical interface software: robo
3T The download address of Robo 3T is: https://robomongo.org/download

Insert picture description here


Insert picture description here


create database

The command to insert a single piece of data is "insertOne()".
db.getCollection('learning').insertOne({"name": "张小二", "age": 17, "address": "浙江"})

Insert picture description here


MongoDB also allows Key to be written as {name: '王小六',
age: 25, work:'Chef'}. However, this way of writing has some limitations, and it will
cause the MongoDB commands to be inconvenient for smooth porting to Python. Therefore, it is recommended to use single quotation marks or double quotation marks.
In MongoDB, the fields to insert data can be modified, added, or deleted at will. For
example, insert again:
db.getCollection('learning').insertOne({“today ": "6.8","tomo":"9"})

Insert picture description here


db.getCollection('learning').insertOne({"today": "June 8th","tomo":"9","week": "Tuesday"})

Insert picture description here


Insert picture description here


Tip:
"Can you do it" is one thing, and "should you do it or not" is another. Although
MongoDB can handle different data types of the same field, it can also add or subtract fields at will, but it does not mean that it should be done. When designing a database, you should try to ensure that the same field uses the same type of data, and consider in advance which fields should be included .

Additions, deletions, revisions

Bulk insert:

db.getCollection('learning').insertMany([{"name": "小明","age":"9"},
{"name": "小红","age":"19"},
{ "Name": "小王","age":"22"}])

Insert picture description here


Switch display mode:

Insert picture description here


Insert picture description here


Insert picture description here

Note:
Whether you are inserting a piece of data or inserting multiple pieces of data,
a field "_id" will be automatically added after each piece of data is inserted into MongoDB. "_Id" is pronounced as "Object Id", which is composed of time, machine code, process pid, and an increment counter.
"_Id" is always incremented, but never repeated.
● At the same time, the "_id" on different machines is different.
● The same machine has different "_id" at different times.
● For data inserted in batches at the same time on the same machine, "_id" is still different.

Query data

db.getCollection('learning').find()
or
db.getCollection('learning').find({}) to
query specific data:

Insert picture description here


db.getCollection('learning').find({'name':'小红'})

The "find" parameter is equivalent to a dictionary. The key of the dictionary is the field name, and the value of the dictionary is the value to be queried. If the dictionary has multiple keys, these fields must be satisfied at the same time.

1) To query all records whose "age" field is not less than 25,
you need to use the greater than or equal operator "$gte". The query statement is as follows:
db.getCollection('example_data_1').find({'age': {'$gte': 25}})

Insert picture description here


range operator and its meaning
Among them, the key of the dictionary used to qualify the field is the name of each field . There are only two values-0 or 1.
● If the value is 0, it means that these fields with a value of 0 are excluded from all fields and returned.
● If the value is 1, it means that only those fields with a value of 1 are returned.
For example, query the data set example_data_1, but do not return the "address" and "age" fields. The query statement is as follows:
db.getCollection('example_data_1').find({}, {'address': 0,'age': 0})
2) Return the number of items queried (how many items meet the requirements in total)
count() :
Db.getCollection('learning').find({'age': {'$gt': 21}}).count()

3) limit(4) limits only to return 4 pieces of data.
The specific command is as follows:
db.getCollection('example_data_1').find().limit(4)

4) Sort the query results-"sort()" command.
The value of the field is -1 for reverse order, and 1 for positive order.
sort({'field name': -1 or 1}) sort
all data with "age" greater than 21 in reverse order by "age". The query
sentence is as follows:
.find({'age': {'$gt':21}}).sort({'age': -1})

Modify deduplication data

The modification operation is also the update operation, and the corresponding MongoDB commands
are "updateOne()" and "updateMany()".
The command used in the deduplication operation is "distinct()". The format is as follows:
db.getCollection('example_data_1').distinct('field name', the first dictionary of the query statement)


De-duplicate the "age" field of records with "age" greater than or equal to 24. Distinct('age',{'age':{'$gte':24}})

mongodb introduction

MongoDB can be used to store large amounts of data, and the fields and formats can be changed at will.
SQL and MongoDB terminology comparison:

Insert picture description here


MongoDB[1] is a database based on distributed file storage. Written by C language. Designed to provide scalable high-performance data storage solutions for WEB applications.