MongoDB安装与基础操作

MongoDB安装与操作指南
本文介绍了MongoDB的安装与操作方法。安装时需将安装包解压到soft目录,在mongodb层创建文件夹并配置mongo.conf文件。操作方面,涵盖启动client端、查看和创建库、插入和查询数据、修改和删除数据等命令,还提供了查看json格式错误的网址。

安装

安装包解压到soft目录

[root@kb141 install] tar -zxf mongodb-linux-x86_64-rhel70-4.0.24.tgz -C ./
[root@kb141 install] mv mongodb-linux-x86_64-rhel70-4.0.24 ../soft
[root@kb141 soft] mv mongodb-linux-x86_64-rhel70-4.0.24/ mongodb

在mongodb层创建文件夹 配置mongo.conf文件

[root@kb141 mongodb] mkdir -p ./data/db
[root@kb141 mongodb] mkdir -p ./log
[root@kb141 mongodb] vim ./mongo.conf



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

操作

启动client端命令

[root@kb141 mongodb]# ./bin/mongo

查看库命令

show dbs
> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB

创建库

> use kgcmongodb
switched to db kgcmongodb
> db
kgcmongodb
> db.createCollection("kb22")
{ "ok" : 1 }
> show collections
kb22
> show dbs(用show databases)
admin   0.000GB
config  0.000GB
local   0.000GB
> use kgcmongodb
switched to db kgcmongodb
> db
kgcmongodb

创建

> db.createCollection("kb22")
{ "ok" : 1 }
> show collections
kb22
> show tables
kb22

删除库

db.dropDatabase()

查看json格式是否错误网址:
https://www.sojson.com/simple_json.html

插入数据

db.comment.insert({"title":"aa","content":"good","name":"a1","userid":"zz","nick":"er"})

查询数据

db.comment.find()
{ "_id" : ObjectId("64d60308162308b01acfffe7"), "title" : "aa", "content" : "good", "name" : "a1", "userid" : "zz", "nick" : "er" }

插入多条

> db.comment.insertMany([ {"_id":"1","title":"aa","content":"good","readnum":"21","name":"a1","userid":"0001","nick":"er"}, {"_id":"2","title":"bb","content":"hi","readnum":"21","name":"b1","userid":"0002","nick":"et"}, {"_id":"3","title":"cc","content":"od","readnum":"24","name":"a1","userid":"001","nick":"er"}, {"_id":"4","title":"dd","content":"no","readnum":"22","name":"d1","userid":"0003","nick":"et"}, {"_id":"5","title":"ee","content":"yes","readnum":"22","name":"e1","userid":"0004","nick":"se"} ])
{
	"acknowledged" : true,
	"insertedIds" : [
		"1",
		"2",
		"3",
		"4",
		"5"
	]
}

查看命令

> db.comment.find()
{ "_id" : ObjectId("64d60308162308b01acfffe7"), "title" : "aa", "content" : "good", "name" : "a1", "userid" : "zz", "nick" : "er" }
{ "_id" : "1", "title" : "aa", "content" : "good", "readnum" : "21", "name" : "a1", "userid" : "0001", "nick" : "er" }
{ "_id" : "2", "title" : "bb", "content" : "hi", "readnum" : "21", "name" : "b1", "userid" : "0002", "nick" : "et" }
{ "_id" : "3", "title" : "cc", "content" : "od", "readnum" : "24", "name" : "a1", "userid" : "001", "nick" : "er" }
{ "_id" : "4", "title" : "dd", "content" : "no", "readnum" : "22", "name" : "d1", "userid" : "0003", "nick" : "et" }
{ "_id" : "5", "title" : "ee", "content" : "yes", "readnum" : "22", "name" : "e1", "userid" : "0004", "nick" : "se" }
> 

条件查询

>db.comment.find({"userid":"zz"})
{ "_id" : ObjectId("64d60308162308b01acfffe7"), "title" : "aa", "content" : "good", "name" : "a1", "userid" : "zz", "nick" : "er" }

限制查询只查找一个

 >db.comment.findOne({"nick":"er"})
{
	"_id" : ObjectId("64d60308162308b01acfffe7"),
	"title" : "aa",
	"content" : "good",
	"name" : "a1",
	"userid" : "zz",
	"nick" : "er"
}

带条件查询,查询条件列为

> db.comment.find({"title":"aa"},{"title":1})
{ "_id" : ObjectId("64d60308162308b01acfffe7"), "title" : "aa" }
{ "_id" : "1", "title" : "aa" }

加条件0不显示1显示

> db.comment.find({"title":"aa"},{_id:0,"title":1})
{ "title" : "aa" }
{ "title" : "aa" }
> db.comment.find({"title":"aa"},{_id:0,"title":1,"content":1})
{ "title" : "aa", "content" : "good" }
{ "title" : "aa", "content" : "good" }

限制条件删除

>db.comment.remove({_id:"1"})

覆盖修改

>db.comment.update({_id:"1"},{"content":"very good"})
{ "_id" : "1", "content" : "very good" }

局部修改+$set: (后面可以添加 ,修改多项)

局部修改 nick:et的为100 只能匹配到1个

> db.comment.update({_id:"2"},{$set:{"content":"very happy"}})
>db.comment.update({nick:"et"},{$set:{"readnum":"100"}})

局部修改 添加,{multi:true}可对多个进行修改

>db.comment.update({nick:"et"},{$set:{"readnum":"122"}},{multi:true})
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值