More MongoDB operations

Source: Internet
Author: User
Tags findone

MongoDB also provides an HTTP interface for viewing the running status and restfull
The default port number is 28017.
Restful access interface
Reference: http://www.mongodb.org/display/DOCS/Http+Interface
Sharding function of MongoDB
MongoDB's auto-sharding function means that MongoDB uses mongos to automatically create a horizontally Scalable Data Base cluster system and store database sub-tables on each sharding node.
A MongoDB cluster includes some shards (including some mongod processes), mongos routing processes, and one or more config servers.
Shards
Each shard includes one or more services and mongod processes that store data (mongod is the core process of MongoDB data)
Typically, multiple services are enabled for each shard to improve service availability. These service/mongod processes form a replica set in the shard.
Chunks
A chunk is a data range from a special set. (collection, Minkey, maxkey) describes a chunk, which is between the Minkey and maxkey ranges.
For example, the maxsize of chunks is 100 mb. If a file reaches or exceeds this range, it is split into two new chunks. When a Shard has an excessive amount of data, chunks will be migrated to other shards. Similarly, Chunks can be migrated to other shards.
Config server s
The Config server stores the metadata information of the cluster, including the basic information and Chunk information of each server and shard.
The Config server mainly stores chunk information. Each config server copies the complete chunk information.
Configuration: (simulate two shard services and one config Service)
Shard1: 27020
Shard2: 27021
Configuration: 27022
Default port 27017 used when mongos is started
Create a directory for storing data
[Falcon@www.fwphp. CN ~ /Parse data] $ mkdir 27020 27021 27022
Falcon@www.fwphp.cn ~ /Upload data] $ ls
27020 27021 27022
Falcon@www.fwphp.cn ~ /MongoDB/bin] $./mongod -- dbpath/home/Falcon/Alibaba data/27020 -- Port 27020>/home/Falcon/Alibaba data/2701_log &
Falcon@www.fwphp.cn ~ /MongoDB/bin] $./mongod -- dbpath/home/Falcon/mongodata/27021 -- Port 27021>/home/Falcon/mongodata/27021.log &
Falcon@www.fwphp.cn ~ /MongoDB/bin] $./mongod -- dbpath/home/Falcon/mongodata/27022 -- Port 27022>/home/Falcon/mongodata/27022.log &
When mongos is started, port 27017 is enabled by default.
Falcon@www.fwphp.cn ~ /MongoDB/bin] $./mongos -- configdb localhost: 27022>/home/Falcon/login data/config. log &
Check whether startup is enabled
Falcon@www.fwphp.cn ~ /MongoDB/bin] $ PS-Ef | grep Mongo
Falcon 2612 1 0? 00:00:00./mongod -- dbpath/home/Falcon/mongodata/27020 -- Port 27020
Falcon 2619 1 0? 00:00:00./mongod -- dbpath/home/Falcon/mongodata/27021 -- Port 27021
Falcon 2625 1 0? 00:00:00./mongod -- dbpath/home/Falcon/mongodata/27022 -- Port 27022
Falcon 2658 1 0? 00:00:00./mongos -- configdb localhost: 27022
Falcon 2804 2772 0 00:00:00 pts/0 bin/Mongo
Falcon 2847 2812 0 00:00:00 pts/2 grep Mongo
Falcon@www.fwphp.cn ~ /MongoDB/bin] $
Falcon@www.fwphp.cn ~ /MongoDB/bin] $ netstat-an-T
Active Internet connections (servers and established)
PROTO Recv-Q send-Q local address foreign address State
TCP 0 0 0.0.0.0: 10022 0.0.0.0: * listen
TCP 0 0 0.0.0.0: 27017 0.0.0.0: * listen
TCP 0 0 0.0.0.0: 587 0.0.0.0: * listen
TCP 0 0 0.0.0.0: 27020 0.0.0.0: * listen
TCP 0 0 0.0.0.0: 27021 0.0.0.0: * listen
TCP 0 0 0.0.0.0: 27022 0.0.0.0: * listen
......
TCP 0 0 0.0.0.0: 28020 0.0.0.0: * listen
TCP 0 0 0.0.0.0: 28021 0.0.0.0: * listen
TCP 0 0 0.0.0.0: 28022 0.0.0.0: * listen
TCP 0 0 127.0.0.1: 631 0.0.0.0: * listen
........
Copy code generation
The above information proves that MongoDB is fully started. For the HTTP interfaces 28020, 28021, and 28022 enabled
Falcon@www.fwphp.cn ~ /MongoDB/bin] $./Mongo is connected to mongos by default.
MongoDB shell version: 1.2.4-
URL: Test
Connecting to: Test
Type "help" for help
> Show DBS
Admin
Config
Local
Add to shard Node
> Use Admin
Switched to DB Admin
> DB. runcommand ({addshard: "localhost: 27020", allowlocal: true })
{"OK": 1, "added": "localhost: 27020 "}
> DB. runcommand ({addshard: "localhost: 27021", allowlocal: true })
{"OK": 1, "added": "localhost: 27021 "}
> DB. runcommand ({listshards: 1}); view the shard node list
{
"Shards ":[
{
"_ Id": objectid ("4b9cd386c33000afad27718e "),
"Host": "localhost: 27020"
},
{
"_ Id": objectid ("4b9cd381c33000afad27718f "),
"Host": "localhost: 27021"
}
],
"OK": 1
}
User001:
> Config = connect ("localhost: 27022 ")
> Config = config. getsisterdb ("Config ")
> User001 = dB. getsisterdb ("user001 ");
User001
> DB. runcommand ({enablesharding: "user001 "})
{"OK": 1}
> DB. printshardingstatus ();
--- Sharding status ---
Sharding version: {"_ id": objectid ("4b9cd354c33000afad27718d"), "version": 2}
Shards:
{"_ Id": objectid ("4b9cd386c33000afad27718e"), "host": "localhost: 27020 "}
{"_ Id": objectid ("4b9cd381c33000afad27718f"), "host": "localhost: 27021 "}
Databases:
{"Name": "admin", "partitioned": false, "primary": "localhost: 27022", "_ id": objectid ("4b9cd3776693dcfa468dec13 ")}
{"Name": "user001", "partitioned": True, "primary": "localhost: 27021", "_ id": objectid ("4b9cde866693dcfa468dec17 ")}
My chunks
Create a table in user001 and insert data.
> Use user001
Switched to DB user001
> DB. createcollection ("user_001 ")
{"OK": 1}
> Show collections
System. Indexes
User_001
> DB. user_001.insert ({uid: 1, Username: "falcon. c", sex: "male", age: 25 });
> DB. user_001.find ();
{"_ Id": objectid ("4b9ce1a6c84d7f20576c4df1"), "uid": 1, "username": "falcon. C "," sex ":" male "," Age ": 25}
Let's see which shard the user001 database is allocated.
Falcon@www.fwphp.cn ~ /Upload data] $ LS-R
.:
27020 27021 27022 mongos. Log
/27020:
27020. Log producer D. Lock test.0 test.1 test. Ns _ TMP
./27020/_ TMP:
/27021:
27021. Log producer D. Lock _ TMP user.0 user001.0 user001.1 user001.ns user.1 user. NS
./27021/_ TMP:
/27022:
27022. Log config.0 config. Ns mongod. Lock mongos. log _ TMP
./27022/_ TMP:
Falcon@www.fwphp.cn ~ /Upload data] $
From the above file, we can see that user001 is allocated to the shard of 27021, but through the mongos route, we do not feel the chunk where the data is stored.
Sharding management commands
> DB. $ cmd. findone ({isdbgrid: 1 });
{"Isdbgrid": 1, "hostname": "http://www.fwphp.cn/", "OK": 1}
> DB. $ cmd. findone ({ismaster: 1 });
{"Ismaster": 1, "MSG": "isdbgrid", "OK": 1}
> Printshardingstatus (db. getsisterdb ("Config "))
--- Sharding status ---
Sharding version: {"_ id": objectid ("4b9cd354c33000afad27718d"), "version": 2}
Shards:
{"_ Id": objectid ("4b9cd386c33000afad27718e"), "host": "localhost: 27020 "}
{"_ Id": objectid ("4b9cd381c33000afad27718f"), "host": "localhost: 27021 "}
Databases:
{"Name": "admin", "partitioned": false, "primary": "localhost: 27022", "_ id": objectid ("4b9cd3776693dcfa468dec13 ")}
My chunks
{"Name": "user001", "partitioned": True, "primary": "localhost: 27021", "_ id": objectid ("4b9cde866693dcfa468dec17 ")}
My chunks
> Use Admin
Switched to DB Admin
> DB. runcommand ({netstat: 1 })
{"Configserver": "localhost: 27022", "isdbgrid": 1, "OK": 1}
>
Participation information: http://www.mongodb.org/display/DOCS/Sharding
Simple mapreduce operations for MongoDB Databases
MongoDB also implements the mapreduce function to provide the distributed data query service. The mapreduce distribution function is mainly used on shard.
DB. runcommand (
{Mapreduce: <collection>,
Map: <mapfunction>,
Reduce: <performancefunction>
[, Query: <query filter Object>]
[, Sort: <sort the query. Useful for optimization>]
[, Limit: <number of objects to return from collection>]
[, Out: <output-collection Name>]
[, Keeptemp: <true | false>]
[, Finalize: <finalizefunction>]
[, Scope: <object where fields go into JavaScript global scope>]
[, Verbose: True]
}
);
Below is a simple test of mapreduce
This example comes from: http://www.mongodb.org/display/DOCS/MapReduce
> DB. Things. insert ({_ ID: 1, tags: ['Dog', 'cat']});
> DB. Things. insert ({_ ID: 2, tags: ['cat']});
> DB. Things. insert ({_ ID: 3, tags: ['mouse ', 'cat', 'dog']});
> DB. Things. insert ({_ ID: 4, tags: []});
> M = function (){
... This. tags. foreach (
... Function (z ){
... Emit (z, {count: 1 });
...}
...);
};
Function (){
This. tags. foreach (function (z) {emit (z, {count: 1 });});
}
> R = function (Key, values ){
... Var Total = 0;
... For (VAR I = 0; I <values. length; I ++)
... Total + = values [I]. count;
... Return {count: Total };
...};
Function (Key, values ){
VaR Total = 0;
For (VAR I = 0; I <values. length; I ++ ){
Total + = values [I]. count;
}
Return {count: Total };
}
> Res = dB. Things. mapreduce (M, R );
{
"Result": "tmp. MR. mapreduce_1268577545_1 ",
"Timemillis": 25,
"Counts ":{
"Input": 4,
"Emit": 6,
"Output": 3
},
"OK": 1,
"OK": 1,
}
> Res
{
"Result": "tmp. MR. mapreduce_1268577545_1 ",
"Timemillis": 25,
"Counts ":{
"Input": 4,
"Emit": 6,
"Output": 3
},
"OK": 1,
"OK": 1,
}
> DB [res. Result]. Find ()
{"_ Id": "cat", "value": {"count": 3 }}
{"_ Id": "dog", "value": {"count": 2 }}
{"_ Id": "Mouse", "value": {"count": 1 }}
> DB [res. Result]. Drop ()
True
> DB [res. Result]. Find ()
>
Here are some examples of mapreduce:
Http://www.mongodb.org/display/DOCS/MapReduce
Http://github.com/mongodb/mongo/... STS/mr_bigobject.js
Http://github.com/mongodb/mongo/blob/master/jstests/mr5.js
Http://github.com/mongodb/mongo/blob/master/jstests/mr4.js
Http://github.com/mongodb/mongo/blob/master/jstests/mr3.js
Http://github.com/mongodb/mongo/blob/master/jstests/mr2.js
Http://github.com/mongodb/mongo/blob/master/jstests/mr1.js

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.