MongoDB & Linux
1. environment
set path = D:\opt\MongoDB\Server\3.4\bin
D:\opt\MongoDB\Server\3.4\bin;
http://mongodb.github.io/mongo-csharp-driver/2.8/getting_started/quick_tour/
http://mongodb.github.io/mongo-csharp-driver/2.8/getting_started/installation/
http://mongodb.github.io/mongo-csharp-driver/2.8/getting_started/quick_tour/
https://github.com/mongodb/mongo-csharp-driver
2. start up mongo service
mongod --dbpath D:\data\db
./bin/mongod --dbpath=/home/m20 --logpath=/home/mlog/m20.log --fork --configsvr --port 27020
./bin/mongos --logpath=/home/m30.log --configdb=192.168.1.202:27020 --fork
mongod --port 27017 --dbpath D:\MongoDB\data\db
Add the following lines in "mongo.config" file
port=27017
dbpath=C:\mongodb\data\db\
logpath=C:\mongodb\log\mongo.log
Start server :
mongod.exe --config="C:\mongodb\mongo.config"
Connect to remote MongoDB server via command line with authentication.
mongo --username abcd --password abc123 --host server_ip_or_dns --port 27017
3. manage mongo in console
mongo
mongo 127.0.0.1:27017
mongo --port 27020
4
4.1
show dbs/ databases
admin and local, both are manager database that is important.
test
use dbname00
db.user.insert({}) //create user collections(tables) by inserting a raw.
show dbs
db.user.insert({"name":"testuser","age":21})
use dbname
show collections /tables
4.2
db.user.find(); db.user.find({"age":{$gt:20}});
$gt; $gte; $lt; $lte;
db.user.find({"age":{$gte:23,$lte:26}});
4.3 db.user.find({},{name:1,age:1})//select name,age from user
db.user.find({"age":{$gt:24}},{name:1})//select name from user where age>24;
4.4 db.user.find({"name":/mongo/});// select * from user where name like '%mongo%'
db.user.find({"name":/^mongo/});//select * from user where name like 'mongo%';
4.5 db.user.find().sort({"age":1});db.user.find().sort({"age":-1});
4.6 db.user.find({}).limit(2);
4.7 db.user.find().skip(2).limit(3); // select top 3 * from user where id not in( select top 2 from user)
db.user.find().skip(0).limit(2); db.user.find().skip(2).limit(2);
//limit is pageSzie. skip is page number*pageSize
4.8 select * from user where age=20 or age=26
db.user.find({$or:[{age:20},{age:26}]});
4.9 select top 1 * from user; db.usr.findOne(); db.user.find().limit(1);
4.10 select count(*) from user where age>=20;db.user.find({"age":{$gle:20}}).count();
db.user.find().skip(10).limit(5).count(true);
5
5.1.db.dropDatabase(); db.user.drop();
5.2 db.user.update({"name":"oldname"},{$set:{"name":"newName"}});
db.user.update({"name":"oldname"},{$set:{"age":50,"name":"testUser"}});
By default, the update() method updates a single document. To update multiple documents,
use the multi option in the update() method. db.user.update({"male":true},{#set:{"age":21}}:{multi:true});
5.3. update user set age=age+50 where name='Wang';
db.user.update({"name":"Wang"},{$inc:{age:50}},false,true);
db.user.update({"name":"Wang"},{$inc:{age:50},$set:{name:"Wang2"}},false,true);
5.4 very danger: remove the field "name", replace a raw with "age":30
db.user.update({"name":"Wang"},{"age":30}); //very danger
5.5 delete * from user where name='Wang';
db.user.remove({"name":"Wang"});
By default, the remove() method removes all documents that match the remove condition.
Use the just One option to limit the remove operation to only one of the matching documents
db.user.remove({"name":"Wang"},{justOne:true});
6.Index
db.user.ensureIndex({"username":1})
db.user.getIndexes()
db.user.dropIndex({"username":1})
for(var i=0;i<100;i++){ db.shop.insert({"title":"shop"+i})}
db.shop.find().count()
db.shop.find().limit(10)
//数字1表示username关键字的索引按升序存储, -1 表示age关键字的索引按降序存储。
db.user.ensureIndex({"username":1,"age":-1})
7.
db.tablename.find().explain("executionStats")
db.shop.find().explain("executionStats")
//explain.executionStats.executionTimeMillis;
db.user.ensureIndex({"name":1})
db.user.getIndexes()
db.user.find({"name":"tt"}).explain("executionStats")
8.
db.help()
sh.addShard('');
sh.enableShar....
sh.shardCollection('test.dz',{sn:1});
sh.splitAt('test.dz',{sn:1000});
for(var i=2;i<=5;i++){ sh.splitAt('test.dz',{sn:i*1000} }
https://www.youtube.com/watch?v=t13YqEwXh_0&list=PL8LR_PrSuIRhii39nOeRsP7TLyQIBfvJK
*.Tool
set path = D:\opt\MongoDB\Server\3.4\bin
D:\opt\MongoDB\Server\3.4\bin;
http://mongodb.github.io/mongo-csharp-driver/2.8/getting_started/quick_tour/
http://mongodb.github.io/mongo-csharp-driver/2.8/getting_started/installation/
http://mongodb.github.io/mongo-csharp-driver/2.8/getting_started/quick_tour/
https://github.com/mongodb/mongo-csharp-driver
2. start up mongo service
mongod --dbpath D:\data\db
./bin/mongod --dbpath=/home/m20 --logpath=/home/mlog/m20.log --fork --configsvr --port 27020
./bin/mongos --logpath=/home/m30.log --configdb=192.168.1.202:27020 --fork
mongod --port 27017 --dbpath D:\MongoDB\data\db
Add the following lines in "mongo.config" file
port=27017
dbpath=C:\mongodb\data\db\
logpath=C:\mongodb\log\mongo.log
Start server :
mongod.exe --config="C:\mongodb\mongo.config"
Connect to remote MongoDB server via command line with authentication.
mongo --username abcd --password abc123 --host server_ip_or_dns --port 27017
3. manage mongo in console
mongo
mongo 127.0.0.1:27017
mongo --port 27020
4
4.1
show dbs/ databases
admin and local, both are manager database that is important.
test
use dbname00
db.user.insert({}) //create user collections(tables) by inserting a raw.
show dbs
db.user.insert({"name":"testuser","age":21})
use dbname
show collections /tables
4.2
db.user.find(); db.user.find({"age":{$gt:20}});
$gt; $gte; $lt; $lte;
db.user.find({"age":{$gte:23,$lte:26}});
4.3 db.user.find({},{name:1,age:1})//select name,age from user
db.user.find({"age":{$gt:24}},{name:1})//select name from user where age>24;
4.4 db.user.find({"name":/mongo/});// select * from user where name like '%mongo%'
db.user.find({"name":/^mongo/});//select * from user where name like 'mongo%';
4.5 db.user.find().sort({"age":1});db.user.find().sort({"age":-1});
4.6 db.user.find({}).limit(2);
4.7 db.user.find().skip(2).limit(3); // select top 3 * from user where id not in( select top 2 from user)
db.user.find().skip(0).limit(2); db.user.find().skip(2).limit(2);
//limit is pageSzie. skip is page number*pageSize
4.8 select * from user where age=20 or age=26
db.user.find({$or:[{age:20},{age:26}]});
4.9 select top 1 * from user; db.usr.findOne(); db.user.find().limit(1);
4.10 select count(*) from user where age>=20;db.user.find({"age":{$gle:20}}).count();
db.user.find().skip(10).limit(5).count(true);
5
5.1.db.dropDatabase(); db.user.drop();
5.2 db.user.update({"name":"oldname"},{$set:{"name":"newName"}});
db.user.update({"name":"oldname"},{$set:{"age":50,"name":"testUser"}});
By default, the update() method updates a single document. To update multiple documents,
use the multi option in the update() method. db.user.update({"male":true},{#set:{"age":21}}:{multi:true});
5.3. update user set age=age+50 where name='Wang';
db.user.update({"name":"Wang"},{$inc:{age:50}},false,true);
db.user.update({"name":"Wang"},{$inc:{age:50},$set:{name:"Wang2"}},false,true);
5.4 very danger: remove the field "name", replace a raw with "age":30
db.user.update({"name":"Wang"},{"age":30}); //very danger
5.5 delete * from user where name='Wang';
db.user.remove({"name":"Wang"});
By default, the remove() method removes all documents that match the remove condition.
Use the just One option to limit the remove operation to only one of the matching documents
db.user.remove({"name":"Wang"},{justOne:true});
6.Index
db.user.ensureIndex({"username":1})
db.user.getIndexes()
db.user.dropIndex({"username":1})
for(var i=0;i<100;i++){ db.shop.insert({"title":"shop"+i})}
db.shop.find().count()
db.shop.find().limit(10)
//数字1表示username关键字的索引按升序存储, -1 表示age关键字的索引按降序存储。
db.user.ensureIndex({"username":1,"age":-1})
7.
db.tablename.find().explain("executionStats")
db.shop.find().explain("executionStats")
//explain.executionStats.executionTimeMillis;
db.user.ensureIndex({"name":1})
db.user.getIndexes()
db.user.find({"name":"tt"}).explain("executionStats")
8.
db.help()
sh.addShard('');
sh.enableShar....
sh.shardCollection('test.dz',{sn:1});
sh.splitAt('test.dz',{sn:1000});
for(var i=2;i<=5;i++){ sh.splitAt('test.dz',{sn:i*1000} }
https://www.youtube.com/watch?v=t13YqEwXh_0&list=PL8LR_PrSuIRhii39nOeRsP7TLyQIBfvJK
*.Tool
SecureCRT
putty
1.
tar zxvf mongodb-linux-x86_64-2.2.6.tgz
2.cd mongodb-linux-x86_64-2.2.6
3.ll bin/
4.mongod: service inside the server(mysqld.exe)
mongos: look routers and clusters
mongo: client tool(mysql.exe)
5.mongoexport: mongoimport to json,csv,tsv
6. mongodump, export bson(mysqldump)(all database)
mongorestore: import bson(all database)
7
bsondump:bson to json
monoplog:
8 mongostats,mongotop,mongosniff, check mongo status
9 rm -rf /usr/local/mongodb/
mv mongodb-linux-x86_64-2.2.6 /usr/local/mongodb
cd /usr/local/mongodb
./bin/mongod --help
10. df -h //check the room of disk
mkdir -p /home/m17 /home/mlog
11./bin/mongod --dbpath /home/m17/ --logpath /home/mlog/m17.log --fork --port 27017
12.ps aux|grep mongo
du -h // mongodb default more 3G room.
options:--smallfiles 400M
memcached,redis kv(key/value)
mongodb Bson->json
putty
1.
tar zxvf mongodb-linux-x86_64-2.2.6.tgz
2.cd mongodb-linux-x86_64-2.2.6
3.ll bin/
4.mongod: service inside the server(mysqld.exe)
mongos: look routers and clusters
mongo: client tool(mysql.exe)
5.mongoexport: mongoimport to json,csv,tsv
6. mongodump, export bson(mysqldump)(all database)
mongorestore: import bson(all database)
7
bsondump:bson to json
monoplog:
8 mongostats,mongotop,mongosniff, check mongo status
9 rm -rf /usr/local/mongodb/
mv mongodb-linux-x86_64-2.2.6 /usr/local/mongodb
cd /usr/local/mongodb
./bin/mongod --help
10. df -h //check the room of disk
mkdir -p /home/m17 /home/mlog
11./bin/mongod --dbpath /home/m17/ --logpath /home/mlog/m17.log --fork --port 27017
12.ps aux|grep mongo
du -h // mongodb default more 3G room.
options:--smallfiles 400M
memcached,redis kv(key/value)
mongodb Bson->json
Read Concerns
Fast, safe, or the lastest data in your cluster. Pick two. Really depends on your application requirements. If you want fast reads of the lastest data, local or available read concern should suit you just fine. But you are going to lose your durability guarantee.
If you want fast read of the safes data, then majority read concern is a good middle ground. But again, you may not be getting the latest written data to your cluster.
if you want safe reads of the latest data at the time of the read operation, then linearizeable is a good chocie, but it's more likely to be slower. And it's single document reads only. One thing to be really clear about, read concern doesn't prevent deleting data using a CRUD aperation, such as delete. --.--
If you want fast read of the safes data, then majority read concern is a good middle ground. But again, you may not be getting the latest written data to your cluster.
if you want safe reads of the latest data at the time of the read operation, then linearizeable is a good chocie, but it's more likely to be slower. And it's single document reads only. One thing to be really clear about, read concern doesn't prevent deleting data using a CRUD aperation, such as delete. --.--
评论
发表评论