mongodb linux增删改查操作


mongodb 安装

[root@hadoop103 software]# tar -zxf mongodb-linux-x86_64-rhel70-4.0.24.tgz
[root@hadoop103 software]# mv mongodb-linux-x86_64-rhel70-4.0.24 mogodb
[root@hadoop103 software]# cd mongodb

新建数据库目录: /root/software/mongodb/data/db
新建数据库日志:/root/software/mongodb/log
[root@hadoop103 mongodb]# mkdir -p ./data/db
[root@hadoop103 mongodb]# mkdir -p ./log


[root@hadoop103 mongodb]# vi ./mongo.conf

systemLog:
   destination: file
   path: "/root/software/mongodb/log/mongod.log"
   logAppend: true
storage:
   dbPath: "/root/software/mongodb/data/db"
   journal:
      enabled: true
processManagement:
   fork: true
net:
   bindIp: localhost,192.168.107.103
   port: 27017


// 启动mongo
[root@hadoop103 mongodb]# ./bin/mongod -config ./mongo.conf

//查看mongo服务是否起  不是用jps
[root@hadoop103 mongodb]# ps -ef | grep mongo

//进入到shell 
[root@hadoop103 mongodb]# ./bin/mongo

> show dbs;
admin   0.000GB
config  0.000GB
local   0.000GB
> use kb11            //新建库
switched to db kb11
> show dbs;   
admin   0.000GB
config  0.000GB
local   0.000GB
> db.createCollection("students")   //新建表  之后才能看到库       显示创建   隐式创建 直接插入数据就有表
{ "ok" : 1 }
> show dbs
admin   0.000GB
config  0.000GB
kb11    0.000GB
local   0.000GB
> use kb11 
switched to db kb11
> show tables;                  //查看表
kb09
students
teacher

> show collections				 //查看表
kb09
students
teacher

> db.students.find() 			//查看具体表内容

>db.dropDatabase("kb11")        //删除库

> db.abc.insert({"title":"aa","content":"bb","name":"cc","userid":"0001","nick":"kk"})   // 插入json字符串  掺入单条数据
WriteResult({ "nInserted" : 1 })  		//影响行数 1 
> db.abc.find()							//查看表内容 ,可以看到 _id  自动生成 也是自己写
{ "_id" : ObjectId("60b97730b96d258b1bff0910"), "title" : "aa", "content" : "bb", "name" : "cc", "userid" : "0001", "nick" : "kk" }
>db.abc.count()                   	   // 查询表里的数量
>1
> db.kb09.drop()						//删除表
true
> show dbs
admin   0.000GB
config  0.000GB
kb11    0.000GB
local   0.000GB


> use kgcdsj					//使用 kgcdsj 数据库 (如果有数据库选择,没有创建后选择)
switched to db kgcdsj
> db
kgcdsj


// 下面数据主题为文章   有两行是同一个作者   (主键,标题,内容,阅读量,名字,编号,别名)
// 隐式创建表students 插入多条数据
try{
db.students.insertMany(        
	[
		{"_id" :"1","title":"aa","content":"good","readNum":21,"name":"a1","userid":"0001","nick":"gree"},
		{"_id" :"2","title":"bb","content":"hi","readNum":28,"name":"b1","userid":"0002","nick":"ant"},
		{"_id" :"3","title":"cc","content":"ok","readNum":27,"name":"c1","userid":"0003","nick":"plan"},
		{"_id" :"4","title":"dd","content":"no","readNum":29,"name":"a1","userid":"0001","nick":"gree"},
		{"_id" :"5","title":"ee","content":"yes","readNum":22,"name":"e1","userid":"0004","nick":"dog"}
	]
)
}catch(e){
 print(e)
}

> db.students.findOne({"name":"a1"})    // 查询一条内容
{
        "_id" : "1",
        "title" : "aa",
        "content" : "good",
        "readNum" : 21,
        "name" : "a1",
        "userid" : "0001",
        "nick" : "gre
}

