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

Build MongoDB server, basic use of MongoDB, import and export of data

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

Share

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

MongoDB

1.1 Software introduction

Products between relational and non-relational databases

-A database based on distributed file storage.

-written in C++ language. Designed to provide scalable high-performance data storage solutions for WEB applications.

-MongoDB stores the data as a document, and the data structure consists of key-value (key= > value) pairs.

-MongoDB documents are similar to JSON objects. Field values can contain other documents, arrays, and document arrays.

1.2 Software Features

-easy to install

-for document storage, the operation is simple and easy

-support rich query expression

-you can set the index of any property

-supports mainstream programming languages RUBY | PYTHON | JAVA | PHP | C++

-support for replica sets, fragmentation

Set up MongoDB server

2.1 package

[root@50] # tar-xf mongodb-linux-x86_64-rhel70-3.6.3.tgz

[root@50] # ls mongodb-linux-x86_64-rhel70-3.6.3

[root@50 bin] # mkdir / usr/local/mongodb

[root@50 ~] # mv mongodb-linux-x86_64-rhel70-3.6.3/bin / usr/local/mongodb/bin

[root@50 ~] # cd / usr/local/mongodb/bin

[root@50 mongodb] # mkdir etc log # etc configuration file log log file

[root@50 mongodb] # mkdir-p data/db

2.2 create a profile

[root@50 bin] #. / mongod-- help View help

[root@50 etc] # cd / usr/local/mongodb/etc/

[root@50 etc] # vim mongodb.conf

Logpath=/usr/local/mongodb/log/mongodb.log

Logappend=true # record log information by appending

Dbpath=/usr/local/mongodb/data/db # database directory

Fork=true # daemon mode

Bind_ip=192.168.4.50

Port=27050

2.3 Mobile Services

[root@50 mongodb] # ln-s / usr/local/mongodb/bin/* / sbin/

[root@50] # mongod-f / usr/local/mongodb/etc/mongodb.conf # start the service

[root@50 mongodb] # mongod-- shutdown-f / usr/local/mongodb/etc/mongodb.conf # related service

2.3.1 View Progress

[root@50 log] # ps-C mongod

PID TTY TIME CMD

2432? 00:00:00 mongod

2.3.2 View Port

[root@50 log] # netstat-antulp | grep: 27017

Tcp 0 0 127.0.0.1 27017 0.0.0.0 * LISTEN 2432/mongod

2.3.3 check to see if there are log files

[root@50 log] # ls

Mongodb.log

2.4 pick up service

[root@50 log] # mongo

MongoDB shell version v3.6.3

Connecting to: mongodb://127.0.0.1:27017

MongoDB server version: 3.6.3

.

2018-07-07T10:49:21.013+0800 I CONTROL [initandlisten] * * We suggest setting it to 'never'

2018-07-07T10:49:21.013+0800 I CONTROL [initandlisten]

Show dbs / / Show existing libraries

Admin 0.000GB

Config 0.000GB

Local 0.000GB

Db / display shows the front database

Test

Exit # disconnect

Bye

Basic use of MongoDB

3.1 specify the service IP address

[root@50 log] # killall-9 mongod

[root@50 mongodb] # vim etc/mongodb.conf

Bind_ip=192.168.4.50 change ip

Port=27050 change Port

~

[root@50 mongodb] # mongod-f / usr/local/mongodb/etc/mongodb.conf

[root@50 mongodb] # netstat-antulp | grep: 27050

Tcp 0 0 192.168.4.50 27050 0.0.0.0 * LISTEN 2794/mongod

[root@50 mongodb] # mongo-host 192.168.4.50-port 27050

3.2 create an alias to facilitate the relationship

[root@50 ~] # vim / root/.bashrc

Alias mstart='mongod-f / usr/local/mongodb/etc/mongodb.conf'

Alias mstop='mongod-- shutdown-f / usr/local/mongodb/etc/mongodb.conf'

3.3 Common management commands

-show dbs to view existing libraries

