MongDB

1.linux下安装mongodb

       简介:

 

      1.1  在线下载  wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.2.9.tgz

      1.2 官网下载   https://www.mongodb.com/download-center?ct=atlasheader#atlas

安装(在线安装):

1:进入/opt目录下

cd /opt

2:创建文件夹

mkdir mongodb

3:进入mongodb目录下,在线下载mongodb资源安装包

cd mongodb/
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.2.9.tgz

4:解压mongodb资源包到当前路径下

   tar  -zvxf mongodb-linux-x86_64-3.2.9.tgz

5:进入目录/usr/local/下,并创建文件夹mongodb

cd /usr/local
mkdir mongodb

6:将mongodb-linux-x86_64-3.2.9文件夹内的内容 移动到 /usr/local/mongodb下

mv /opt/mongodb/mongodb-linux-x86_64-3.2.9/*  /usr/local/mongodb

7:在mongodb目录下,新建1个文件夹和1个文件

mkdir data
touch logs

8:在系统执行文件~/.bash_profile中添加mongodb的执行命令

vim ~/.bash_profile
内容为:
    PATH=$PATH:$HOME/bin:/usr/local/mongodb/bin

source ~/.bash_profie

Mongodb启动方式

mongodb有三种启动方式:

启动1:进入/bin目录下,输入mongod --dbpath=/usr/local/mongodb/data --fork --logpath=/usr/local/mongodb/logs  

    启动之后,输入mongo就可以进入mongodb客户端连接
    然后输入show dbs;如果显示数据库,则启动搭建成功

启动2:配置启动文件启动mongodb.conf(推荐)

    mongodb.conf内容:

    logpath=/usr/local/mongodb/logs
    logappend = true 
    #fork and run in background
    fork = true
    #port = 27017
    dbpath=/usr/local/mongodb/data
    #location of pidfile
    pidfilepath=/usr/local/mongodb/mongod.pid 

之前打开数据库了,所以先关库:
可以使用db.shutdownServer()命令,或者ps -ef|grep mongod查看进程号,使用kill -9 进程号即可

启动3:配置service服务启动(推荐)

2.基本操作

  2.1 创建集合 db.chen.insert({name:'chen'});  

  2.2  删除集合数据  db.chen.remove({name:”user1”});

  2.3 db.chen.findone(); 随机返回集合中的一行数据

  2.4 db.chen.find({},{age:0});返回age字段外的所有字段

  2.5  db.user.find({tags:'tennis'},{comments:0});查找tags=tennis除去comments的所有字段

  2.6  db.user.find({userid:16},{name:1});吵着userid为16的name值

  2.7  db.chen.find({age:{$gt:30}}); 查询年龄大于30的数据

  2.8  db.chen.find({age:{$gte:30}}); 查询年龄大于等于30的数据

  2.9  db.chen.find({age:{$lt:30}}); 查询年龄小于于30的数据

  3.0  db.chen.find({age:{$lte:30}}); 查询年龄小于等于30的数据

  3.1  db.chen.find({age:{$ne:30}}); 查询年龄不等于30的数据

  3.2 db.customer.find({age:{$lt:5}}).count();  统计年龄小于5的总数量

  3.3 db.customer.find().sort({age:1}); 降序-1

  3.4 db.customer.find().sort({age:-1}).skip(2).limit(3);

  3.5 db.chen.find({name:{$exists:1}})   $exists:1表示真,指存在   $exists:0表示假,指不存在

 

创建普通索引,使用命令 db.collection.ensureIndex({key:1})

查看关于索引的相关信息,使用命令 db.collection.stats()

查看查询使用索引的情况,使用命令   db.collection.find({key:value}).explain()

删除索引,使用命令 db.collection.dropIndex({key:1})

删除集合,也会将集合中的索引全部删除

 

创建唯一索引,使用命令 db.collection.ensureIndex({key:1}{unique:true})

查看关于索引的相关信息,使用命令 db.collection.stats()

查看查询使用索引的情况,使用命令   db.collection.find({key:value}).explain()

删除索引,使用命令 db.collection.dropIndex({key:1})

删除集合,也会将集合中的索引全部删除

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值