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





