Mongodb的安装:
vi /etc/selinux/config
Modify:SELINUX=disabled
service iptables stop
yum -y install wget
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel62-3.0.6.tgz
tar -zxvf mongodb-linux-x86_64-rhel62-3.4.2.tgz
mv mongodb-linux-x86_64-rhel62-3.4.2 /usr/local/mongodb
cd /usr/local/mongodb/bin
mkdir -p /data/db /data/logs
touch /data/logs/mongodb.log
./mongod --dbpath=/data/db -fork --logpath=/data/logs/mongodb.log #-fork 表示后台运行
export PATH=/usr/local/mongodb/bin:$PATH
mongo 进入mongodb,可以使用CTRL+D来进行退出
Mongodb的基本使用:
db.version() 查看当前mongodb数据库版本
3.0.6
show dbs 查看当前的所有数据库
> show dbs
local 0.078GB
gogo 0.078GB
db 查看当前所在数据库的位置,即位于哪个数据库
> db
gogo
use gogo 切换到数据库gogo
show collections 查看当前数据库内的集合
> show collections
system.indexes
user
db.user.insert({"name":"zhangsan","age":"22","height":"1.72","weight":"60kg"}) 插入文档
> db.user.insert({"name":"zhangsan","age":"22","height":"1.72","weight":"60kg"})
WriteResult({ "nInserted" : 1 })
db.user.find() 查看当前user集合下的所有文档
> db.user.find()
{ "_id" : ObjectId("58d13e7d79a177687c20accb"), "name" : "zhangsan", "age" : "22", "height" : "1.72", "weight" : "60kg" }
{ "_id" : ObjectId("58d13e8379a177687c20accc"), "name" : "lisi", "age" : "25", "height" : "1.60", "weight" : "50kg" }
{ "_id" : ObjectId("58d13e8b79a177687c20accd"), "name" : "huangwu", "age" : "27", "height" : "1.66", "weight" : "65kg" }
db.user.remove() 删除当前user集合下的所有文档
> db.user.remove({})
WriteResult({ "nRemoved" : 3 })
db.user.stats() 查看当前user集合的状态信息
> db.user.stats()
{
"ns" : "mall.user",
"count" : 3,
"size" : 336,
"avgObjSize" : 112,
"numExtents" : 1,
"storageSize" : 8192,
"lastExtentSize" : 8192,
"paddingFactor" : 1,
"paddingFactorNote" : "paddingFactor is unused and unmaintained in 3.0. It remains hard coded to 1.0 for compatibility only.",
"userFlags" : 1,
"capped" : false,
"nindexes" : 1,
"totalIndexSize" : 8176,
"indexSizes" : {
"_id_" : 8176
},
"ok" : 1
}
db.user.find({"age":"22"}) 可以凭单个字段进行检索
> db.user.find()
{ "_id" : ObjectId("58d13e7d79a177687c20accb"), "name" : "zhangsan", "age" : "22", "height" : "1.72", "weight" : "60kg" }
{ "_id" : ObjectId("58d13e8379a177687c20accc"), "name" : "lisi", "age" : "25", "height" : "1.60", "weight" : "50kg" }
{ "_id" : ObjectId("58d13e8b79a177687c20accd"), "name" : "hunagwu", "age" : "27", "height" : "1.66", "weight" : "65kg" }
{ "_id" : ObjectId("58d1488579a177687c20acce"), "name" : "xingliu", "age" : "22", "height" : "1.72", "weight" : "60kg" }
> db.user.find({"age":"22"})
{ "_id" : ObjectId("58d13e7d79a177687c20accb"), "name" : "zhangsan", "age" : "22", "height" : "1.72", "weight" : "60kg" }
{ "_id" : ObjectId("58d1488579a177687c20acce"), "name" : "xingliu", "age" : "22", "height" : "1.72", "weight" : "60kg" }
db.user.save
1.文档存在就更新,不存在就新建
> db.user.save({"name":"binbin"})
WriteResult({ "nInserted" : 1 })
db.user.find({"name":"binbin"})
{ "_id" : ObjectId("58d15f8879a177687c20accf"), "name" : "binbin" }
db.user.update
1.整体替换文档中的内容:
> db.user.find({"name":"zhangsan"})
{ "_id" : ObjectId("58d13e7d79a177687c20accb"), "name" : "zhangsan", "age" : "22", "height" : "1.72", "weight" : "60kg" }
> db.user.update({"name":"zhangsan"},{"name":"zhangsan","age":"26"})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
可以通过下面看到最终zhangsan的年龄被更改成了26,而且其后面的字段被删除。所以这是个整体型替换。
> db.user.find({"name":"zhangsan"})
{ "_id" : ObjectId("58d13e7d79a177687c20accb"), "name" : "zhangsan", "age" : "26" }
2.找不到就新增:
那如果找不到该怎么办呢?比如里面没有zhangsan这个人。那么就需要,让其在查找的时候,找不到就新增,找到就全部替换。(需要在后面添加true)
> db.user.update({"name":"wanghui"},{"name":"wanghui","age":"18","sex":"girl"},true)
WriteResult({
"nMatched" : 0,
"nUpserted" : 1,
"nModified" : 0,
"_id" : ObjectId("58d15323ecd08bc8f1114c90")
})
3.局部替换文档中的内容:
只更改部分字段,保留其他字段:(使用$set:)此为原来的数据:
> db.user.find({"name":"lisi"})
{ "_id" : ObjectId("58d13e8379a177687c20accc"), "name" : "lisi", "age" : "25", "height" : "1.60", "weight" : "50kg" }
更改weight的字段:
> db.user.update({"name":"lisi"},{$set:{"weight":"55kg"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.find({"name":"lisi"})
{ "_id" : ObjectId("58d13e8379a177687c20accc"), "name" : "lisi", "age" : "25", "height" : "1.60", "weight" : "55kg" }
4.$set:找不到就新增:
如果找不到要修改的字段,则新增这个字段:
> db.user.update({"name":"lisi"},{$set:{"sex":"man"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
最后可以看到weight变成了55kg,且新增的字段sex为man:
> db.user.find({"name":"lisi"})
{ "_id" : ObjectId("58d13e8379a177687c20accc"), "name" : "lisi", "age" : "25", "height" : "1.60", "weight" : "55kg", "sex" : "man" }
5.$unset:删除某一个字段:
> db.user.find({"name":"lisi"})
{ "_id" : ObjectId("58d13e8379a177687c20accc"), "name" : "lisi", "age" : "25", "height" : "1.60", "weight" : "55kg", "sex" : "man" }
> db.user.update({"name":"lisi"},{$unset:{"weight":"55kg"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.find({"name":"lisi"})
{ "_id" : ObjectId("58d13e8379a177687c20accc"), "name" : "lisi", "age" : "25", "height" : "1.60", "sex" : "man" }
6.$set新建数组,并在数组的末尾新增$push一个元素
> db.user.find({"name":"zhangsan"})
{ "_id" : ObjectId("58d13e7d79a177687c20accb"), "name" : "zhangsan", "age" : "26" }
> db.user.update({"name":"zhangsan"},{$set:{group:[3,4]}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.find({"name":"zhangsan"})
{ "_id" : ObjectId("58d13e7d79a177687c20accb"), "name" : "zhangsan", "age" : "26", "group" : [ 3, 4 ] }
> db.user.update({"name":"zhangsan"},{$push:{group:5}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.find({"name":"zhangsan"})
{ "_id" : ObjectId("58d13e7d79a177687c20accb"), "name" : "zhangsan", "age" : "26", "group" : [ 3, 4, 5 ] }
7.通过使用$push和$each一次推送多个值
> db.user.update({"name":"zhangsan"},{$push:{group:{$each:[5,6,7,8]}}})
> db.user.find({"name":"zhangsan"})
{ "_id" : ObjectId("58d13e7d79a177687c20accb"), "name" : "zhangsan", "age" : "26", "group" : [ 3, 4, 5, 5, 6, 7, 8 ] }
8.替换数组内的元素
> db.user.find({"name":"zhangsan"})
{ "_id" : ObjectId("58d13e7d79a177687c20accb"), "name" : "zhangsan", "age" : "26", "group" : [ 3, 4, 5, 5, 6, 7, 8 ] }
> db.user.update({"name":"zhangsan"},{$set:{"group.1":1}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.find({"name":"zhangsan"})
{ "_id" : ObjectId("58d13e7d79a177687c20accb"), "name" : "zhangsan", "age" : "26", "group" : [ 3, 1, 5, 5, 6, 7, 8 ] }
9.查询更新文档:
> db.runCommand({"getLastError":1})
{
"connectionId" : 3,
"n" : 0,
"syncMillis" : 0,
"writtenTo" : null,
"err" : null,
"ok" : 1
}
“n”表示更新文档的数量
参考文献:
http://blog.csdn.net/xiaoxing598/article/details/54233044#t11