-db displays the current library

-use library name switch library, if the library does not exist, automatically delay the creation of the library

-show collections or show tables to view existing collections under the library

-db.dropDatabase () deletes the current library

3.4 Database naming rules

The database name can be any UTF-8 string that meets the following conditions.

-cannot be an empty string (").

-must not contain''(space),. , $, /,\ and\ 0 (null characters)

-it should be all lowercase.

-up to 64 bytes.

3.5 Collection Management commands: view creation and deletion

-show collections or show tables # View collections

-db. Collection name .drop () # divide the collection

-db. Collection name .save ({','}) # # create the collection, but the collection does not exist

# create and add documents at the time

Db.user.save ({'name':'bob','age':'21'})

WriteResult ({"nInserted": 1})

Use studb

Switched to db studb

Db

Studb

Show tables

Db.c1.save ({name: "bob", age:19,sex: "boy"})

WriteResult ({"nInserted": 1})

Db.c1.save ({name: "harry", age:20,sex: "girl"})

WriteResult ({"nInserted": 1})

Db.c1.find

Show tables

C1

Basic document management

Documentation: similar to records in the MySQL table

4.1 basic document management

Document management commands: viewing statistics, adding and deleting

-db. Collection name. Find ()

> db.c1.find ()

-db. Collection name .count ()

> db.c1.count () 2

-db. Collection name .insert ({"name": "jim"})

> db.c1.insert ({"name": "jim", "age": 11, "sex": "boy"}) WriteResult ({"nInserted": 1})

-db. Collection name .find ({condition})

> db.c1.find ({"age": 11}) {"_ id": ObjectId ("5b405853f250badccca5adb8"), "name": "jim", "age": 11, "sex": "boy"}

-db. Collection name .findOne () # returns a document

> db.c1.findOne () {"_ id": ObjectId ("5b403a1af250badccca5adb6"), "name": "bob", "age": 19, "sex": "boy"

}

-db. Collection name .remove ({}) # Delete all documents

> db.c1.remove ({})

-db. Collection name .remove ({condition}) # Delete all documents that match the condition

Db.c1.remove ({"name": "bob"})

WriteResult ({"nRemoved": 1})

Basic data type

5.1 character string/ Boolean bool/ empty null

String string

-UTF-8 strings can be represented as data of string type

-{name: "Zhang San"} or {school: "tarena"}

Boolean bool

-Boolean type has two values true and false, {x:true}

Db.c1.save ({name: "tom", age:19,sigle:true})

Empty null

-used to indicate fields that are null or do not exist, {x:null}

Db.c1.save ({name: "lucy", age:18,sigle:false,pay:null})

5.2Numeric / Array array

5.2.1 numerical value

-shell defaults to 64 as a floating-point value. {x: 3.14} or {x: 3}.

> db.c1.save ({name: "yaya", XRV 3.99})

-NumberInt (4-byte integer) {x:NumberInt (3)}

Db.c1.save ({"name": "zhangsan", x:NumberInt (3)})

-NumberLong (8-byte integer) {x:NumberLong (3)}

Db.c1.save ({name: "yaya", x:NumberLong (3)})

Db.c1.find ()

{"_ id": ObjectId ("5b403a4bf250badccca5adb7"), "name": "harry", "age": 20, "sex": "girl"}

{"_ id": ObjectId ("5b405853f250badccca5adb8"), "name": "jim", "age": 11, "sex": "boy"}

{"_ id": ObjectId ("5b405e31f250badccca5adb9"), "name": "zhangsan", "x": 3}

{"_ id": ObjectId ("5b405ec1f250badccca5adba"), "name": "tom", "age": 19, "sigle": true}

{"_ id": ObjectId ("5b405f36f250badccca5adbb"), "name": "lucy", "age": 18, "sigle": false, "pay": null}

{"_ id": ObjectId ("5b405f9bf250badccca5adbc"), "name": "yaya", "x": 3.99}

{"_ id": ObjectId ("5b405fdef250badccca5adbd"), "name": "yaya", "x": NumberLong (3)}

5.2.2 Array array

-A data list or dataset can be represented as an array

-{x: ["a", "b", "c"]}

5.3 Code / date / object

Code

-any JavaScript code can be included in queries and documents

-{x: function () {/ Code /}}

Date

-the date is stored as the number of milliseconds that have elapsed since the new era, without storing the time zone

-{x:new Date ()}

Db.c1.save ({name: "liwei", birthday:new Date ()})

Object

-the object id is a 12-byte string that is the unique identifier of the document

-{x: ObjectId ()}

Db.c1.save ({name: "xiaodong", stuid:ObjectId ()})

5.4 embedded / regular expressions

5.4.1 embedded

-documents can be nested within other documents, and nested documents can be treated as values

-{tarena: {address: "Beijing", tel: "888888", perso

N: "hanshaoyun"

-}}

Db.c1.save ({

... Ywzd: {p: "dmy", jg:69,v:2}

... Ngsfc: {p: "birdg", jg:89,v:4}

.)

5.4.2 regular expression

-when querying, use regular expressions as qualification

-{xPlus / regular expression /}

Db.c1.save ({

... Name: "hanmm", match:/ ^ a /

.)

Data import and export

6.1 data export

Syntax format 1

Mongoexport [--host IP address-- port port]

D library name-c collection name-f field name 1, field name 2

-- type=csv > directory name / file name .csv

[root@50 mnt] # mongoexport-- host 192.168.4.50-- port 27050-d studb-c C1-f _ id,name-- type=csv > / mnt/c1.csv

2018-07-07T16:31:22.526+0800 connected to: 192.168.4.50 purl 27050

2018-07-07T16:31:22.527+0800 exported 7 records

Syntax format 2

-# mongoexport-- host IP address-- port port

Library name-c collection name-Q'{condition}'- f field name 1, field name 2

-- type=csv > directory name / file name .csv

* Note: exporting to csv format must use-f to specify the list of field names!

Syntax format 3

Mongoexport [--host IP address-- port port]

-d library name-c collection name [- Q'{condition}'- f field list

]

-- type=json

Directory name / file name .json

[root@50 mnt] # mongoexport-- host 192.168.4.50-- port 27050-d studb-c C1-- type=json > / mnt/c1.json

2018-07-07T15:50:46.202+0800 connected to: 192.168.4.50 purl 27050

2018-07-07T15:50:46.203+0800 exported 7 records

6.2 data Import

Syntax format 1

-# mongoimport-host IP address-port port

-d library name-c collection name

-- type=json

Directory name / file name .json

[root@50 mnt] # mongoimport-- host 192.168.4.50-- port 27050-d bbsdb-c T1-- type=json / mnt/c1.json

2018-07-07T16:20:39.777+0800 connected to: 192.168.4.50 purl 27050

2018-07-07T16:20:40.115+0800 imported 7 documents

[root@50 mnt] # mongoimport-- host 192.168.4.50-- port 27050-d bbsdb-c T2-f _ id,name-- type=csv / mnt/c1.csv

2018-07-07T16:43:37.797+0800 connected to: 192.168.4.50 purl 27050

2018-07-07T16:43:37.953+0800 imported 8 documents

Syntax format 2

-# mongoimport-host IP address-port port

-d library name-c collection name

-- type=csv-- headerline [--drop] directory name / file name .csv

Note: when the library and collection do not exist when importing data, the library and collection will be created and then the data will be imported. Otherwise, the data will be imported into the collection in an appended way. Use the-drop option to delete the original data and import the new data-headerline ignores the title

[root@50 mnt] # mongoimport-- host 192.168.4.50-- port 27050-d studb-c C1-- headerline-- drop-- type=csv / mnt/c1.csv

2018-07-07T17:00:41.677+0800 connected to: 192.168.4.50 purl 27050

2018-07-07T17:00:41.678+0800 dropping: studb.c1

2018-07-07T17:00:41.967+0800 imported 7 documents

Input system usage information into mongodb

Create a new file

Db.c3.save ({name: "yaya", pass: "x", uid: "123", gid: "123", comment: "teacher", homeaddr: "/ home/bin", shell: "/ bin/bash"})

And then lead out

[root@50 mnt] # mongoexport-- host 192.168.4.50-- port 27050-d bbsdb-c c3-f name,pass,uid,gid,comment,homeaddr,shell-- type=csv > / mnt/c3.csv

2018-07-07T17:34:29.526+0800 connected to: 192.168.4.50 purl 27050

2018-07-07T17:34:29.527+0800 exported 1 record

[root@50 mnt] # cp / etc/passwd.

[root@50 mnt] # ls

C1.csv c1.json c3.csv passwd

[root@50 mnt] # sed-I''c3.csv

[root@50 mnt] # sed-I'$r passwd' c3.csv

[root@50 mnt] # vim c3.csv

[root@50 mnt] # sed-I's Gunther c3.csv

[root@50 mnt] # vim c3.csv

Log c3.csv d into the database

[root@50 mnt] # mongoimport-host 192.168.4.50-port 27050\

-d studb-c c3-- headerline-- drop-- type=csv / mnt/c3.csv

2018-07-07T17:45:47.963+0800 connected to: 192.168.4.50 purl 27050

2018-07-07T17:45:47.964+0800 dropping: studb.c3

2018-07-07T17:45:48.121+0800 imported 43 documents

[root@50 ~] # mongo-host 192.168.4.50-port 27050 # login database view

Db.c3.find ()

{"_ id": ObjectId ("5b408bcb7f8c3df38b1fca2d"), "name": "yaya", "pass": "x", "uid": 123,123 "gid", "comment": "teacher", "homeaddr": "/ home/bin", "shell": "/ bin/bash"}

{"_ id": ObjectId ("5b408bcb7f8c3df38b1fca2e"), "name": "root", "pass": "x", "uid": 0, "gid": 0, "comment": "root", "homeaddr": "/ root", "shell": "/ bin/bash"}

{"_ id": ObjectId ("5b408bcb7f8c3df38b1fca2f"), "name": "bin", "pass": "x", "uid": 1, "gid": 1, "comment": "bin", "homeaddr": "/ bin", "shell": "/ sbin/nologin"}

{"_ id": ObjectId ("5b408bcb7f8c3df38b1fca30"), "name": "daemon", "pass": "x", "uid": 2, "gid": 2, "comment": "daemon", "homeaddr": "/ sbin", "shell": "/ sbin/nologin"}

..

Data backup and recovery

7.1 data backup

Back up all the data libraries to the dump directory under the current directory

[root@50 mnt] # mongodump-- host 192.168.4.50-- port 27050-d bbsdb

2018-07-07T17:56:11.403+0800 writing bbsdb.t2 to

2018-07-07T17:56:11.403+0800 writing bbsdb.t1 to

2018-07-07T17:56:11.403+0800 writing bbsdb.c3 to

2018-07-07T17:56:11.405+0800 done dumping bbsdb.t2 (8 documents)

# mongodump [--host ip address-- port port

Specify the backup library and backup directory when backing up

# mongodump [--host ip address-- port port]-d database name

-c collection name-o directory (you can specify the directory when you do not need to create a backup in advance!)

View the contents of the bson file

# bsondump. / dump/bbs/t1.bson

7.2 data recovery

Grammatical format

-mongorestore-- host IP address-- port port-d number

Backup directory name according to library name [- c collection name]

[root@50 mnt] # mongorestore-- host 192.168.4.50-- port 27050-d db1-c T1 / mnt/mogobak/bbsdb/t2.bson

2018-07-07T18:04:47.914+0800 checking for collection data in / mnt/mogobak/bbsdb/t2.bson

2018-07-07T18:04:47.914+0800 reading metadata for db1.t1 from / mnt/mogobak/bbsdb/t2.metadata.json

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