> db.students.find({"name":"a1")			//查询满足条件的所有内容
{ "_id" : "1", "title" : "aa", "content" : "good", "readNum" : 21, "name" : "a1", "userid" : "0001", "nick" : "gree" }
{ "_id" : "4", "title" : "dd", "content" : "no", "readNum" : 29, "name" : "a1", "userid" : "0001", "nick" : "gree" }

> db.students.find({"name":"a1","readNum":21})   //多条件查询
{ "_id" : "1", "title" : "aa", "content" : "good", "readNum" : 21, "name" : "a1", "userid" : "0001", "nick" : "gree" }



// 多条件查询显示title,title:1      _id 默认显示 不显示:_id:0
> db.students.find({"name":"a1","content":"no"},{title:1})
{ "_id" : "4", "title" : "dd" }
> db.students.find({"name":"a1","content":"no"},{"title":1})
{ "_id" : "4", "title" : "dd" }

//
> db.students.find({"name":"a1","content":"no"},{title:1,readNum:1})
{ "_id" : "4", "title" : "dd", "readNum" : 29 }
> db.students.find({"name":"a1","content":"no"},{title:1,readNum:1,_id:0})
{ "title" : "dd", "readNum" : 29 }

//第一个{}不给条件,第二个{}中给要显示的列         查询全部,只显示title,content列
> db.students.find({},{title:1,content:1})
{ "_id" : "1", "title" : "aa", "content" : "good" }
{ "_id" : "2", "title" : "bb", "content" : "hi" }
{ "_id" : "3", "title" : "cc", "content" : "ok" }
{ "_id" : "4", "title" : "dd", "content" : "no" }
{ "_id" : "5", "title" : "ee", "content" : "yes" }
> db.students.find({},{title:1,content:1,_id:0})
{ "title" : "aa", "content" : "good" }
{ "title" : "bb", "content" : "hi" }
{ "title" : "cc", "content" : "ok" }
{ "title" : "dd", "content" : "no" }
{ "title" : "ee", "content" : "yes" }


//指定主键删除
> db.students.remove({"_id":"5"})
WriteResult({ "nRemoved" : 1 })

//删除指定内容
> db.students.remove({"name":"a1"})
WriteResult({ "nRemoved" : 2 })

//根据指定多条件删除
> db.students.remove({"content":"ok","name":"c1"})
WriteResult({ "nRemoved" : 1 })

// 根据条件查询 统计数量
> db.students.find({name:"a1"}).count();
2

// 限制两行 查询出前两行            --分页操作
> db.students.find().limit(2)
{ "_id" : "1", "title" : "aa", "content" : "good", "readNum" : 21, "name" : "a1", "userid" : "0001", "nick" : "gree" }
{ "_id" : "2", "title" : "bb", "content" : "hi", "readNum" : 28, "name" : "b1", "userid" : "0002", "nick" : "ant" }

// 跨越两行  查询 3 4 行     
> db.students.find().limit(2).skip(2)
{ "_id" : "3", "title" : "cc", "content" : "ok", "readNum" : 27, "name" : "c1", "userid" : "0003", "nick" : "plan" }
{ "_id" : "4", "title" : "dd", "content" : "no", "readNum" : 29, "name" : "a1", "userid" : "0001", "nick" : "gree" }


// 对查询结果进行排序   指定排序的列,1表示升序  sort({readNum:1})     -1 表示降序 sort({readNum:-1})
> db.students.find().sort({readNum:1})
{ "_id" : "1", "title" : "aa", "content" : "good", "readNum" : 21, "name" : "a1", "userid" : "0001", "nick" : "gree" }
{ "_id" : "5", "title" : "ee", "content" : "yes", "readNum" : 22, "name" : "e1", "userid" : "0004", "nick" : "dog" }
{ "_id" : "3", "title" : "cc", "content" : "ok", "readNum" : 27, "name" : "c1", "userid" : "0003", "nick" : "plan" }
{ "_id" : "2", "title" : "bb", "content" : "hi", "readNum" : 28, "name" : "b1", "userid" : "0002", "nick" : "ant" }
{ "_id" : "4", "title" : "dd", "content" : "no", "readNum" : 29, "name" : "a1", "userid" : "0001", "nick" : "gree" }


//查询阅读量最高的作者
> db.students.find({},{name:1}).sort({readNum:-1}).limit(1)
{ "_id" : "4", "name" : "a1" }


//按userid升序 同样的userid 再按照readNum 降序
> db.students.find().sort({userid:1,readNum:-1})
{ "_id" : "4", "title" : "dd", "content" : "no", "readNum" : 29, "name" : "a1", "userid" : "0001", "nick" : "gree" }
{ "_id" : "1", "title" : "aa", "content" : "good", "readNum" : 21, "name" : "a1", "userid" : "0001", "nick" : "gree" }
{ "_id" : "2", "title" : "bb", "content" : "hi", "readNum" : 28, "name" : "b1", "userid" : "0002", "nick" : "ant" }
{ "_id" : "3", "title" : "cc", "content" : "ok", "readNum" : 27, "name" : "c1", "userid" : "0003", "nick" : "plan" }
{ "_id" : "5", "title" : "ee", "content" : "yes", "readNum" : 22, "name" : "e1", "userid" : "0004", "nick" : "dog" }


// 查询content 包含 "o" 的
> db.students.find({content:/o/})
{ "_id" : "1", "title" : "aa", "content" : "good", "readNum" : 21, "name" : "a1", "userid" : "0001", "nick" : "gree" }
{ "_id" : "3", "title" : "cc", "content" : "ok", "readNum" : 27, "name" : "c1", "userid" : "0003", "nick" : "plan" }
{ "_id" : "4", "title" : "dd", "content" : "no", "readNum" : 29, "name" : "a1", "userid" : "0001", "nick" : "gree" }

//查询content 以 "o" 开头的
> db.students.find({content:/^o/})
{ "_id" : "3", "title" : "cc", "content" : "ok", "readNum" : 27, "name" : "c1", "userid" : "0003", "nick" : "plan" }

$gt
$lt
$gte
$lte
$ne

//查询readNum阅读量 大于25 小于29 的数据
> db.students.find({readNum:{$gt:25,$lt:29}});
{ "_id" : "2", "title" : "bb", "content" : "hi", "readNum" : 28, "name" : "b1", "userid" : "0002", "nick" : "ant" }
{ "_id" : "3", "title" : "cc", "content" : "ok", "readNum" : 27, "name" : "c1", "userid" : "0003", "nick" : "plan" }
{ "_id" : "4", "title" : "dd", "content" : "no", "readNum" : 29, "name" : "a1", "userid" : "0001", "nick" : "gree" }

//查询readNum阅读量 大于25 小于30 作者名字为a1 的数据
> db.students.find({readNum:{$gt:25,$lt:30},name:"a1"});
{ "_id" : "4", "title" : "dd", "content" : "no", "readNum" : 29, "name" : "a1", "userid" : "0001", "nick" : "gree" }

$in
$or
$and


//查询userid为 0001 和 0002 的数据        $in 
> db.students.find({userid:{$in:["0001","0002"]}});
{ "_id" : "1", "title" : "aa", "content" : "good", "readNum" : 21, "name" : "a1" , "userid" : "0001", "nick" : "gree" }
{ "_id" : "2", "title" : "bb", "content" : "hi", "readNum" : 28, "name" : "b1",  "userid" : "0002", "nick" : "ant" }
{ "_id" : "4", "title" : "dd", "content" : "no", "readNum" : 29, "name" : "a1",  "userid" : "0001", "nick" : "gree" }


//查询readNum阅读量 大于21 并且 小于25    $and
> db.students.find({$and:[{readNum:{$gt:21}},{readNum:{$lt:25}}]})
{ "_id" : "5", "title" : "ee", "content" : "yes", "readNum" : 22, "name" : "e1", "userid" : "0004", "nick" : "dog" }


//查询name为c1 或者 readNum 小于25 	       $or
> db.students.find({$or:[{name:"c1"},{readNum:{$lt:25}}]})
{ "_id" : "1", "title" : "aa", "content" : "good", "readNum" : 21, "name" : "a1", "userid" : "0001", "nick" : "gree" }
{ "_id" : "3", "title" : "cc", "content" : "ok", "readNum" : 27, "name" : "c1", "userid" : "0003", "nick" : "plan" }
{ "_id" : "5", "title" : "ee", "content" : "yes", "readNum" : 22, "name" : "e1", "userid" : "0004", "nick" : "dog" }

//全部修改   查询到的数据除_id   不加$set  全部修改
> db.students.update({title:"bb"},{content:"hihihi"})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.students.find()
{ "_id" : "1", "title" : "aa", "content" : "good", "readNum" : 21, "name" : "a1", "userid" : "0001", "nick" : "gree" }
{ "_id" : "2", "content" : "hihihi" }
{ "_id" : "3", "title" : "cc", "content" : "ok", "readNum" : 27, "name" : "c1", "userid" : "0003", "nick" : "plan" }
{ "_id" : "4", "title" : "dd", "content" : "no", "readNum" : 29, "name" : "a1", "userid" : "0001", "nick" : "gree" }
{ "_id" : "5", "title" : "ee", "content" : "yes", "readNum" : 22, "name" : "e1", "userid" : "0004", "nick" : "dog" }

//局部修改 $set   
> db.students.update({title:"aa"},{$set:{content:"hihihi"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.students.find()
{ "_id" : "1", "title" : "aa", "content" : "hihihi", "readNum" : 21, "name" : "a1", "userid" : "0001", "nick" : "gree" }
{ "_id" : "2", "content" : "hihihi" }
{ "_id" : "3", "title" : "cc", "content" : "ok", "readNum" : 27, "name" : "c1", "userid" : "0003", "nick" : "plan" }
{ "_id" : "4", "title" : "dd", "content" : "no", "readNum" : 29, "name" : "a1", "userid" : "0001", "nick" : "gree" }
{ "_id" : "5", "title" : "ee", "content" : "yes", "readNum" : 22, "name" : "e1", "userid" : "0004", "nick" : "dog" }

// update 只修改一条
> db.students.update({name:"a1"},{$set:{nick:"black"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

//updateMany修改多条
> db.students.updateMany({name:"a1"},{$set:{nick:"black"}})
{ "acknowledged" : true, "matchedCount" : 2, "modifiedCount" : 1 }
> db.students.find();
{ "_id" : "1", "title" : "aa", "content" : "hihihi", "readNum" : 21, "name" : "a1", "userid" : "0001", "nick" : "black" }
{ "_id" : "2", "content" : "hihihi" }
{ "_id" : "3", "title" : "cc", "content" : "ok", "readNum" : 27, "name" : "c1", "userid" : "0003", "nick" : "plan" }
{ "_id" : "4", "title" : "dd", "content" : "no", "readNum" : 29, "name" : "a1", "userid" : "0001", "nick" : "black" }
{ "_id" : "5", "title" : "ee", "content" : "yes", "readNum" : 22, "name" : "e1", "userid" : "0004", "nick" : "dog" }

//update  后面  多加条件    multi:true   也能更改多条
> db.students.update({name:"a1$set:{nick:"tom"}},{multi:true})
WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })

//创建索引
> db.students.createIndex({userid:1})
{
        "createdCollectionAutomatically" : false,
        "numIndexesBefore" : 1,
        "numIndexesAfter" : 2,
        "ok" : 1
}

// 获取索引
> db.students.getIndexes()
[
        {
                "v" : 2,
                "key" : {
                        "_id" : 1
                },
                "name" : "_id_",
                "ns" : "kgcdsj.students"
        },
        {
                "v" : 2,
                "key" : {
                        "userid" : 1
                },
                "name" : "userid_1",
                "ns" : "kgcdsj.students"
        }
]


//
> db.students.createIndex({userid:1,readNum:-1})
{
        "createdCollectionAutomatically" : false,
        "numIndexesBefore" : 2,
        "numIndexesAfter" : 3,
        "ok" : 1
}

// 删除索引  给索引名
> db.students.dropIndex("userid_1_readNum_-1")
{ "nIndexesWas" : 3, "ok" : 1 }

// 删除索引  升序索引userid
> db.students.dropIndex({userid:1})
{ "nIndexesWas" : 2, "ok" : 1 }

// 删除所有索引
> db.students.dropIndexes()
{
        "nIndexesWas" : 2,
        "msg" : "non-_id indexes dropped for collection",
        "ok" : 1
}



---------------------------------------------------------------------------


// 根据查询计划 看出 find的时候是 "stage" : "COLLSCAN" 全局扫描
> db.students.find({userid:"0004"}).explain()
{
        "queryPlanner" : {
                "plannerVersion" : 1,
                "namespace" : "kgcdsj.students",
                "indexFilterSet" : false,
                "parsedQuery" : {
                        "userid" : {
                                "$eq" : "0004"
                        }
                },
                "winningPlan" : {
                        "stage" : "COLLSCAN",
                        "filter" : {
                                "userid" : {
                                        "$eq" : "0004"
                                }
                        },
                        "direction" : "forward"
                },
                "rejectedPlans" : [ ]
        },
        "serverInfo" : {
                "host" : "hadoop103",
                "port" : 27017,
                "version" : "4.0.24",
                "gitVersion" : "9df1b3a80f39cf7e7ccd6264a207518426a524f6"
        },
        "ok" : 1
}



// 创建一个索引之后再explain
> db.students.createIndex({userid:1})
{
        "createdCollectionAutomatically" : false,
        "numIndexesBefore" : 1,
        "numIndexesAfter" : 2,
        "ok" : 1
}


// 创建索引之后再 explain 可以看到 "stage" : "IXSCAN" 按照索引查找  加快查询速度
> db.students.find({userid:"0004"}).explain()
{
        "queryPlanner" : {
                "plannerVersion" : 1,
                "namespace" : "kgcdsj.students",
                "indexFilterSet" : false,
                "parsedQuery" : {
                        "userid" : {
                                "$eq" : "0004"
                        }
                },
                "winningPlan" : {
                        "stage" : "FETCH",
                        "inputStage" : {
                                "stage" : "IXSCAN",
                                "keyPattern" : {
                                        "userid" : 1
                                },
                                "indexName" : "userid_1",
                                "isMultiKey" : false,
                                "multiKeyPaths" : {
                                        "userid" : [ ]
                                },
                                "isUnique" : false,
                                "isSparse" : false,
                                "isPartial" : false,
                                "indexVersion" : 2,
                                "direction" : "forward",
                                "indexBounds" : {
                                        "userid" : [
                                                "[\"0004\", \"0004\"]"
                                        ]
                                }
                        }
                },
                "rejectedPlans" : [ ]
        },
        "serverInfo" : {
                "host" : "hadoop103",
                "port" : 27017,
                "version" : "4.0.24",
                "gitVersion" : "9df1b3a80f39cf7e7ccd6264a207518426a524f6"
        },
        "ok" : 1
}
















上一篇:JAVA日报


下一篇:SSM框架实战:简单图书管理与借阅系统