MONGODB basic operations, backup restore and user management

Source: Internet
Author: User
Tags auth mongodb types of tables

Today while the weekend hollow, the recent learning of the MONGODB database commonly used commands to make the following collation, convenient work to view

The logical structure of MongoDB is mainly composed of three parts: document, collection and database. The document is the core concept of MongoDB, which is the smallest unit of the MongoDB logical storage, which is equivalent to a row of records in a relational database, a collection of multiple documents, a collection of tables in a relational database, and multiple collections to form a database.

SQL Terminology Description MongoDB Terminology Description
Database Database Database Database
Table Database tables Collection Collection
Row Recording Document Row fields
Column Field Field Domain
Index Index Index
Table joins Table Connection Not supported
Primary key Primary key Primary key Automatically set the _id field as the primary key

You can create multiple databases in a MongoDB, and the default database is test.

Default database:
  • Admin: From a permission point of view, this is the root database. Add a user to this database, the user will automatically inherit all the permissions of the database;
  • Local: This data is never copied and can be used to store any collection that is limited to a local single server;
  • Config: When MONGO is used for sharding settings, the Config database is used internally to hold information about the Shard.
MongoDB Login, exit
#本地登录(默认实例端口号为:--port=27017,可以不写)> mongo#登录远程主机的实例> mongo --host 192.168.1.2 --port =27017#退出MongoDB> exit
Database
#创建数据库(如果数据库不存在,则创建数据库,否则切换到指定数据库)> use school#查看所有数据库> show dbs#删除school数据库> use school> db.dropDatabase()#显示数据库操作命令> db.help()
Collection

A collection is a MongoDB document group, similar to a relational database management system in the table, the collection in the database, the collection does not have a fixed structure, the collection can be stored in different formats and types of tables.

#创建info集合> db.createcollection(‘info‘)#查看集合方法一:> show tabels方法二:> show colletctions#显示info集合操作命令> db.info.help()
Documents (Add, delete, change, check)

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

1. Inserting a document
#插入一条记录> db.info.insert({"id":1,"score":88,"address":"金川校区","hobby":["game","talk","sport"]})#向指定集合中插入一条文档数据> db.collection.insertOne()#向指定集合中插入多条文档数据> db.collection.insertMany()#通过循环批量插入数据> for(var i=1;i<100;i++)db.info.insert({"id":i,"name":"jack"+i})
2. Deleting a document
#删除info集合中id=1的文档> db.info.remove({"id":"1"})
3. Modify the Document
#修改info集合id=1的name值为"zhangsan"文档db.info.update({"id":"1"},{$set:{"name":"zhangsan"}})
4. Querying documents
#查询info集合所有文档> db.info.find()#查询info集合id为1的文档> db.info.findOne({id:1})#统计记录数> db.info.count()
Backing up and recovering a database
    1. Export import data via mongoexport and Mongoimport directories;
    2. The format of the exported data file is: JSON format or CSV format;
Parameter description:
    • -D: Name of the database
    • Name of the-c:collection
    • -F: Which columns to export
    • -O: File name to export
    • -Q: Filter criteria to export data
#备份本地school数据库 > [[email protected] ~]# mkdir/backup[[email protected] ~]# 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 (documen TS) #恢复本地school数据库至数据库abc中 > [[email protected] ~]# mongorestore-d ABC--dir=/backup/school2018-07-14t03 : 37:40.174-0400 the--db and--collection args should only being used when restoring from a BSON file. Other uses is deprecated and would not be exist in the future; Use--nsinclude instead2018-07-14t03:37:40.174-0400 building a list of collections to restore From/backup/school DIR20 18-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 RESTORE20 18-07-14t03:37:40.208-0400 finished restoring Abc.info (documents) 2018-07-14t03:37:40.209-0400 done# Export Native School database Info Collection &GT [[email protected] ~]# 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 records# import backup data to native School database user collection > [[email protected] ~]# 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 documents# Export native School Database User1 collection id=10 data > [[email protected] ~]# mongoexport-d Scho Ol-c user-q ' {"id": {"$lt": Ten}} '-o/backup/top10.json2018-07-14t03:51:23.968-0400 connected to:localhost2018-07-14t0 3:51:23.969-0400 exported 9 Records
Copy Database
> show dbs> db.copyDatabase("school","school_1")![image](https://note.youdao.com/favicon.ico)
Clone collection
#启用如下2个实例> [[email protected] ~]# netstat  -tunlp | grep mongodtcp        0      0 0.0.0.0:27017           0.0.0.0:*               LISTEN      61249/mongodtcp        0      0 0.0.0.0:27018           0.0.0.0:*               LISTEN      61212/mongod#登录端口号为27018的实例> mongo --port 27018#查询数据库> show dbsadmin   0.000GBconfig  0.000GBlocal   0.000GB#克隆端口号为27017实例的school数据库的info表至本实例数据库中> db.runCommand({"cloneCollection":"school.info","from":"192.168.100.100:27017"})![image](https://note.youdao.com/favicon.ico)
User Authorization (Authentication login)
#登录mongodbmongo#在admin数据库创建新用户root:123123> use admin> db.createUser({"user":"root","pwd":"123123","roles":["root"]})Successfully added user: { "user" : "root", "roles" : [ "root" ] }#退出> exit#关闭mongodb服务mongod -f /data/conf/mongodb1.conf --shutdown#带认证参数方式启动mongodb服务mongod -f /data/conf/mongodb1.conf --auth#登录mongodb数据库mongo#查询数据库show dbs> 不显示内容,这里要先授权认证后才能执行操作> use admin#使用授权root用户验证> db.auth("root":"123123")#再次查询,已经可以查询数据了> show dbsadmin   0.000GBconfig  0.000GBlocal   0.000GBschool  0.000GB#退出> exit
Process Management
1. 查看当前正在运行的进程的命令:db.currentOp() ------> 获取opid进程号2. 终止正在运行的高消耗资源的进程命令:db.killOP(opid)

For more detailed information, please refer to the MongoDB online Help documentation

MONGODB basic operations, backup restore and user management

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.