简介
MongoDB 将数据记录作为文档存储在集合中。数据库存储一个或多个文档集合。(所以要先创建集合然后在集合内创建数据文档)。 如果集合不存在,MongoDB 会在您首次存储该集合的数据时创建该集合。 存储结构为:数据库 >> 集合 >> 文档。 数据类型:BSON。文档的数据结构和 JSON 基本一样。所有存储在集合中的数据都是 BSON 格式。BSON 是一种类似 JSON 的二进制形式的存储格式,是 Binary JSON 的简称。
Linux 安装、卸载
点击这里进入官网下载安装入口
vim /etc/yum.repos.d/mongodb-org-5.0.repo
[mongodb-org-5.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/5.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-5.0.asc
yum install -y mongodb-org
service mongod stop
yum erase $(rpm -qa | grep mongodb-org)
rm -r /var/log/mongodb
rm -r /var/lib/mongo
介绍
默认配置
1、默认情况下,MongoDB 使用mongod用户帐户运行并使用以下默认目录。包管理器在安装期间创建默认目录。所有者和组名称是mongod
/var/lib/mongo(数据目录)
/var/log/mongodb(日志目录
drwxr-xr-x 2 mongod mongod 4096 Jan 28 01:40 mongo
2、配置文件路径 /etc/mongod.conf
自定义配置
1、如果要使用默认目录以外的数据目录和/或日志目录:
创建一个或多个新目录。 编辑配置文件/etc/mongod.conf 并相应地修改以下字段:
storage.dbPath指定新的数据目录路径(例如/some/data/directory)
systemLog.path指定新的日志文件路径(例如/some/log/directory/mongod.log)
确保运行 MongoDB 的用户有权访问一个或多个目录:
sudo chown -R mongod:mongod
如果更改运行 MongoDB 进程的用户,则必须 授予新用户对这些目录的访问权限.
启动
systemctl start mongod
systemctl status mongod
systemctl enable mongod
systemctl stop mongod
systemctl restart mongod
/var/lib/mongosh
使用
进入到 /usr/bin 目录下使用 或者配置环境变量后使用 Tips:MongoDB 中默认的数据库为 test,如果你没有创建新的数据库,集合将存放在 test 数据库中。
$ mongosh
$ test> show dbs
admin 41 kB
config 49.2 kB
local 41 kB
$ db
test
$ test> use hello
switched to db hello
$ hello> db.dropDatabase()
{ ok: 1, dropped: 'hello' }
$ hello> show collections
collection1
$ hello> db.createCollection("one")
{ ok: 1 }
$ hello> show collections
collection1
one
$ hello> db.createCollection("name1", {capped: true, size: 20, max: 2})
{ ok: 1 }
$ hello> show collections
collection1
name1
one
$ hello> show tables
collection1
$ hello> show collections
collection1
$ hello> db.collection_name.drop()
true
$ hello> db.name1.insertOne({"2": 2})
{
acknowledged: true,
insertedId: ObjectId("621ec8c81d216f37410dc0fc")
}
$ hello> db.name1.insertMany([{"3": 3}, {"4":4}])
{
acknowledged: true,
insertedIds: {
'0': ObjectId("621ec8e61d216f37410dc0fd"),
'1': ObjectId("621ec8e61d216f37410dc0fe")
}
}
hello> db.one.find()
[ { '1': 3, _id: ObjectId("621ecaee1d216f37410dc101"), en: 'en' } ]
hello> db.one.updateOne({"1":3},{$set:{"1":4, "age":1}})
{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}
hello> db.one.find()
[
{
'1': 4,
_id: ObjectId("621ecaee1d216f37410dc101"),
en: 'en',
age: 1
}
]
$ hello> db.one.find({'name': 'a1'})
[
{
_id: ObjectId("621f379867ec5338709bcebf"),
name: 'a1',
age: 120,
class: '一年级四班'
}
]
$ hello> db.one.updateOne({'name': 'a1'}, {$inc: {'age': 5}})
{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}
$ hello> db.one.find({'name': 'a1'})
[
{
_id: ObjectId("621f379867ec5338709bcebf"),
name: 'a1',
age: 125,
class: '一年级四班'
}
]
$ hello> db.one.updateOne({'name': 'a1'}, {$inc: {'age': 6}})
{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}
$ hello> db.one.find({'name': 'a1'})
[
{
_id: ObjectId("621f379867ec5338709bcebf"),
name: 'a1',
age: 131,
class: '一年级四班'
}
]
$ hello> db.one.findOneAndDelete({"1":2})
null
$ hello> db.one.findOneAndDelete({"1":1})
{ '1': 1, _id: ObjectId("621ece391d216f37410dc102") }
$ hello> db.one.remove({"name":"jony"})
{ acknowledged: true, deletedCount: 1 }
$ hello> db.one.remove({"name":"jony"})
{ acknowledged: true, deletedCount: 0 }
$ hello> db.one.deleteOne({"id":1})
{ acknowledged: true, deletedCount: 1 }
$ hello> db.one.remove({})
{ acknowledged: true, deletedCount: 2 }
| 释意 | 格式 | 示例 | 等效mysql |
| 等于 | {key: value} | db.collection_name.find({"id":1}) | where id = 1 |
| 小于 | {key: {$lt:value}} | db.collection_name.find({"id":{$lt:1}}) | where id < 1 |
| 小于等于 | {key: {$lte:value}} | db.collection_name.find({"id":{$lte:9}}) | where id <= 1 |
| 大于 | {key: {$gt:value}} | db.collection_name.find({"id":{$gt:3}}) | where id > 3 |
| 大于等于 | {key: {$gte:value}} | db.collection_name.find({"id":{$gte:3}}) | where id >= 3 |
| 不等于 | {key: {$ne:value}} | db.collection_name.find({"id":{$ne:8}}) | where id != 8 |
$ hello> db.one.find({"id":2,"name":"woli"})
[ { _id: ObjectId("621ed6f91d216f37410dc108"), id: 2, name: 'woli' } ]
$ hello> db.one.find({"id":2,"name":"woli1"})
$ hello> db.one.find()
[
{ _id: ObjectId("621ed6f91d216f37410dc108"), id: 2, name: 'woli' },
{ _id: ObjectId("621ed7791d216f37410dc109"), id: 2, name: 'heol' },
{ _id: ObjectId("621ed8411d216f37410dc10a"), id: 3, name: 'gana' },
{ _id: ObjectId("621ed8411d216f37410dc10b"), id: 4, name: 'bala' }
]
$ hello> db.one.find({$or:[{'id':2}, {'name':'woli'}]})
[
{ _id: ObjectId("621ed6f91d216f37410dc108"), id: 2, name: 'woli' },
{ _id: ObjectId("621ed7791d216f37410dc109"), id: 2, name: 'heol' }
]
$ hello> db.one.find({'id': {$lt:9}, $or:[{'id':2}, {'name':'woli'}]})
[
{ _id: ObjectId("621ed6f91d216f37410dc108"), id: 2, name: 'woli' },
{ _id: ObjectId("621ed7791d216f37410dc109"), id: 2, name: 'heol' }
]
$ hello> db.one.find({'id':{$type: 'int'}})
[
{ _id: ObjectId("621ed6f91d216f37410dc108"), id: 2, name: 'woli' },
{ _id: ObjectId("621ed7791d216f37410dc109"), id: 2, name: 'heol' },
{ _id: ObjectId("621ed8411d216f37410dc10a"), id: 3, name: 'gana' },
{ _id: ObjectId("621ed8411d216f37410dc10b"), id: 4, name: 'bala' }
]
$ hello> db.one.find()
[
{ _id: ObjectId("621ed6f91d216f37410dc108"), id: 2, name: 'woli' },
{ _id: ObjectId("621ed7791d216f37410dc109"), id: 2, name: 'heol' },
{ _id: ObjectId("621ed8411d216f37410dc10a"), id: 3, name: 'gana' },
{ _id: ObjectId("621ed8411d216f37410dc10b"), id: 4, name: 'bala' }
]
$ hello> db.one.find().limit(2)
[
{ _id: ObjectId("621ed6f91d216f37410dc108"), id: 2, name: 'woli' },
{ _id: ObjectId("621ed7791d216f37410dc109"), id: 2, name: 'heol' }
]
$ hello> db.one.find({'id':{$gte:3}}).limit(2)
[
{ _id: ObjectId("621ed8411d216f37410dc10a"), id: 3, name: 'gana' },
{ _id: ObjectId("621ed8411d216f37410dc10b"), id: 4, name: 'bala' }
]
$ hello> db.one.find().limit(4).skip(3)
[ { _id: ObjectId("621ed8411d216f37410dc10b"), id: 4, name: 'bala' } ]
$ hello> db.one.find().limit(4).skip(2)
[
{ _id: ObjectId("621ed8411d216f37410dc10a"), id: 3, name: 'gana' },
{ _id: ObjectId("621ed8411d216f37410dc10b"), id: 4, name: 'bala' }
]
$ hello> db.one.find().sort({'id':1})
[
{ _id: ObjectId("621ed6f91d216f37410dc108"), id: 2, name: 'woli' },
{ _id: ObjectId("621ed7791d216f37410dc109"), id: 2, name: 'heol' },
{ _id: ObjectId("621ed8411d216f37410dc10a"), id: 3, name: 'gana' },
{ _id: ObjectId("621ed8411d216f37410dc10b"), id: 4, name: 'bala' }
]
$ hello> db.one.find().sort({'id':-1})
[
{ _id: ObjectId("621ed8411d216f37410dc10b"), id: 4, name: 'bala' },
{ _id: ObjectId("621ed8411d216f37410dc10a"), id: 3, name: 'gana' },
{ _id: ObjectId("621ed6f91d216f37410dc108"), id: 2, name: 'woli' },
{ _id: ObjectId("621ed7791d216f37410dc109"), id: 2, name: 'heol' }
]
hello> db.one.aggregate([{$group:{_id: '$id', co: {$sum: 1}}}])
[ { _id: 2, co: 2 }, { _id: 4, co: 1 }, { _id: 3, co: 1 } ]
hello> db.one.find().pretty()
[
{
_id: ObjectId("621ed6f91d216f37410dc108"),
id: 2,
name: 'woli',
age: 9,
sex: '男'
},
{
_id: ObjectId("621ed7791d216f37410dc109"),
id: 2,
name: 'heol',
age: 10,
sex: '男'
},
{
_id: ObjectId("621ed8411d216f37410dc10a"),
id: 3,
name: 'gana',
age: 13,
sex: '男'
},
{
_id: ObjectId("621ed8411d216f37410dc10b"),
id: 4,
name: 'bala',
age: 6,
sex: '女'
}
]
hello> db.one.aggregate([{$group: {_id: '$sex', max_age: {$max: '$age'} }}])
[ { _id: '女', max_age: 6 }, { _id: '男', max_age: 13 } ]
hello> db.one.aggregate([{$group: {_id: '$sex', age_list: {$push: '$age'} }}])
[
{ _id: '女', age_list: [ 6 ] },
{ _id: '男', age_list: [ 9, 10, 13 ] }
]
hello> db.one.aggregate([{$group: {_id: '$id', sex_list: {$addToSet: '$sex'}, name_lsit: {$push: '$name'} }}])
[
{ _id: 2, sex_list: [ '男' ], name_lsit: [ 'woli', 'heol' ] },
{ _id: 4, sex_list: [ '女' ], name_lsit: [ 'bala' ] },
{ _id: 3, sex_list: [ '男' ], name_lsit: [ 'gana' ] }
]
hello> db.one.aggregate( [{$match: {age: {$gt: 5, $lt: 9}}}, {$group: {_id: '$id', count: {$push: '$age'} } }] )
[ { _id: 4, count: [ 6 ] } ]
hello> db.one.find({name: {$regex: 'w'}})
[
{
_id: ObjectId("621ed6f91d216f37410dc108"),
id: 2,
name: 'woli',
age: 9,
sex: '男'
}
]
hello> db.one.find({name:/w/})
[
{
_id: ObjectId("621ed6f91d216f37410dc108"),
id: 2,
name: 'woli',
age: 9,
sex: '男'
}
]
|