MONGO practice-operation via JS Shell MONGO
Save command:
J={name: "Wangjingjing", age:15}
Db.user.save (j);
Query command:
var cursor = Db.user.find ();
while (Cursor.hasnext ()) Printjson (Cursor.next ());
Printjson (Cursor[4]);
var arr = Cursor.toarray ();
Db.user.find ({name: "wangjingjing"});
Select age from user where name= ' wangjingjing '
Db.user.find ({name: "wangjingjing"},{age:true});
Db.user.findOne ();
Db.user.find (). limit (3);
To modify a command:
Db.user.update ({name: "wangjingjing"},{$set: {name: "Jingjing"}});
Db.user.update ({name: "wangjingjing"},{$inc: {age:10}}); Add 10 to age
Db.user.update ({name: "wangjingjing"},{$push: {habit:["basketball", "Football"]}});
Delete command:
Db.user.remove ({name: "Jingjing"})
Advanced query Commands:
Db.user.find ({x:{$gt: 3}}); X>3
Db.user.find ({x:{$lt: 3}}); X<3
Db.user.find ({x:{$gte: 3}}); X>=3
Db.user.find ({x:{$lte: 3}}); X<=3
Db.user.find ({x:{$lte: 3, $gte: 2}}); 2<=x<=3
var x = {x:[3,4,5]}
Db.user.save (x);
Db.user.find ({x:{$all: [3,4]}}) match
Db.user.find ({x:{$all: [3,4,6]}}) mismatch
Db.user.find ({x:{$in: [3,4,6]}}) match
Db.user.find ({x:{$nin: [3,4,6]}}) match
Db.user.find ({x:{$exsits: true}) matches attributes with X
Db.user.find ({x:{$mod: [6,1]}}) modulo 6 equals 1
Db.user.find ({x:{$ne: 1}}) x!=1
Db.user.find ({$or: [{name: ' Jing '],[age:10]}) or action
Db.user.find ({x:{$size: 3}}) the number of elements in array x is 3
Db.user.find (). Skip (1). Limit (1). Count (True); A second record
Db.user.find (). Sort ({age:1}); ascending
Db.user.find (). Sort ({age:-1}); descending
Export File
mongoexport-d user-c User-o User.bat
Import file
mongoimport-d user-c User User.bat
Backup files
mongodump-d User-o My_user_dump
Recover files
mongorestore-d User My_user_dump\user\
Secure access
Mongod--bind_ip 192.168.1.103--port 28018
MONGO 192.168.1.103:28018
Build an index
Db.user.ensureIndex ({age:1},{background:true}); Ascending
Db.user.getIndexes ();
Interpreting execution
Db.user.find (). Explain ()
Copy set
Replica Set Usage Guide (configured on a single machine)
Brief introduction:
A replica-set is a set of n Mongod services that work together for backup support.
Building a replica-set requires two steps: Start the service on each node, and initialize the set.
In standard mode, 1 set contains 3 nodes. We're going to build one on the same server right now.
Once the Mongod service node is started, we need an instruction to initialize the set. After a few seconds, three nodes will select a
Master node, after which we can write or query data to this set.
Step One:
Allocate the location space for data storage for 3 nodes, respectively
$ mkdir-p/data/r0
$ mkdir-p/DATA/R1
$ mkdir-p/DATA/R2
After that, using the--replset parameter to start the Mongod service, this parameter requires us to specify the name of the Replica-set, here we call this set "foo"
$ mongod--replset foo--port 27017--dbpath/data/r0
$ mongod--replset foo--port 27018--dbpath/data/r1
$ mongod--replset foo--port 27019--dbpath/data/r2
Now 3 nodes are all running up. At this point, each node should have the following warning message:
————————
Mon 2 11:30:19 [startreplsets] replset can ' t get local.
System.replset config from self or any seed (emptyconfig)
————————
Don't be nervous, it's because we haven't initialized this set yet.
Step Two:
We can initialize this set by running the Replsetinitiate command from one of any 3 nodes.
[Email protected] ~$]$ MONGO localhost:27017
MongoDB Shell version:1.6.3
Connecting To:localhost:27017/test
> config = {_id: ' foo ', Members: [
{_id:0, Host: ' localhost:27017 '},
{_id:1, Host: ' localhost:27018 '},
{_id:2, Host: ' localhost:27019 '}]
}
> rs.initiate (config);
{
"Info": "Config now saved locally. Should come online in about a minute. ",
"OK": 1
}
We need to configure the Config object to indicate the name of the set and the members under this set. The config is then passed to Rs.initiate ().
If everything goes well here, we'll have to get a message saying: This set will start in 1 minutes. Within this time period, there is a
The node is selected as the primary node (master node).
If you want to see the current state of this set, use the Rs.status command:
> Rs.status ()
{
"Set": "Foo",
"Date": "Mon-11:39:08 GMT-0400 (EDT)",
"MyState": 1,
"Members": [
{
"Name": "arete.local:27017",
"Self": true,
},
{
"Name": "localhost:27019",
"Health": 1,
"Uptime": 101,
"Lastheartbeat": "Mon-11:39:07 GMT-0400",
},
{
"Name": "localhost:27018",
"Health": 1,
"Uptime": 107,
"Lastheartbeat": "Mon-11:39:07 GMT-0400",
}
],
"OK": 1
}
Here is a value of "mystate", if this value is 1, the description is the Master node (master), or 2, the slave node (slave).
At the same time, each group of Replica sets has only one Primary, which is used to accept write operations. It is then replicated asynchronously to the other member database. Once the primary is dead, it will automatically vote to elect the successor primary, and the original server becomes a regular member after the recovery. Data may be lost if the data has not been copied from the previous primary to the member server.
Primary> Db.test.insert ({"Name": "Foobar", "Age": 25})
Primary> Db.test.find ()
{"_id": ObjectId ("4f4f38fc47db2bfa5ceb2aee"), "name": "Foobar", "Age": 25}
Secondary> Db.test.find ()
Error: {"$err": "Not Master and Slaveok=false", "Code": 13435}
Secondary> Db.test.insert ({"Name": "Foobar", "Age": 25})
Not master
Set Slaveok=ok on the main library (tested to find that you need to set OK from the library)
Primary> Db.getmongo (). Setslaveok ()
secondary> Use test
Switched to DB test
Secondary> Db.test.find ()
{"_id": ObjectId ("4f4f38fc47db2bfa5ceb2aee"), "name": "Foobar", "Age": 25}
Then kill the main library, will be a candidate as the main library, when repaired, it becomes from the library
The ID generated when inserting data is divided into four time stamps, host number, process number, counter
Http://www.myexception.cn/javascript/1227152.html
MONGO practice-operation via JS Shell MONGO