Mongodb selection 01 episode (cognition, installation, introductory case)

MongoDB introduction and installation

mongodb documentation: https://docs.mongodb.com/

1. Introduction to mongodb

1.1 What is mongodb

  • mongodb is the most feature-rich NoSQL non-relational database. Written in C++ language.
  • Mongodb itself provides S-side storage data, which is server; it also provides C-side operation processing (such as query, etc.) data, which is client.

1.2 The main difference between SQL and NoSQL

  • Hierarchical relationship in SQL: database>table>data
  • In NoSQL, it is: Database>Collection>Document

1.3 No correlation between data

  • How to add external related data in SQL, the standardized approach is to add a foreign key to the original table to associate the external data table.
  • NoSQL can put external data directly into the original data set to improve query efficiency. The disadvantages are also obvious, and it will be more troublesome to update the associated data.
  • In SQL, the field of each data in a table is fixed. The key (field) of each document (data) in a collection (table) in NoSQL can be different from each other.

1.4 Further reading

https://www.cnblogs.com/jeakeven/p/5402095.html

1.5 Advantages of mongodb as a non-relational database over relational databases

Easy to expand: There are many types of NoSQL databases, but one common feature is to remove the relational characteristics of relational databases. There is no relationship between the data, so it is very easy to expand

Large data volume, high performance: NoSQL databases have very high read and write performance, especially under large data volumes. This is due to its non-relational nature and the simple structure of the database

Flexible data model: NoSQL does not need to create fields for the data to be stored in advance, and can store custom data formats at any time. In relational databases, adding and deleting fields is a very troublesome thing. If it is a very large data volume table, adding fields is simply a nightmare

2. The installation of mongodb

Take ubuntu18.04 as an example

mongodb has two installation methods: command installation or source installation

2.1 Command installation

Install using apt-get tool in ubuntu

sudo apt-get install -y mongodb-org

Or refer to the official documentation https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/

2.2 Source code installation

2.2.1 Select the corresponding version and operating system and download

https://www.mongodb.com/download-center/community?jmp=docs

2.2.2 Unzip

tar -zxvf mongodb-linux-x86_64-ubuntu1804-4.0.3.tgz

2.2.3 Move to the /usr/local/ directory

sudo mv -r mongodb-linux-x86_64-ubuntu1804-4.0.3/ /usr/local/mongodb

2.2.4 Add the mongodb executable file to the environment variable PATH in the initialization script .bashrc of the shell

a. Enter the .bashrc file

cd ~
sudo vi .bashrc

b. Add at the end of the .bashrc file:

export PATH=/usr/local/mongodb/bin:$PATH

3. The official documentation of mongodb

https://docs.mongodb.com/manual/introduction/

4. Getting Started Case

4.1 Start of mongodb server

  • Default port: 27017
  • The location of the default configuration file: /etc/mongod.conf
  • The location of the default log: /var/log/mongodb/mongod.log

There are two ways to start the mongodb server:

  • Start the local test mode (only with the function of adding, deleting, modifying and checking local data)
  • Start the production environment (with complete and full functions)

4.2 Start of test mode

  • 启动: sudo service mongod start (sudo service mongod start)
  • Stop: sudo service mongod stop
  • Restart: sudo service mongod restart

4.3 Formal startup method of production environment

启动: sudo mongod [–auth --dbpath=dbpath --logpath=logpath --append --fork] [-–f logfile ]

  • When only started with the sudo mongod command, the data is stored in the /data/db directory by default and needs to be created manually
  • --Dbpath: specify the storage path of the database
  • --Logpath: Specify the storage path of the log
  • --Append: or --logappend set the log write format to append mode
  • --Fork: or -fork to start a new process to run mongodb service
  • --F: or -f configuration file path (you can write the above configuration information into a file and then load and start it through the parameters in the file)
  • --Auth: start with authorization authentication, we will learn this content in later courses

4.4 Check whether the startup is successful

ps to | grep mongod

4.5 Start the mongodb client: enter the mongo shell

  • Start the local client: mongo
  • View help: mongo –help
  • Exit: exit or ctrl+c

4.6 Simple use of mongodb

When the mongodb server is turned on, you can simply use it after entering the mongo shell

4.7 Commands of mongodb database

  • View the current database: db (the test database is used by default if the database is not switched)
  • View all databases: show dbs /show databases
  • Switch database: use db_name
    • db_name is the database name returned after show dbs
  • Delete the current database: db.dropDatabase()

4.8 Commands for mongodb collection

  • No need to manually create a collection:
    the first time you add data to a non-existent collection, the collection will be created automatically
  • Manually create a collection:
    • db.createCollection(name,options)
    • db.createCollection(“stu”)
    • db.createCollection(“sub”, { capped : true, size : 10 } )
    • Parameter capped: the default value is false means no upper limit is set, and a value of true means upper limit is set
    • Parameter size: the number of bytes occupied by the collection. When the capped value is true, you need to specify this parameter to indicate the upper limit size. When the document reaches the upper limit, the previous data will be overwritten in bytes
  • View collections: show collections
  • Drop collection: db.collection name.drop()
  • Check whether the collection has an upper limit: db.collection name.isCapped()

6. Simple exercises

Enter the following commands in the mongo shell to view the results

show dbs
use test
show collections
db
db.stu.insert({'name':'郭靖', 'age':22})
show dbs
show collections
db.stu.find()
db.stu.drop()
show collections
db.dropDatabase()
show dbs
exit

6.1 Common data types in mongodb (understand)

Common type

  • Object ID: document ID/data ID, primary key of data
  • String: String, most commonly used, must be valid UTF-8
  • Boolean: store a Boolean value, true or false
  • Integer: Integer can be 32-bit or 64-bit, depending on the server
  • Double: floating point number
  • Arrays: array/list
  • Object: A piece of data/document in mongodb, that is, document nested document
  • Null: store the null value
  • Timestamp: Timestamp, representing the total number of seconds from 1970-1-1 to the present
  • Date: UNIX time format for storing the current date or time

6.2 Precautions

  • Each document has an attribute, _id, to ensure the uniqueness of each document, mongodb uses _id as the primary key by default

    • You can manually set the value of _id. If it is not provided, then MongoDB provides a unique _id for each document, the type is objectID
  • The objectID is a 12-byte hexadecimal number, with two bits per byte, a total of 24 character strings:

    • The first 4 bytes are the current timestamp
    • The machine ID of the next 3 bytes
    • MongoDB service process id in the next 2 bytes
    • The last 3 bytes are simple increment values

Guess you like

Origin blog.csdn.net/weixin_38640052/article/details/108356255