Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

MongoDB basic operation, backup and restore and user management

2025-02-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Take advantage of the free weekend today, I will sort out the following common commands in the MongoDB database that I have been learning recently, so that I can easily check them at work.

The logical structure of MongoDB mainly consists of three parts: document, collection and database. Among them, document is the core concept of MongoDB, it is the smallest unit of MongoDB logical storage, which is equivalent to a row of records in a relational database, multiple documents constitute a set, a set is equivalent to a table in a relational database, and multiple sets constitute a database.

SQL terminology description MongoDB terminology description database table database table collection collection row records document row field column field indexindex index table joins table join does not support primary key automatically sets the _ id field as the primary key

Multiple databases can be created in a MongoDB, and the default database is test.

Default database: admin: from a permission perspective, this is the root database. Add a user to this database and the user will automatically inherit the permissions of all databases; local: this data will never be replicated and can be used to store any collection limited to a single local server; config: when Mongo is used for sharding settings, the config database is used internally to store shard-related information. MongoDB login, exit # local login (default instance port number is:-- port=27017, can not be written) > mongo# login remote host instance > mongo-- host 192.168.1.2-- port=27017 # exit MongoDB > exit database # create a database (if the database does not exist, create a database Otherwise, switch to specified database) > use school# view all databases > show dbs# delete school database > use school > db.dropDatabase () # Show database operation command > db.help () collection

The collection is the MongoDB document group, which is similar to the table in the relational database management system, the collection is stored in the database, the collection has no fixed structure, so the collection can store different formats and types of tables.

# create info collection > db.createcollection ('info') # View collection method 1: > show tabels method 2: > show colletctions# display info collection operation command > db.info.help () document (add, delete, modify, check)

The document is a key-value pair (BSON), the same fields do not need to be set, and the same fields do not need the same data type.

1. Insert document # insert a record > db.info.insert ({"id": 1, "score": 88, "address": "Jinchuan Campus", "hobby": ["game", "talk", "sport"]}) # insert a piece of document data into the specified collection > db.collection.insertOne () # insert multiple document data into the specified collection > db.collection.insertMany () # insert data in batches through a loop > for (var item1) I db.info.remove ({"id": "1"}) 3. Modify document # modify the name value of the info collection id=1 to "zhangsan" document db.info.update ({"id": "1"}, {$set: {"name": "zhangsan"}}) 4. Query documents # query all documents in the info collection > db.info.find () # query documents in the info collection with id 1 > db.info.findOne ({id:1}) # Statistical records > db.info.count () backup and recovery databases export imported data through the mongoexport and mongoimport directories; the format of the exported data file is: JSON format or CSV format Parameter description:-d: name of database-name of c:collection-f: which columns to export-o: file name to export-Q: filter condition to export data # backup local school database > [root@localhost ~] # mkdir / backup [root@localhost ~] # mongodump-d school-o / backup/2018-07-14T03:36:44.427-0400 writing school.info to2018-07-14T03:36:44.429-0400 Done dumping school.info (99 documents) # restore local school database to database abc > [root@localhost ~] # mongorestore-d abc-- dir=/backup/school2018-07-14T03:37:40.174-0400 the-- db and-- collection args should only be used when restoring from a BSON file. Other uses are deprecated and will not exist in the future Use-- nsInclude instead2018-07-14T03:37:40.174-0400 building a list of collections to restore from / backup/school dir2018-07-14T03:37:40.175-0400 reading metadata for abc.info from / backup/school/info.metadata.json2018-07-14T03:37:40.187-0400 restoring abc.info from / backup/school/info.bson2018-07-14T03:37:40.208-0400 no indexes to restore2018-07-14T03:37:40.208-0400 finished restoring abc .info (99 documents) 2018-07-14T03:37:40.209-0400 done# exports native school database info collection > [root@localhost ~] # mongoexport-d school-c info-o / backup/info.json2018-07-14T03:44:41.610-0400 connected to: localhost2018-07-14T03:44:41.613-0400 exported 99 records# imports backup data into native school database user collection > [root@localhost ~] # mongoimport-d school-c User-- file / backup/info.json2018-07-14T03:45:09.300-0400 connected to: localhost2018-07-14T03:45:09.330-0400 imported 99 documents# exports data from the native school database user1 collection id=10 > [root@localhost ~] # mongoexport-d school-c user-Q'{"id": {"$lt": 10}'- o / backup/top10.json2018-07-14T03:51:23.968-0400 connected to: localhost2018-07- 14T03:51:23.969-0400 exported 9 records replication database > show dbs > db.copyDatabase ("school" "school_1")! [image] (https://note.youdao.com/favicon.ico) Clone Collection # enables the following two instances > [root@localhost ~] # netstat-tunlp | grep mongodtcp 0 0 0 image 27017 0 0 0 V * LISTEN 61249/mongodtcp 0 0 0 7 0 0 0 of the following two instances .0.0: * LISTEN 61212/mongod# login to the instance with port number 27018 > mongo-- port 270 query database > show dbsadmin 0.000GBconfig 0.000GBlocal 0.000GB# clone the info table of the school database with port number 27017 to this instance database > db.runCommand ({"cloneCollection": "school.info") "from": "192.168.100.100mongodbmongo# 27017"})! [image] (https://note.youdao.com/favicon.ico) user authorization (authentication login) # login mongodbmongo# to create a new user in the admin database root:123123 > use admin > db.createUser ({"user": "root", "pwd": "123123", "roles": ["root"]}) Successfully added user: {"user": "root" "roles": ["root"]} # exit > exit# closes mongodb service mongod-f / data/conf/mongodb1.conf-- shutdown# starts mongodb service with authentication parameters mongod-f / data/conf/mongodb1.conf-- auth# login mongodb database mongo# query database show dbs > does not display content Here you must authorize authentication before performing the operation > use admin# uses authorized root user authentication > db.auth ("root": "123123") # query again, you can query data > show dbsadmin 0.000GBconfig 0.000GBlocal 0.000GBschool 0.000GB# exit > exit process management 1. The command to view the currently running process: db.currentOp ()-> get the opid process number 2. The command to terminate a running resource-consuming process: db.killOP (opid)

For more details, please refer to the MongoDB online help documentation

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report