Yum installs MongoDB and database management

Source: Internet
Author: User
Tags findone

About MongoDB

MongoDB is written by the C + + language and is an open source database system based on distributed file storage. In the case of high load, adding more nodes can guarantee the performance of the server. MongoDB is designed to provide scalable, high-performance data storage solutions for Web applications. MongoDB stores data as a document and data structures consist of key-value (key=>value) pairs. A MongoDB document is similar to a JSON object. Field values can contain other documents, arrays, and array of documents.

Main features of MongoDB
  • MongoDB is a document-oriented database that is simple and easy to operate.
  • Set the index of any property in a MongoDB record for faster sorting
  • Create data mirroring locally or on the network to make MongoDB more scalable
  • If the load increases (requiring more storage space and greater processing power), it can be distributed across other nodes in the computer network. This is called sharding.
  • The MONGO supports rich query expressions. Query directives use JSON-style markup to easily query objects and arrays embedded in the document
  • MongoDb uses the update () command to implement a replacement of the completed document (data) or some specified data fields
  • The map/reduce in MongoDB is primarily used for batch processing and aggregation operations of data
  • Map and reduce. The map function calls emit (Key,value) to traverse all records in the collection, passing key and value to the reduce function for processing
  • The map function and the reduce function are written in JavaScript and can be executed with the Db.runcommand or MapReduce command.
  • Gridfs is a built-in feature in MongoDB that can be used to store a large number of small files
  • MongoDB allows you to execute scripts on the server, write a function in JavaScript, execute directly on the server, or store the definition of the function on the server, the next time you call directly
  • MongoDB supports a variety of programming languages: ruby,python,java,c++,php,c# and many other languages
  • MongoDB Easy to install
MongoDB installation configuration Yum source repository
cd /etc/yum.repos.d/vim mongodb-org.repo//创建yum仓库,写入如下内容[mongodb-org]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.asc
Yum Installation
yum  install -y mongodb-org
Configuration file Modification

Configuration file modifications including path, DBPath, port and Bindip, if only in the local use, completely can not make changes directly to use, but also can simply modify its listening address (BINDIP), for other address use to provide convenience.

Shutting down firewalls and SELinux
systemctl disable firewalld.servicesystemctl stop firewalld.servicesetenforce 0
Open Database
systemctl start mongod.servicenetstat -anpt | grep mongod

Enter the database
mongo                 //进入数据库db.version()          //查看数据库版本show dbs              //查看所有库db.getMongo()         //查看当前数据库机器的连接地址

Open multi-instance create Path,dbpath directory
mkdir -p  /data/mongodb/mkdir /data/mongodb/mongotouch /data/mongodb/mongod2.logchmod 777 /data/mongodb/mongod2.log
Copy and modify profile configuration files
path: /data/mongodb/mongod.logdbPath: /data/mongodb/mongoport: 27018bindIp: 0.0.0.0

Start a new instance
mongod -f /etc/mongod2.conf

Entering the new instance database
mongo --port 27018netstat -anpt | grep mongod

Database Management database Basic operations

The library created in MongoDB is closely linked to the table, and to create the library, you need only use it, and when you do not create a collection (table) in the library, the library does not exist, and when you create the collection, the library also exists accordingly.

> use python    //若不存在则会自动创建python库,若是不创建集合,又会自行删除> db.createCollection(‘a‘)          //创建名称为a的集合> db.a.insert({“name”:“zhangsan”,"score":90,"hobby":["game","talk","sport"]})//集合中插入数据;字符串使用双引号;数值不加双引号;字符串数组使用[]括起来> db.users.update()             //更改数据> show collections               //查看集合> db.a.drop()                       //删除集合> db.dropDatabase()          //删除数据库


Example Demo
> Use Stady//using library switched to DB stady> db.createcollection (' school ')//Create School collection {"OK": 1}&gt ;                                  Db.school.insert ({"id": 1, "name": "Lisi", "Score": 90})//Insert data into the collection Writeresult ({"ninserted": 1}) > Db.school.find ()  View the data in the collection {"_id": ObjectId ("5b4843900edf47ef3aa006f3"), "id": 1, "name": "Lisi", "score": }> for (var i=2;i<=5;i++) Db.school.insert ({"id": I, "name": "Tom" +i})//Insert multiple data into the collection Writeresult ({"ninserted": 1} ) > Db.school.find ()//view collection data {"_id": ObjectId ("5b4843900edf47ef3aa006f3"), "id": 1, "name": "Lisi", " Score ": _id {": ObjectId ("5b4844e70edf47ef3aa006f4"), "id": 2, "name": "Tom2"} {"_id": ObjectId ("5b4844e70edf47 Ef3aa006f5 ")," id ": 3," name ":" Tom3 "} {" _id ": ObjectId (" 5b4844e70edf47ef3aa006f6 ")," id ": 4," name ":" TOM4 "} {" _id ": ObjectId (" 5b4844e70edf47ef3aa006f7 ")," id ": 5," name ":" TOM5 "}> Db.school.findOne ({" id ": 3})//View the third data in the collection {" _id ": ObjectId (" 5b4844e70Edf47ef3aa006f5 ")," id ": 3," name ":" Tom3 "}> A=db.school.findone ({" id ": 3})//View the third record and assign it an alias a{" _id ": ObjectId (" 5b4844e70edf47ef3aa006f5 ")," id ": 3," name ":" Tom3 "}> typeof (a.ID)//view attribute type number//attribute number Word > typeof (A.name)//view attribute type string//property is String > db.school.update ({"id": 3},{$set: {"name": "Jack"} })//Modify data Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1}) > Db.school.findOne ({"id": 3}) {"_id": O  Bjectid ("5b4844e70edf47ef3aa006f5"), "id": 3, "name": "Jack"}> Db.school.count ()//statistics collection with how many records 5> Db.createcollection (' tea ')//Create new collection {"OK": 1}> Show tables//view collection in library schooltea> Db.te A.drop ()//delete collection true> show tables school//collection deleted &G T Show DBS//View Library admin 0.000GBconfig 0.000GBlocal 0.000GBpython 0.000GBstady 0.000gb> US e pythonswitched to Db python> db.dropdatabase ()//delete Python database, first must be advanced database, in select Delete {"Dropped": "Python", "OK": 1}> Show D  Bsadmin 0.000GBconfig 0.000GBlocal 0.000GBstady 0.000gb>
Import and export of data
    • Exporting data in a library
      [[email protected] mongodb]#mongoexport -d stady -c school -o /opt/school.json   //导出stady库中的school集合到/opt下,命名为school.json 注意:文件名一定是.json结尾,要在linux模式操作

    • Import data into the library
      [[email protected] mongodb]#mongoimport -d stady -c school2 --file /opt/school.json

    • Conditional filtering Export
      [[email protected] mongodb]# mongoexport -d stady -c school -q ‘{"id":{"$eq":3}}‘ -o /opt/top3.json

Backing Up the database
mkdir /backup     //创建备份文件夹mongodump -d stady -o /backup/

Recovering a Database
mongorestore -d chen --dir=/backup/stady

Yum installs MongoDB and database 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.