centos7中安装mongod-4.2
tar -zxvf mongodb-linux-x86_64-rhel70-4.2.1.tgz -C /usr/local/
cd /usr/local/
mv mongodb-linux-x86_64-rhel70-4.2.1 mongodb
cd mongodb
mkdir data log
mkdir -p /var/run/mongodb
vi mongod.conf,内容如下
# mongod.conf
# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /usr/local/mongodb/log/mongod.log
# Where and how to store data.
storage:
dbPath: /usr/local/mongodb/data
journal:
enabled: true
# engine:
# wiredTiger:
# how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /var/run/mongodb/mongod.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo
# network interfaces
net:
port: 27017
bindIp: 0.0.0.0 # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
#security:
#operationProfiling:
#replication:
#sharding:
## Enterprise-Only Options
#auditLog
#snmp:
启动服务端
cd /usr/local/mongod/bin
./mongod -f ../mongod.conf
启动客户端cli
./mongo --host 127.0.0.1 --port 27017
关闭服务端
use admin
db.shutdownServer()
ps -ef |grep mongod | grep -v grep|awk '{print $2}'
killall mongod
kill -2 <pid>
kill <pid>
#查看所有db
show dbs
#查看当前db
db
#查看表
show tables
show collections
#创建/切换数据库(没有则新建)
use mydb
#创建表
db.createCollection("t_student")
#插入一些数据
db.t_student.insert({"name":"张三","score":78,"sex":1,"birthday":"1993-03-23","create_time":"2019-11-02 10:20:20"});
db.t_student.insert({name:"李四","score":85,"sex":1,"birthday":"1993-04-23","create_time":"2019-11-06 10:20:20"});
db.t_student.insert({name:"王五","score":90,"sex":1,"birthday":"1992-04-20","create_time":"2019-11-10 10:20:20"});
db.t_student.insert({name:"陈六","score":92,"sex":2,"birthday":"1994-04-22","create_time":"2019-12-02 10:20:20"});
db.t_student.insert({name:"张三丰","score":99,"sex":1,"birthday":"1989-05-20","create_time":"2019-10-02 10:20:20"});
#查询
db.t_student.find()
db.t_student.find().pretty()
#删除表
db.t_student.drop()
#删除数据库
use mydb
db.dropDatabase()
#模糊查询 like
db.getCollection('t_student').find({'name':{$regex:/张/}})
db.getCollection('t_student').find({'name':/张/})
db.getCollection('t_student').find({'name':/zhang/i})
#模糊查询 not like
db.getCollection('t_student').find({'name':{$not:/张/}})
#按时间范围段查询
db.getCollection('t_student').find({
'create_time': {$gte: '2019-11-01 00:00:00',$lte:'2019-11-30 23:59:59'}
})
#按时间范围段查询, 分页查询
db.getCollection('t_student').find({
'create_time': {$gte: '2019-11-01 00:00:00',$lte:'2019-11-30 23:59:59'}
}).skip(0).limit(10)
#按时间范围段查询,按时间倒序排序,分页查询
db.getCollection('t_student').find({
'create_time': {$gte: '2019-11-01 00:00:00',$lte:'2019-11-30 23:59:59'}
}).sort({create_time:-1}).skip(0).limit(10)
#删除一条
db.t_student.deleteOne({name:'张三'})
#删除多条
db.t_student.deleteMany({sex:1})
#删除所有
db.t_student.deleteMany()
#更新数据
db.t_student.update({'name':'张三'},{$set:{'score':79}},{'multi':true})
#save指定_id字段覆盖更新
db.t_student.save({"_id" : ObjectId("5de77e7ce57719ec0a93212a"),"name":"张三","score":77,"sex":1,"birthday":"1993-03-23"});


阅读排行


Copyright © 叮叮声的奶酪 版权所有
备案号:鄂ICP备17018671号-1