Mongodb安装和简单操作

文章目录

  • 1.安装服务端
    • 1.1 官网下载,解压安装
    • 1.2 配置启动
  • 2.安装客户端
  • 2.1 MongoDB Shell下载安装
  • 2.2 连接服务
  • 3.操作
    • 3.1 创建数据库
  • Create a New Database and Collection
    • 3.1 Insert Documents
    • 3.2 Query Documents
    • 3.3 Update Documents
    • 3.4 Delete Documents

1.安装服务端

1.1 官网下载,解压安装

mogodb下载地址

# 下载
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-7.0.11.tgz
# 解压
tar -zxvf mongodb-linux-x86_64-rhel70-7.0.11.tgz 
# 修改包名
mv mongodb-linux-x86_64-rhel70-7.0.11 mongodb
# 配置环境变量
export MONGODB_HOME=/opt/software/mongo
export PATH=$MONGODB_HOME/bin:$PATH
# 使环境变量生效
source /etc/profile

1.2 配置启动

默认情况下 MongoDB 启动后会初始化以下两个目录:
数据存储目录:/var/lib/mongodb
日志文件目录:/var/log/mongodb

#创建目录
sudo mkdir -p /var/lib/mongo
sudo mkdir -p /var/log/mongodb
# 设置权限
sudo chown `whoami` /var/lib/mongo     
sudo chown `whoami` /var/log/mongodb 
# 启动
mongod --dbpath /var/lib/mongo --logpath /var/log/mongodb/mongod.log --fork
# 查看日志
tail -10f /var/log/mongodb/mongod.log

2.安装客户端

MongoShell安装参考地址

2.1 MongoDB Shell下载安装

# 下载
wget https://downloads.mongodb.com/compass/mongosh-2.2.6-linux-x64.tgz
# 解压
tar -zxvf mongosh-2.2.6-linux-x64.tgz
# 授权
chmod +x bin/mongosh
# 移动以下两个文件到以下路径下
sudo cp mongosh /usr/local/bin/
sudo cp mongosh_crypt_v1.so /usr/local/lib/
# 软链接
sudo ln -s $(pwd)/bin/* /usr/local/bin/

2.2 连接服务

mongosh "mongodb://localhost:28015"
mongosh "mongodb://mongodb0.example.com:28015" --username alice --authenticationDatabase admin

3.操作

3.1 创建数据库

Create a New Database and Collection

use myNewDatabase
db.myCollection.insertOne( { x: 1 } );

use maps
switched to db maps
maps> 
maps> db.maps.insertOne({x:1,y:1})
{
  acknowledged: true,
  insertedId: ObjectId('665bb7f84323849c6fa26a13')
}

3.1 Insert Documents

The MongoDB shell provides the following methods to insert documents into a collection:
To insert a single document, use db.collection.insertOne().

 db.music.insertOne({singer:"xxx",name:"do it",type:"popular"})
{
  acknowledged: true,
  insertedId: ObjectId('665bb96b4323849c6fa26a14')
}

To insert multiple documents, use db.collection.insertMany().

db.music.insertMany([{singer:"yyy",name:"love it",type:"popular"},{singer:"zzz",name:"forget it",type:"popular"}])
{
  acknowledged: true,
  insertedIds: {
    '0': ObjectId('665bb9e04323849c6fa26a16'),
    '1': ObjectId('665bb9e04323849c6fa26a17')
  }
}

3.2 Query Documents

db.music.find()

db.music.find()
[
  {
    _id: ObjectId('665bb96b4323849c6fa26a14'),
    singer: 'xxx',
    name: 'do it',
    type: 'popular'
  },
  {
    '0': { singer: 'yyy', name: 'love it', type: 'popular' },
    '1': { singer: 'zzz', name: 'forget it', type: 'popular' },
    _id: ObjectId('665bb9ca4323849c6fa26a15')
  },
  {
    _id: ObjectId('665bb9e04323849c6fa26a16'),
    singer: 'yyy',
    name: 'love it',
    type: 'popular'
  },
  {
    _id: ObjectId('665bb9e04323849c6fa26a17'),
    singer: 'zzz',
    name: 'forget it',
    type: 'popular'
  }
]

3.3 Update Documents

db.music.updateOne({singer:"xxx"},{$set:{type:"sad"}})
{
  acknowledged: true,
  insertedId: null,
  matchedCount: 1,
  modifiedCount: 1,
  upsertedCount: 0
}
maps> db.music.find({singer:"xxx"})
[
  {
    _id: ObjectId('665bb96b4323849c6fa26a14'),
    singer: 'xxx',
    name: 'do it',
    type: 'sad'
  }
]

3.4 Delete Documents

db.music.deleteOne( { singer: "xxx" } )
{ acknowledged: true, deletedCount: 1 }
maps> db.music.find()
[
  {
    '0': { singer: 'yyy', name: 'love it', type: 'popular' },
    '1': { singer: 'zzz', name: 'forget it', type: 'popular' },
    _id: ObjectId('665bb9ca4323849c6fa26a15')
  },
  {
    _id: ObjectId('665bb9e04323849c6fa26a16'),
    singer: 'yyy',
    name: 'love it',
    type: 'popular'
  },
  {
    _id: ObjectId('665bb9e04323849c6fa26a17'),
    singer: 'zzz',
    name: 'forget it',
    type: 'popular'
  }
]
上一篇:SpringBoot如何缓存方法返回值?


下一篇:HTML跳动的爱心