mongoDB的基本使用方法
MongoDB
安装(乌班图系统)
apt install mongodb
mongoDB与sql的对比
SQL术语/概念 | MongoDB术语/概念 | 解释/说明 |
---|---|---|
database | database | 数据库 |
table | collection | 数据库表/集合 |
row | document | 数据记录行/文档 |
column | field | 数据字段/域 |
index | index | 索引 |
table joins | 表连接,MongoDB不支持 | |
primary key | primary key | 主键,MongoDB自动将_id字段设置为主键 |
连接
命令连接:mongo
zj@zj-Lenovo:~$ mongo
MongoDB shell version v3.6.3
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.6.3
Server has startup warnings:
2020-06-21T08:27:21.842+0800 I STORAGE [initandlisten]
2020-06-21T08:27:21.842+0800 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2020-06-21T08:27:21.842+0800 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem
2020-06-21T08:27:27.401+0800 I CONTROL [initandlisten]
2020-06-21T08:27:27.401+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2020-06-21T08:27:27.401+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2020-06-21T08:27:27.401+0800 I CONTROL [initandlisten]
>
标准 URI 连接语法:
mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]
创建数据库
use database_name
例如:命令行执行
> use test
switched to db test
删除数据库
db.dropDatabase()
:删除当前数据库
例如:命令行执行
> use test
switched to db test
> db.dropDatabase()
{ "dropped" : "test", "ok" : 1 }
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
创建集合
db.createCollection("name",options)
:先创建集合
> db.createCollection("title")
{ "ok" : 1 }
db.collection.insert()
:直接创建并插入数据
> db.title.insert({"name":"mongoDB"})
WriteResult({ "nInserted" : 1 })
> db.title.find()
{ "_id" : ObjectId("5eeec83e9a6da52bb561a3a5"), "name" : "mongoDB" }
删除集合
db.collection.drop()
> db.title.drop()
true
插入文档
db.collection.insert()
> db.title.insert({"name":"mongoDB"})
WriteResult({ "nInserted" : 1 })
> db.title.find()
{ "_id" : ObjectId("5eeec83e9a6da52bb561a3a5"), "name" : "mongoDB" }
db.collection.save()
> db.title.save({"name":"mysql"})
WriteResult({ "nInserted" : 1 })
> db.title.find()
{ "_id" : ObjectId("5eeec83e9a6da52bb561a3a5"), "name" : "mongoDB" }
{ "_id" : ObjectId("5eeeca769a6da52bb561a3a6"), "name" : "mysql" }
更新文档
db.collection.update({query},{$set:{query}})
:默认只更新匹配条件的一条记录,如要更新全部,设置选项multi:true
例如:更新一条记录
> db.title.find()
{ "_id" : ObjectId("5eeec83e9a6da52bb561a3a5"), "name" : "mongoDB" }
{ "_id" : ObjectId("5eeeca769a6da52bb561a3a6"), "name" : "mysql" }
{ "_id" : ObjectId("5eeecb6e9a6da52bb561a3a7"), "name" : "mysql" }
{ "_id" : ObjectId("5eeecb719a6da52bb561a3a8"), "name" : "mysql" }
> db.title.update({"name":"mysql"},{$set:{"name":"redis"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.title.find()
{ "_id" : ObjectId("5eeec83e9a6da52bb561a3a5"), "name" : "mongoDB" }
{ "_id" : ObjectId("5eeeca769a6da52bb561a3a6"), "name" : "redis" }
{ "_id" : ObjectId("5eeecb6e9a6da52bb561a3a7"), "name" : "mysql" }
{ "_id" : ObjectId("5eeecb719a6da52bb561a3a8"), "name" : "mysql" }
更新多条
> db.title.find()
{ "_id" : ObjectId("5eeec83e9a6da52bb561a3a5"), "name" : "mongoDB" }
{ "_id" : ObjectId("5eeeca769a6da52bb561a3a6"), "name" : "redis" }
{ "_id" : ObjectId("5eeecb6e9a6da52bb561a3a7"), "name" : "mysql" }
{ "_id" : ObjectId("5eeecb719a6da52bb561a3a8"), "name" : "mysql" }
> db.title.update({"name":"mysql"},{$set:{"name":"redis"}},{multi:true})
WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })
> db.title.find()
{ "_id" : ObjectId("5eeec83e9a6da52bb561a3a5"), "name" : "mongoDB" }
{ "_id" : ObjectId("5eeeca769a6da52bb561a3a6"), "name" : "redis" }
{ "_id" : ObjectId("5eeecb6e9a6da52bb561a3a7"), "name" : "redis" }
{ "_id" : ObjectId("5eeecb719a6da52bb561a3a8"), "name" : "redis" }
删除文档
db.collection.remove(query,justOne)
:默认删除匹配条件的所有记录,仅要删除一条,设置选项justOne:true或1
例如:删除name=redis的所有记录
> db.title.remove({"name":"redis"})
WriteResult({ "nRemoved" : 3 })
> db.title.find()
{ "_id" : ObjectId("5eeec83e9a6da52bb561a3a5"), "name" : "mongoDB" }
例如:仅删除符合条件的一条记录
> db.title.find()
{ "_id" : ObjectId("5eeec83e9a6da52bb561a3a5"), "name" : "mongoDB" }
{ "_id" : ObjectId("5eeecd339a6da52bb561a3a9"), "name" : "mysql" }
{ "_id" : ObjectId("5eeecd349a6da52bb561a3aa"), "name" : "mysql" }
{ "_id" : ObjectId("5eeecd359a6da52bb561a3ab"), "name" : "mysql" }
> db.title.remove({"name":"mysql"},true)
WriteResult({ "nRemoved" : 1 })
> db.title.find()
{ "_id" : ObjectId("5eeec83e9a6da52bb561a3a5"), "name" : "mongoDB" }
{ "_id" : ObjectId("5eeecd349a6da52bb561a3aa"), "name" : "mysql" }
{ "_id" : ObjectId("5eeecd359a6da52bb561a3ab"), "name" : "mysql" }
db.collection.remove({})
:删除所有记录,类似常规 SQL 的 truncate 命令
> db.title.remove({})
WriteResult({ "nRemoved" : 3 })
> db.title.find()
查询文档
db.collection.find()
> db.title.find()
{ "_id" : ObjectId("5eeec83e9a6da52bb561a3a5"), "name" : "mongoDB" }
{ "_id" : ObjectId("5eeecd349a6da52bb561a3aa"), "name" : "mysql" }
{ "_id" : ObjectId("5eeecd359a6da52bb561a3ab"), "name" : "mysql" }
db.collection.find().pretty()
:以人们较易阅读的方式显示查询结果
> db.title.find().pretty()
{ "_id" : ObjectId("5eeed9269a6da52bb561a3ac"), "name" : "mysql" }
{ "_id" : ObjectId("5eeed9289a6da52bb561a3ad"), "name" : "mysql" }
{ "_id" : ObjectId("5eeed9339a6da52bb561a3ae"), "name" : "redis" }
{ "_id" : ObjectId("5eeeda6a9a6da52bb561a3af"), "name" : "redis" }
{
"_id" : ObjectId("5eeeda959a6da52bb561a3b0"),
"name" : "redis",
"country" : "china"
}
MongoDB 与 RDBMS Where 语句比较
如果你熟悉常规的 SQL 数据,通过下表可以更好的理解 MongoDB 的条件语句查询:
操作 | 格式 | 范例 | RDBMS中的类似语句 |
---|---|---|---|
等于 | {key:value} | db.col.find({“by”:”mongoDB”}).pretty() | where by = ‘mongoDB’ |
小于 | {key:{$lt:value}} | db.col.find({“likes”:{$lt:50}}).pretty() | where likes < 50 |
小于或等于 | {key:{$lte:value}} | db.col.find({“likes”:{$lte:50}}).pretty() | where likes <= 50 |
大于 | {key:{$gt:value}} | db.col.find({“likes”:{$gt:50}}).pretty() | where likes > 50 |
大于或等于 | {key:{$gte:value}} | db.col.find({“likes”:{$gte:50}}).pretty() | where likes >= 50 |
不等于 | {key:{$ne:value}} | db.col.find({“likes”:{$ne:50}}).pretty() | where likes != 50 |
and | {key1:value1, key2:value2} | db.col.find({“name”:”mongoDB”, “age”:17}).pretty() | where name=”mongoDB” and age=17 |
or | {$or:[{“key1″:”value1”},{“key2”: “value2”}]} | db.col.find({$or:[{“name”:”mongDB教程”},{“title”: “MongoDB”}]}).pretty() | where name=”mongDB教程” or title=”MongoDB” |
Limit与Skip方法
db.collection.find().limit(number)
例如:查询title集合中的前两条记录
> db.title.find().limit(2)
{ "_id" : ObjectId("5eeed9269a6da52bb561a3ac"), "name" : "mysql" }
{ "_id" : ObjectId("5eeed9289a6da52bb561a3ad"), "name" : "mysql" }
db.collection.find().limit(number).skip(number)
例如:跳过title集合中的第一条记录,查出两条记录
> db.title.find().limit(2).skip(1)
{ "_id" : ObjectId("5eeed9289a6da52bb561a3ad"), "name" : "mysql" }
{ "_id" : ObjectId("5eeed9339a6da52bb561a3ae"), "name" : "redis" }
排序
在 MongoDB 中使用 sort() 方法对数据进行排序,sort() 方法可以通过参数指定排序的字段,并使用 1 和 -1 来指定排序的方式,其中 1 为升序排列,而 -1 是用于降序排列。db.COllection.find().sort({key:1})
> db.title.find().sort({"likes":1})
{ "_id" : ObjectId("5eef0c5d9a6da52bb561a3b3"), "name" : "mongoDB", "likes" : 10 }
{ "_id" : ObjectId("5eef0c399a6da52bb561a3b1"), "name" : "mysql", "likes" : 12 }
{ "_id" : ObjectId("5eef0c489a6da52bb561a3b2"), "name" : "redis", "likes" : 32 }
创建索引
db.collection.createIndex(keys, options)
语法中 Key 值为你要创建的索引字段,1 为指定按升序创建索引,如果你想按降序来创建索引指定为 -1 即可。
> db.title.createIndex({"likes":-1})
聚合函数
db.collection.aggregate()
:类似于sql语句中的count(*)