Get the App
SLTechnology News&Howtos  ›  Database  › 

Introduction to NoSQL (7)

Shulou Source: shulou.com Published: 2022-06-01 02:52:53 09月15日 Update

Introduction to NoSQL (7)

MongoDB introduction

The official website www.mongodb.comc++, based on distributed, belongs to NoSQL. In NoSQL, MongoDB, which is most like a relational database, stores the data as a document, and the data structure is composed of key-value pairs (key= > value). MongoDB documents are similar to JSON objects. Field values can contain other documents, arrays, and document arrays. Because it is distributed, it is easy to extend

Comparison between MongoDB and Relational Database

SQL terminology concept MongoDB term concept explanation databasedatabase database tablecollection database table / collection rowdocument data record row / document columnfiled data field / domain indexindex index table joins table join MongoDB does not support primary keyprimary key primary key MongoDB automatically sets the _ id field as the primary key

MongoDB installation

Official installation documentation https://docs.mongodb.com/manual/tutorial/install-mongodb-on-red-hat/

Vim / etc/yum.repos.d/mongodb-org-3.6.repo [mongodb-org-3.6] name=MongoDB Repositorybaseurl= https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.6/x86_64/gpgcheck=1enabled=1gpgkey=https://www.mongodb.org/static/pgp/server-3.6.ascyum list | grep mongodbyum install-y mongodb-org

Connection of MongoDB

Systemctl start mongod.servicenetstat-tlnp | grep mongodtcp 0 192.168.221.10 grep mongodtcp 27017 0.0.0.0 LISTEN 1999/mongod tcp 0 0127.0.0.1 grep mongodtcp 27017 0.0.0.0 LISTEN 1999/mongodmongo-- port 27017-- host 192.168.221.10mongo-uusername-ppasswd-- authenticationDatabase db

MongoDB user Management

Mongo-- port 27017-- host 192.168.221.10 > use admin / / switch to admin Library > db.createUser ({user: "admin", customData: {description: "superuser"}, pwd: "admin", roles: [{role: "root", db: "admin"}]}); > db.system.users.find (); > show users; > db.createUser ({user: "zs", pwd: "zs", roles: [{role: "read", db: "testdb"]}) / / create zs user > db.dropUser ('zs'); / / delete user zs

To connect to the mongo database with a user name and password, you need to modify the startup script and restart

Vim / usr/lig/systemd/system/mongod.service / / add "--auth" Environment= "OPTIONS=--auth-f / etc/mongod.conf" systemctl daemon-reloadsystemctl restart mongod.servicemongo-u 'admin'-p' admin'-- authenticationDatabase 'admin' / / need to specify a database after "database"

Create a user test1 in the database db1 to read and write to the db1 library and read only to the db2 library.

> use db1; > db.createUser ({user: "test1", pwd: "test1", roles: [{role: "readWrite", db: "db1"}, {role: "read", db: "db2"}]}); ctrl+dmongo-u 'test1'-p' test1'-- authenticationDatabase 'db1' > use db2; > db.auth ("test1", "test1"); Error: Authentication failed. / / an error is reported because the user test1 is created in db1

MongoDB user role

Read: allows the user to read the specified database readWrite: allows the user to read and write the specified database dbAdmin: allows the user to perform management functions in the specified database, such as index creation, deletion, viewing statistics or accessing system.profileuserAdmin: allows the user to write to the system.users collection, and can find the specified database to create, delete and manage the user clusterAdmin: only available in the admin database, giving the user administrative rights to all shard and replication set related functions. ReadAnyDatabase: available only in admin database, give user read permission to all databases readWriteAnyDatabase: available only in admin database, give user read and write permission to all databases userAdminAnyDatabase: available only in admin database, give user userAdmin permission to all databases dbAdminDatabase: available only in admin database, give user dbAdmin permission to all databases root: only available in admin data. Super account, super privilege

MongoDB creates a collection

/ / db.createCollection (name,options); name is the name of the collection. Optional iptions is used to configure the parameters of the collection. The parameters are as follows: capped true/false: if true, the capped collection is enabled. A capped collection is a collection of fixed size, and when it reaches its maximum size, it automatically overwrites the earliest entries. If you specify true, you also need to specify size parameters. Size (optional) specifies the maximum size byte capped collection. If the capping is true, you also need to specify this field max to specify the maximum number of files allowed in the capped collection. > db.createCollection ('mycol', {capped:true,size:6142800,max:10000})

View the collection

> show tables; or show collections

Create a collection Account and insert content

> db.Account.insert ({AccountID:2,UserName:'lisi',password:'lisi'})

View all the contents and conditional queries of the collection Account

> db.Account.find (); > db.Account.find ({AccountID:1})

Delete a record in the Account collection according to the condition

> db.Account.remove ({AccountID:1})

Print collection status

> db.printCollectionStats ()

Modify a record in the collection

> db.Account.update ({AccountID:2}, {"$set": {age:20}})

Delete a collection

> db.Account.drop ()

Tags: Data databases users documents permissions capping fields management maximum parameters size content functions distributed arrays terms conditions concepts indexes medium creation Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Shulou Technology Huawei MariaDB MySQL