Basic operation and daily maintenance of MongoDB

Source: Internet
Author: User
Tags auth mongodb mongodb commands mongodb monitoring mongodb server numeric value

MongoDB Basic Operations

MongoDB basically operates on databases, collections, and documents, including creating databases, deleting databases, inserting documents, changing documents, deleting documents, and querying documents.

Operation Description
Show DBS View the list of databases under the current instance
Show Users Show Users
Use <db_name> Toggle the current database
Db.help () Show Database Operations Command
Show.collections Displays the collection in the current database
Db.foo.help () Show collection Operations Command, Foo is a collection under the current database
Db.foo.find () Data lookup for the Foo collection in the current database
MongoDB data type
    • MongoDB supports a list of many data types given below:
    • String: This is the most commonly used data type to store data. The string in MongoDB must be a valid UTF-8.
    • Integer: This type is used to store a numeric value. The integer can be either 32-bit or 64-bit, depending on your server.
    • Boolean: This type is used to store a Boolean value (True/false).
    • Double: This type is used to store floating-point values.
    • Min/max keys: This type is used to compare the lowest and highest values of the Bson element.
    • Arrays: Use this type of array or list or multiple values to store to a key.
    • Timestamp: Timestamp. This makes it easy to record when the file has been modified or added.
    • Object: This data type is used for embedded files.
    • null: This type is used to store a null value.
    • Symbol: This data type is used for the same string, but it is usually used for languages reserved for a particular symbol type.
    • Date: This data type is used to store the UNIX time format for the current date or time. You can specify your own date and time, date and year, month, day to create an object.
    • Object ID: This data type is used to store the ID of the document.
    • binary data: This data type is used to store the binaries.
    • Code: This data type is used to store JavaScript code in the document.
    • Regular expression: This data type is used to store the regular table
#创建数据库testdb数据库,使用以下语句mongos> use testdb;#查询数据库,要显示数据库必须插入至少一条文档mongos> show dbs;#插入数据文档mongos> db.tablename.insert({"name":"antian"});#数据库生成了mongos> show dbs;testdb  0.078GB#查询数据库mongos> show dbs;testdb  0.078GB#进入数据库mongos> use testdb;#删除数据库mongos> db.dropDatabase();{ "dropped" : "testdb", "ok" : 1 }#查询数据库mongos> show dbs;
#创建集合#进入数据库mongos> use testdb;#创建集合mongos> db.createCollection("mycollection")mongos> show tables;mycollection#删除集合#进入数据库mongos> use testdb;mongos> show tables;mycollectionmongos> db.mycollection.drop();truemongos> show tables;
#插入文档#插入一条文档mongos> db.tablesname.insert([{"name":"aaaaa","age":"18"}#插入两条文档mongos> db.tablesname.insert([{"name":"ddddd","age":"18"},{"name":"eeee","age":"10"}]);#查询一个文档:mongos> db.tablesname.findOne();
MongoDB Daily Maintenance one. Import and Export

The mongoimport command can import the contents of a particular format file into the specified collection. The tool can import JSON-formatted data or import data in CSV format.
The mongoexport command can export a collection to a file in JSON or CSV format. You can specify the exported data item by parameter, or you can export the data based on the specified criteria.
Parameter description:

    • -D: Indicates the name of the database
    • -C: Indicates the name of the collection
    • -F: Indicates which columns to export
    • -O: Indicates the file name to export
    • -Q: Indicates the filtering criteria for the exported data
for(var i=1;i<=100;i++)db.info.insert({"id":i,"name":"jack"+i})    //循环写入100条数据mongoexport -d school -c info -o /opt/info.json   //导出mongoimport -d school -c info1 --file /opt/info.json  //导入到info集合mongoexport -d school -c info1 -q ‘{"id":{"$eq":10}}‘ -o /opt/top10.json    //条件导出指定第10行
Two. Backup and recovery

Backup: Mongodump
Recovery: Mongorestore

Parameter description:

    • -h:mongodb server Address
    • -D: DB instance that needs to be backed up
    • -O: The location of the backed up data, the directory needs to be established in advance, after the backup is completed, the system automatically establishes a test directory under the Dump directory, which contains the backup data of the database instance.
mkdir /backup   //创建存放目录mongodump -d abc -o /backup/       //备份abc数据库mongorestore -d abc123 --dir=/backup/abc    //恢复到abc123数据库
Three. Copying a database

Db.copydatabase

>db.copyDatabase("abc","abc1")  //复制数据库abc生成abc1
Four. Cloning a collection

RunCommand
Cloning an info collection from ABC to instance 2

mongo --port 27018      //进入实例2db.runCommand({"cloneCollection":"abc.info","from":"192.168.100.152:27017"})
Five. Authorization to start

You can configure authorized users to access MongoDB, you must specify Auth=true on startup, or the authorization will not work.
Users can be added to the role, and the built-in database user role includes: Read, ReadWrite, database management roles include: DbAdmin, Dbowner, useadmin, Superuser role root.

vim /usr/bin/mongodb1.conf......auth=true......mongo>use admin>db.createUser({"zx":"root","pwd":"123","roles":"[root"]})      //创建用户zx,密码为123,分配到root角色>db.auth("root","123")      //验证用户
Six. Process Management
    • The command to view the currently running process is:db.currentop ()
    • The process command to terminate a running high-consumption resource is:db.killop (opid)
Seven. MongoDB Monitoring
    • To view the status information for a DB instance:db.serverstatus ()
    • View statistics for the current database:db.status ()
    • View collection Statistics:db.users.stats ()
    • View collection Size:db.users.dataSize ()
    • You can also view system monitoring information from the Web interface by adding the following line to the configuration file:
      httpinterface=true

It is then accessed through the http://localhost:28017 in the browser. From the Web page you can see:

    • All connections to the current MongoDB
    • Access statistics for each database and collection, including: Reads, writes, Queries, Getmores, inserts, Updates, removes.
    • State of the Write lock
    • The last hundreds of lines of the log file.
    • All of the MongoDB commands.

Basic operation and daily maintenance of MongoDB

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.