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
}
















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: MongoDB是一种流行的NoSQL数据库,它是一个开源的文档数据库,可以在Linux操作系统上运行。MongoDB具有高可扩展性、高性能、灵活的数据模型和易于使用的API等优点,因此被广泛应用于Web应用程序、大数据和实时分析等领域。在Linux上安装和配置MongoDB非常简单,只需要下载安装包并按照官方文档进行操作即可。 ### 回答2: MongoDB是一个非关系型数据库管理系统,而Linux是一种开源操作系统。在使用MongoDB时,可以选择在Linux系统上进行安装和部署。 首先,MongoDB的官方提供了针对Linux的安装包,可以根据所使用的Linux版本选择对应的安装包进行安装。安装过程相对简单,只需下载安装包并进行解压,然后设置环境变量即可。 一旦安装完毕,就可以在Linux系统中启动MongoDB,通过命令行或者配置文件指定相关参数来启动。在启动MongoDB后,可以使用命令行工具或者客户端连接到数据库,并进行数据增删改查操作。 在Linux系统中,MongoDB的性能现良好,能够充分利用Linux操作系统的优势。Linux提供了强大的文件系统和多线程支持,可以提高MongoDB数据处理能力和并发性能。 此外,Linux系统还提供了丰富的监控和调优工具,可以帮助用户监控和管理MongoDB的运行状态,以及对其进行性能优化。用户可以使用Linux系统自带的命令和工具,或者通过第三方的开源软件来完成这些任务。 总之,MongoDBLinux系统上的安装和使用非常方便,而Linux操作系统可以提供良好的环境和支持,使得MongoDB能够充分发挥其功能和性能优势。 ### 回答3: MongoDB是一种高性能的非关系型数据库,它以JSON格式存储数据,并且具有灵活的数据模型。 MongoDBLinux系统上运行良好,因为Linux作为开源操作系统,提供了高度的兼容性和灵活性。在Linux上安装MongoDB非常简单,只需下载MongoDB的二进制文件,解压缩并设置PATH即可。 使用MongoDBLinux上的好处之一是它对Linux的特性进行了利用。Linux系统支持高度的并发性和多线程,这对于处理大量数据请求非常重要。MongoDB能够充分利用Linux的多线程处理能力,从而提供更好的性能和响应速度。 此外,MongoDB也可以与Linux的其他工具和技术集成,使其更强大和灵活。例如,可以使用Linux的shell命令和脚本与MongoDB进行交互,从而实现更高级的操作和管理。 与此同时,Linux还提供了丰富的监控和调试工具,可以帮助我们更好地监视和分析MongoDB的性能。这些工具可以帮助我们实时监控数据库的运行情况,识别潜在的问题,并对数据库进行优化。 总之,MongoDBLinux上是一个优秀的选择。它能够充分利用Linux的优势,提供高性能和灵活性。无论是在开发环境还是生产环境中,MongoDBLinux上都可以发挥出色的作用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值