mongodb查询数据库集合的基础命令

您所在的位置:网站首页 mongodb显示所有集合的命令 mongodb查询数据库集合的基础命令

mongodb查询数据库集合的基础命令

2024-02-18 10:55| 来源: 网络整理| 查看: 265

基础命令 启动mongo服务 mongod -f /usr/local/mongodb/mongod.conf //注意配置文件路径 停止mongo服务

关闭mongodb有三种方式: 一种是进入mongo后通过mongo的函数关闭;

use admin db.shutdownServer()

一种是通过mongod关闭;

mongod --shutdown --dbpath /usr/local/mongodb/

一种是直接关闭mongodb的进程(不推荐)。

//先找到进程号 ps aux | grep mongodb kill -9 进程号 创建用户 //超级用户 db.createUser({"user":"root_name","pwd":"password","roles":["root"]}) //普通用户 db.createUser("user":"user1", "pwd":"pwd1", roles:["readWrite"]) //查看用户 show users 使用账户密码登录mongo mongo -u "用户名" -p'密码' //或 mongo --host 10.10.18.11 -u "用户名" --authenticationDatabase "数据库名" -p'密码' 增删改查 help//帮助 db.help()//集合所有方法 db.stats()//集合当前状态信息 db//当前数据库db.getName() show dbs//所有数据库列表 use test//切换数据库 db.createCollection('collection1')//数据库创建集合/表 db.getCollectionNames()//获取数据库所有集合/表 db.dropDatabase()//删除当前数据库 show tables 或者 show collections//查看当前库中的表/集合 db.mycollection.drop()//删除mycollection集合

集合-查(操作文档)

db.collection1.find()//查所有 db.getCollection("collection1").find() db.collection1.find({'字段名':'字段属性'})//查指定的文档 db.getCollection("collection1").find({'字段名':'字段属性'}) db.collection1.find({'expect':{$exists:false}})//查expect字段不存在的文档 db.getCollection('collection1').find({'expect':{$exists:false}}) db.collection1.find({'字段1':{$exists:false},'字段2':{$exists:true}})//多字段查询 db.getCollection('collection1').find({'字段1':{$exists:false},'字段2':{$exists:true}}) db.collection1.find({post_text:{$regex:"runoob"}})//查post_text部分匹配runoob的文档 db.getCollection("collection1").find({post_text:{$regex:"runoob"}}) db.collection1.find({'people.name':‘kj’})//name是people的⼦字段。查找所有name为kj的⽂档 db.getCollection('collection1').find({'people.name':‘kj’}) db.collection1.find().count()//查数量 db.getCollection("collection1").find().count() db.collection1.find().sort({})//先排序 db.getCollection("collection1").find().sort({}) db.collection1.find().limit(3).skip(3)//跳过3条查前3条 db.getCollection("collection1").find().limit(3).skip(3) db.collection1.find({$or:[{age:21},{age:22}]})//$or二选一 db.getCollection("collection1").find({$or:[{age:21},{age:22}]}) db.collection1.find().findOne()//查第一条 db.getCollection("collection1").find().findOne() db.collection1.find({name:'kj4'})//查kj4 db.getCollection("collection1").find({name:'kj4'}) db.collection1.find({age:{$lt:20}})//查小于20 db.getCollection("collection1").find({age:{$lt:20}}) db.collection1.find({age:{$gt:20}})//查大于20 db.getCollection("collection1").find({age:{$gt:20}}) db.collection1.find({age:{$lte:20}})//查小于等于20 db.getCollection("collection1").find({age:{$lte:20}}) db.collection1.find({age:{$gte:20}})//查大于等于20 db.getCollection("collection1").find({age:{$gte:20}}) db.collection1.find({age:{$gte:20,$lte:20}})//查大于等于20且小于等于20 db.getCollection("collection1").find({age:{$gte:20,$lte:20}}) db.collection1.find({name:/1/})//name中含有1 db.getCollection("collection1").find({name:/1/}) db.collection1.find({name:/^1/})//name中开头含有1 db.getCollection("collection1").find({name:/^1/}) db.collection1.find({name:/1$/})//name中结尾含有1 db.getCollection("collection1").find({name:/1$/}) db.collection1.find({},{name:1,age:0})//指定列数据1为显示该列数据,0为不显示该列数据 db.getCollection("collection1").find({},{name:1,age:0}) 我们如果需要查询同时满足两个以上条件,需要使用$and操作符将条件进行关联。(相 当于SQL的and) 如果两个以上条件之间是或者的关系,我们使用 $or 操作符进行关联 db.collection1.find({$and:[{age:{$gte:NumberInt(10)}},{age:{$lt:NumberInt(50)}}]})//查询collection1集合中age大于等于10并且小于50的文档 db.comment.find({$or:[ {userid:"1003"} ,{likenum:{$lt:1000} }]})//查询评论集合中userid为1003,或者点赞数小于1000的文档记录

