## 看看mongodb的基本操作
### mongodb的基本使用包括几个方面
增、删、查、改是重点
CRUD
C:insert()
R:find()
U:update()
D:remove()
### 增加数据
[root@blog ~]# hostname blog.unix178.com [root@blog ~]# mongo MongoDB shell version: 2.4.6 connecting to: test > show dbs admin0.203125GB local0.078125GB test0.203125GB > use zhuima switched to db zhuima > info = {Name:"zhuima",Age:26,Gender:"F",Address:"Beijing China",Work:"Engineer",Other:"DevOps"} { "Name" : "zhuima", "Age" : 26, "Gender" : "F", "Address" : "Beijing China", "Work" : "Engineer", "Other" : "DevOps" } > db.peroson.insert(info) > db.person.find() > db.peroson.find() { "_id" : ObjectId("53cbc52255ea1fd13e47ad9d"), "Name" : "zhuima", "Age" : 26, "Gender" : "F", "Address" : "Beijing China", "Work" : "Engineer", "Other" : "DevOps" } >
### 同样我们可以批量插入数据
> for(x=1;x<=1000;x++) db.peroson.insert({Name:"zhuima"+x,Age:x%100,Address:x+".Number Beijing China"}) > db.peroson.find() { "_id" : ObjectId("53cbc52255ea1fd13e47ad9d"), "Name" : "zhuima", "Age" : 26, "Gender" : "F", "Address" : "Beijing China", "Work" : "Engineer", "Other" : "DevOps" } { "_id" : ObjectId("53cbc67855ea1fd13e47ad9e"), "Name" : "zhuima1", "Age" : 1, "Address" : "1.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47ad9f"), "Name" : "zhuima2", "Age" : 2, "Address" : "2.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47ada0"), "Name" : "zhuima3", "Age" : 3, "Address" : "3.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47ada1"), "Name" : "zhuima4", "Age" : 4, "Address" : "4.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47ada2"), "Name" : "zhuima5", "Age" : 5, "Address" : "5.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47ada3"), "Name" : "zhuima6", "Age" : 6, "Address" : "6.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47ada4"), "Name" : "zhuima7", "Age" : 7, "Address" : "7.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47ada5"), "Name" : "zhuima8", "Age" : 8, "Address" : "8.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47ada6"), "Name" : "zhuima9", "Age" : 9, "Address" : "9.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47ada7"), "Name" : "zhuima10", "Age" : 10, "Address" : "10.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47ada8"), "Name" : "zhuima11", "Age" : 11, "Address" : "11.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47ada9"), "Name" : "zhuima12", "Age" : 12, "Address" : "12.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adaa"), "Name" : "zhuima13", "Age" : 13, "Address" : "13.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adab"), "Name" : "zhuima14", "Age" : 14, "Address" : "14.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adac"), "Name" : "zhuima15", "Age" : 15, "Address" : "15.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adad"), "Name" : "zhuima16", "Age" : 16, "Address" : "16.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adae"), "Name" : "zhuima17", "Age" : 17, "Address" : "17.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adaf"), "Name" : "zhuima18", "Age" : 18, "Address" : "18.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adb0"), "Name" : "zhuima19", "Age" : 19, "Address" : "19.Number Beijing China" } Type "it" for more >
### 删除数据
> db.peroson.remove({Name:"zhuima"}) > db.peroson.find() { "_id" : ObjectId("53cbc67855ea1fd13e47ad9e"), "Name" : "zhuima1", "Age" : 1, "Address" : "1.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47ad9f"), "Name" : "zhuima2", "Age" : 2, "Address" : "2.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47ada0"), "Name" : "zhuima3", "Age" : 3, "Address" : "3.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47ada1"), "Name" : "zhuima4", "Age" : 4, "Address" : "4.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47ada2"), "Name" : "zhuima5", "Age" : 5, "Address" : "5.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47ada3"), "Name" : "zhuima6", "Age" : 6, "Address" : "6.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47ada4"), "Name" : "zhuima7", "Age" : 7, "Address" : "7.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47ada5"), "Name" : "zhuima8", "Age" : 8, "Address" : "8.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47ada6"), "Name" : "zhuima9", "Age" : 9, "Address" : "9.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47ada7"), "Name" : "zhuima10", "Age" : 10, "Address" : "10.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47ada8"), "Name" : "zhuima11", "Age" : 11, "Address" : "11.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47ada9"), "Name" : "zhuima12", "Age" : 12, "Address" : "12.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adaa"), "Name" : "zhuima13", "Age" : 13, "Address" : "13.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adab"), "Name" : "zhuima14", "Age" : 14, "Address" : "14.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adac"), "Name" : "zhuima15", "Age" : 15, "Address" : "15.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adad"), "Name" : "zhuima16", "Age" : 16, "Address" : "16.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adae"), "Name" : "zhuima17", "Age" : 17, "Address" : "17.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adaf"), "Name" : "zhuima18", "Age" : 18, "Address" : "18.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adb0"), "Name" : "zhuima19", "Age" : 19, "Address" : "19.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adb1"), "Name" : "zhuima20", "Age" : 20, "Address" : "20.Number Beijing China" } Type "it" for more >
### 批量删除数据
> db.peroson.remove({Age:{$gt:1,$lt:10}}) > db.peroson.find() { "_id" : ObjectId("53cbc67855ea1fd13e47ad9e"), "Name" : "zhuima1", "Age" : 1, "Address" : "1.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47ada7"), "Name" : "zhuima10", "Age" : 10, "Address" : "10.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47ada8"), "Name" : "zhuima11", "Age" : 11, "Address" : "11.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47ada9"), "Name" : "zhuima12", "Age" : 12, "Address" : "12.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adaa"), "Name" : "zhuima13", "Age" : 13, "Address" : "13.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adab"), "Name" : "zhuima14", "Age" : 14, "Address" : "14.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adac"), "Name" : "zhuima15", "Age" : 15, "Address" : "15.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adad"), "Name" : "zhuima16", "Age" : 16, "Address" : "16.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adae"), "Name" : "zhuima17", "Age" : 17, "Address" : "17.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adaf"), "Name" : "zhuima18", "Age" : 18, "Address" : "18.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adb0"), "Name" : "zhuima19", "Age" : 19, "Address" : "19.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adb1"), "Name" : "zhuima20", "Age" : 20, "Address" : "20.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adb2"), "Name" : "zhuima21", "Age" : 21, "Address" : "21.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adb3"), "Name" : "zhuima22", "Age" : 22, "Address" : "22.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adb4"), "Name" : "zhuima23", "Age" : 23, "Address" : "23.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adb5"), "Name" : "zhuima24", "Age" : 24, "Address" : "24.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adb6"), "Name" : "zhuima25", "Age" : 25, "Address" : "25.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adb7"), "Name" : "zhuima26", "Age" : 26, "Address" : "26.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adb8"), "Name" : "zhuima27", "Age" : 27, "Address" : "27.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adb9"), "Name" : "zhuima28", "Age" : 28, "Address" : "28.Number Beijing China" } Type "it" for more >
### 指定某个条件查找数据
> db.peroson.findOne() { "_id" : ObjectId("53cbc67855ea1fd13e47ad9e"), "Name" : "zhuima1", "Age" : 1, "Address" : "1.Number Beijing China" } > db.peroson.find({Name:"zhuima888"}) { "_id" : ObjectId("53cbc67855ea1fd13e47b115"), "Name" : "zhuima888", "Age" : 88, "Address" : "888.Number Beijing China" } >
### 指定多重条件查找
> db.peroson.find({Age:{$gt:33,$lt:44}}) { "_id" : ObjectId("53cbc67855ea1fd13e47adbf"), "Name" : "zhuima34", "Age" : 34, "Address" : "34.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adc0"), "Name" : "zhuima35", "Age" : 35, "Address" : "35.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adc1"), "Name" : "zhuima36", "Age" : 36, "Address" : "36.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adc2"), "Name" : "zhuima37", "Age" : 37, "Address" : "37.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adc3"), "Name" : "zhuima38", "Age" : 38, "Address" : "38.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adc4"), "Name" : "zhuima39", "Age" : 39, "Address" : "39.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adc5"), "Name" : "zhuima40", "Age" : 40, "Address" : "40.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adc6"), "Name" : "zhuima41", "Age" : 41, "Address" : "41.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adc7"), "Name" : "zhuima42", "Age" : 42, "Address" : "42.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47adc8"), "Name" : "zhuima43", "Age" : 43, "Address" : "43.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47ae23"), "Name" : "zhuima134", "Age" : 34, "Address" : "134.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47ae24"), "Name" : "zhuima135", "Age" : 35, "Address" : "135.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47ae25"), "Name" : "zhuima136", "Age" : 36, "Address" : "136.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47ae26"), "Name" : "zhuima137", "Age" : 37, "Address" : "137.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47ae27"), "Name" : "zhuima138", "Age" : 38, "Address" : "138.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47ae28"), "Name" : "zhuima139", "Age" : 39, "Address" : "139.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47ae29"), "Name" : "zhuima140", "Age" : 40, "Address" : "140.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47ae2a"), "Name" : "zhuima141", "Age" : 41, "Address" : "141.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47ae2b"), "Name" : "zhuima142", "Age" : 42, "Address" : "142.Number Beijing China" } { "_id" : ObjectId("53cbc67855ea1fd13e47ae2c"), "Name" : "zhuima143", "Age" : 43, "Address" : "143.Number Beijing China" } Type "it" for more >
### 更新数据
> db.peroson.find({Name:"zhuima100"}) { "_id" : ObjectId("53cbc67855ea1fd13e47ae01"), "Name" : "zhuima100", "Age" : 0, "Address" : "100.Number Beijing China" } > db.peroson.find({Name:"zhuima100"}) { "_id" : ObjectId("53cbc67855ea1fd13e47ae01"), "Name" : "zhuima100", "Age" : 100, "Address" : "100.Number Beijing China" } >
### 批量更新数据
数组修改器
$push 会向已有的数组末尾加入一个元素,要是没有就会创建一个新的数组
> db.person.findOne() { "_id" : ObjectId("53615d0b447315f76c35aee9"), "user_id" : "zhuima", "age" : 27, "tel" : 12335345, "add" : "beijing.tiananmen" } > db.person.update({"tel":12335345},{"$push":{"info":{"gender":"male","email":"[email protected]"}}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.person.findOne({"tel":12335345}) { "_id" : ObjectId("53615d0b447315f76c35aee9"), "user_id" : "zhuima", "age" : 27, "tel" : 12335345, "add" : "beijing.tiananmen", "info" : [ { "gender" : "male", "email" : "[email protected]" } ] } >
## 看了上面的一些例子,我们来点短小的命令
### 命令行接口
mongo >
### 查看所有数据库
> show dbs
### 查看数据库中所有的表
后面的;可有可无,看个人喜好
> show dbs; > use zhuima; > show collections;
### 为数据库下某个表插入数据
> j = { name : "zhuima"} > k = { age : 23} > m = {sex : "fmale"} > db.test.insert(j) > db.test.insert(k) > db.test.insert(m) >
### 查看某个数据库中的数据
> db.test.find() # 默认显示前20行 可以再>后接着输入it显示下一页的20行数据~ >
### 查看数据库中的某个表的数据第一行
> db.test.findOne() >
### 查看数据库中的某个表的前几个
limit限制输出信息量
> db.test.find().limit(3) 只列出前几行 >
### 如何显示数据库中所有信息(突破只能显示20行的限制)
> var c = db.test.find() > while (c.hasNext()) printjson(c.next()) >
### 对数据进行筛选
> db.text.find({x:3}) #筛选出x对应值为3的匹配的行 >
### 批量插入数据
> for (i =1; i<=25;i++) db.test.insert({x:i}) >
### 只列出从第几行开始的行(有规律的)
sort进行排序操作
$gt是mongodb内置的比较运算的内置变量
> db.test.find({x:{$gt:5}}).sort({x:3}) #列出从第6行开始的行并进行排序 >
### 只列出某个范围内的行
> db.test.find({x:{$gt:5}}).limit(5) # 只列出第6行到第10行 >
### 过滤包含某些关键字的筛选
$in也是mongodb内置的比较运算的内置变量
> db.inventory.find( { type: { $in: [ 'food', 'snacks' ] } } ) > db.zhuima.find({name:{$in:["zhuima"]}}) { "_id" : ObjectId("53b272e9c74fabe96af3a054"), "name" : "zhuima" } >
### 数组定义
> i={"date":new Date()}
### 可以嵌套数组的
> name={"name":{"gender":"male"}} { "name" : { "gender" : "male" } } >
### 定义一个名为追马的人的相关信息
> info={"name":"zhuima","other":["male","single"],"age":22} { "name" : "zhuima", "other" : [ "male", "single" ], "age" : 22 } >
内置变量(非完全)
### 元素查询
$exists 检查是否存在
db.collections.find({address:{$exists:true}}) 验证存在address这个字符串的数据
$mod 将指定字段的值进行取模运算
$type 返回指定字段的值类型
### 逻辑运算
$or
$and
$not
$nor
db.collections.find({$and :{age :{$gte :61}},{age :{$lte 80}}}})
将大于61小于80的打印出来
### 比较运算
$gt
$gte
$in
$lt
$lte
$ne
$nin
### 只打印指定信息
{age:1},{name:1} 1表示为真
### 更新文档
db.collections.update({age:{$gt:80}},{$set:{Gender:"F"}})
将年龄大于80的性别全部更改为男
### Field类常用的操作
$inc 增加已有键的值
$rename 重命名已有键
$set 在原来的基础上进行数据的增加,如果存在则添加,如果不存在就新建
$unset 删除某个字段