当前位置:Gxlcms > 数据库问题 > 四十二.部署MongoDB服务 、 MongoDB基本使用

四十二.部署MongoDB服务 、 MongoDB基本使用

时间:2021-07-01 10:21:17 帮助过:17人阅读

"name" : "bob", "age" : 19, "sex" : "boy", "tel" : "13152098678" }   > db.t1.remove({name:"tom"})   > db.t2.drop() > show tables > show tables system.version t1   > db.t1.remove({}) > db.t1.find() # 查找数据 > show tables system.version t1   2.2 mongodb基本数据类型 字符类型 "abc"  "中国"    布尔 true 或false 空   null 数值 64位浮点型  8  NumberLong()  NumberInt() 数组 ["","",""]   字符 > db.t1.save({name:"tom"}) 布尔 > db.t1.save({name:"jerry",card:true,marry:false}) > db.t1.save({name:"tom",card:true}) 空 > db.t1.save({name:"lilei",ruslt:null}) > db.t1.find() { "_id" : ObjectId("5c6a57c76f800ed6eb2a847c"), "name" : "tom" } { "_id" : ObjectId("5c6a57d16f800ed6eb2a847d"), "name" : "jerry", "card" : true, "marry" : false } { "_id" : ObjectId("5c6a57d86f800ed6eb2a847e"), "name" : "tom", "card" : true } { "_id" : ObjectId("5c6a58046f800ed6eb2a847f"), "name" : "lilei", "ruslt" : null }     数值 > db.t1.save({name:"mack3",pay:NumberLong(300000)}) > db.t1.save({name:"mack3",pay:NumberInt(300.56)}) > db.t1.save({name:"mack3",pay:300.56}) { "_id" : ObjectId("5c6a5a656f800ed6eb2a8480"), "name" : "mack3", "pay" : NumberLong(300000) } { "_id" : ObjectId("5c6a5a676f800ed6eb2a8481"), "name" : "mack3", "pay" : 300 } { "_id" : ObjectId("5c6a5a6f6f800ed6eb2a8482"), "name" : "mack3", "pay" : 300.56 }   数组 > db.t1.save({name:"mack4",like:["a","b","c"]}) > db.t1.find() { "_id" : ObjectId("5c6a5aa06f800ed6eb2a8483"), "name" : "mack4", "like" : [ "a", "b", "c" ] }   代码 > db.t1.save({ lname:"php",dm:function(){/* <?php echo "abc"; ?>*/}}) > db.t1.find({lname:"php"}) { "_id" : ObjectId("5c6a5ac86f800ed6eb2a8485"), "lname" : "php", "dm" : { "code" : "function (){/* <?php echo \"abc\"; ?>*/}" } }   对象 > db.t1.save({name:"009", num:ObjectId() }) > db.t1.find() { "_id" : ObjectId("5c6a5ae36f800ed6eb2a8487"), "name" : "009", "num" : ObjectId("5c6a5ae36f800ed6eb2a8486") }   日期 > db.t1.save({ name:"jerry",birthday:new Date() }) { "_id" : ObjectId("5c6a5b6c6f800ed6eb2a8488"), "name" : "jerry", "birthday" : ISODate("2019-02-18T07:14:52.596Z") }   内嵌   > db.t3.save({  birdbook: { worker:"birdboy" ,pay:99 , ver:3.0},   ttt: { addr:"bg" ,tel:"12306",per:"shy"}   }) > db.t3.find() { "_id" : ObjectId("5c6a5ba66f800ed6eb2a8489"), "birdbook" : { "worker" : "birdboy", "pay" : 99, "ver" : 3 }, "ttt" : { "addr" : "bg", "tel" : "12306", "per" : "shy" } }   正则表达式 > db.t3.save({name:"cc",bds:/.*a.*/}) > db.t3.save({name:"dd",bds:/^..$/}) > db.t3.save({name:"dd",bds:/^a/}) > db.t3.find() { "_id" : ObjectId("5c6a5bf86f800ed6eb2a848b"), "name" : "cc", "bds" : /.*a.*/ } { "_id" : ObjectId("5c6a5bfe6f800ed6eb2a848c"), "name" : "dd", "bds" : /^..$/ } { "_id" : ObjectId("5c6a5c036f800ed6eb2a848d"), "name" : "dd", "bds" : /^a/ }   2.3 数据导入导出    数据导出: 把集合的文档存储到系统文件里 创建存储文件的目录 ]# mkdir /mdb 导出为json格式 ]# /usr/local/mongodb/bin/mongoexport \  --host 192.168.4.50 --port 27050  \  -d gamedb -c t1 --type=json > /mdb/gamedb_t1.json(必须有gamedb库,t1表)   ]# ls /mdb/*.json ]# cat /mdb/gamedb_t1.json    导出为csv格式 ]# /usr/local/mongodb/bin/mongoexport  \  --host 192.168.4.50 --port 27050 \  -d gamedb -c t1 -f name,age --type=csv >  /mdb/gamedb_t1.csv   ]# ls /mdb/*.csv ]# cat /mdb/gamedb_t1.csv   数据导入: 把系统文件的内容存储到集合里   使用json文件导入数据 ]# /usr/local/mongodb/bin/mongoimport \  --host 192.168.4.50 --port 27050 \  -d bbsdb  -c user --type=json /mdb/gamedb_t1.json    ]#/usr/local/mongodb/bin/mongo --host 192.168.4.50 --port 27050 > show dbs > use bbsdb > show tables > db.user.count() > db.user.find()   使用csv文件导入数据 ]# /usr/local/mongodb/bin/mongoimport \    --host 192.168.4.50 --port 27050 \     -d bbsdb  -c user2  -f user,old \    --type=csv  /mdb/gamedb_t1.csv   > use bbsdb > db.user2.count() > db.user2.find()   不加 --drop 选项 是追加方式导入数据 反之 删除原表后再导入数据 ]# /usr/local/mongodb/bin/mongoimport \   --host 192.168.4.50 --port 27050 \    -d bbsdb  -c user5  --headerline  --drop  \   --type=csv  /mdb/gamedb_t1.csv   > use bbsdb > db.user5.count() > db.user5.find()   把/etc/passwd文件的内容存储到 bbsdb库里user6集合里。 ]# cp /etc/passwd /mdb/   ]# sed -i  ‘s/:/,/g‘  /mdb/passwd   ]# sed -i ‘1iname,password,uid,gid,comment,homedir,shell‘ /mdb/passwd   ]# /usr/local/mongodb/bin/mongoimport --host 192.168.4.50 \    --port 27050  -d bbsdb  -c user6 \    --headerline --type=csv /mdb/passwd   > use bbsdb > db.user6.count() > db.user6.find()   2.4 数据备份恢复 数据备份   170  cd /mdb   171  ls   172  /usr/local/mongodb/bin/mongodump  --host 192.168.4.50 --port 27050    173  ls   174  cd dump/   175  ls   176  ls admin/   177  cat admin/system.version.bson  报错   178  /usr/local/mongodb/bin/bsondump /mdb/dump/bbsdb/user6.bson      180  mkdir /mdbbak   181  /usr/local/mongodb/bin/mongodump  --host 192.168.4.50 \        --port 27050 -d bbsdb  -c user6  -o  /mdbbak/      182  ls /mdbbak/   183  ls /mdbbak/bbsdb/   184  /usr/local/mongodb/bin/bsondump /mdbbak/bbsdb/user6.bson(查看 数据)    数据恢复 ]# /usr/local/mongodb/bin/mongorestore  \  --host  192.168.4.50  --port 27050 \  -d testdb -c teatab  /mdbbak/bbsdb/user6.bson    删除要恢复的库 再执行恢复 ]# /usr/local/mongodb/bin/mongorestore  \  --host  192.168.4.50  --port 27050  /mdb/dump/

四十二.部署MongoDB服务 、 MongoDB基本使用

标签:cal   bind   top   定义   down   count()   null   0.00   命令别名   

人气教程排行