集合-删(操作文档)

db.collection1.remove({name:'kj4'}) db.getCollection('API').remove({'created':{'\$gt':154, '$lt':156}}), 为删除集合“API”中‘created’字段属性在154-156之间的数据。

集合-改(操作文档)

db.collection1.update({name:'kj3'},{$set{age:22}}) 重新设置age值 db.collection1.update({name:'kj3'},{$inc{age:23}}) 在age的基础上加等于 db.collection1.update({name:'kj4'},{$inc:{age:23}},true,true) 修改两条相等的name=kj4 如果没有name=kj4 true是创建false为不创建

集合-增(操作文档)

db.collection1.insert([{name:'kj2',age:21},{age:21,name:'kj3'}]) db.collection1.save({name:'kj3',age:21}) db.runoob.insert({"name":"菜鸟就是我"})//数据库在runoob表中插入数据

在 MongoDB 中,你可以不用创建集合。当你插入一些文档时,MongoDB 会自动创建集合。如: 在这里插入图片描述

查看集合里面所有的数据,命令:db.集合名字.find()

在这里插入图片描述 MongoDB 数据库备份与恢复

mongoexport备份某个表语法格式:mongoexport --port 端口号 -d 库名 -c 表名 -o 备份文件路径.json mongoexport备份某个表csv格式:mongoexport --port 端口号 -d 库名 -c 表名 --type=csv -f 备份的字段 -o 备份文件路径.json mongoimport还原某个表json格式:mongoimport --port 26017 -d 要还原的库名-c 表名 备份文件路径.json mongoimport还原某个表csv格式:mongoimport --port 26017 -d 库名 -c 表名–type=csv --headerline 备份文件路径.csv mongodump备份库:mongodump --port 26017 -d 库名 -o 备份文件路径 mongorestore还原:mongorestore --port 26017 -d 库名 备份文件路径 --drop 压缩格式备份mongodump --port 26017 -d zabbix -o /data/backup/zabbix_db --gzip 将mysql中的表导出还原到mongodb 1\导出mysql中的表为csv格式 2\将Navicat导出包含列标题的csv传到Linux中 3\mongoimport --port 26017 -d zabbix -c users --type=csv --headerline /tmp/users.csv MongoDB - 连接

标准 URI 连接语法:

mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]

mongodb:// 这是固定的格式,必须要指定。

username:password@ 可选项,如果设置,在连接数据库服务器之后,驱动都会尝试登录这个数据库

host1 必须的指定至少一个host, host1 是这个URI唯一要填写的。它指定了要连接服务器的地址。如果要连接复制集,请指定多个主机地址。

portX 可选的指定端口,如果不填,默认为27017

/database 如果指定username:password@,连接并验证登录指定数据库。若不指定,默认打开 test 数据库。

?options 是连接选项。如果不使用/database,则前面需要加上/。所有连接选项都是键值对name=value,键值对之间通过&或;(分号)隔开

关闭mongo服务:db.shutdownServer() 启动mongo服务mongod --config /usr/local/mongodb/mongodb.conf(确定你自己mongdb.conf文件位置)

其它:Linux下MongoDB的一些默认路径

数据文件路径: /var/lib/mongodb/

日志文件路径: /var/log/mongodb/mongod.log

可执行文件路径: /usr/bin/mongo /usr/bin/mongod

mongodb的配置文件: /etc/mongd.conf



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3