CRUD操作是create(创建)、read(读取)、update(更新)和delete(删除)
MongoDB不支持常规的sql的命令,但是自身有丰富的查询语言
MongoDB在插入一条数据后,会自动增加一个_id自动,作为主键,如果插入的文档省略了id字段,则会自动生产一个Object_id字段
MongoDB默认有4个数据库:
test:登陆时默认存在的库,不切换其他库时默认存在的库
admin:系统预留库,MongoDB系统管理库
local:本地预留库,存储关键日志
config:MongoDB配置信息库
mongo默认登陆的时候是在test库下
mongo不需要提前创建库和表,直接use切换就是创建库,直接插入数据就会创建表
使用use切换到的库,如果没有任何数据,实际上并不会真正创建,是个虚的库,所以show dbs并不会显现,只有在插入数据后,数据库才会真正创建
- (>) 大于 - $gt
- (<) 小于 - $lt
- (>=) 大于等于 - $gte
- (<= ) 小于等于 - $lte
二. 常用命令
命令 |
含义 |
---|---|
show databases |
查看数据库 |
db |
显示当前所在数据库 |
use config |
切换数据库 |
help |
显示帮助信息 |
db.help() |
显示数据库帮助信息 |
show users |
查看所有用户 |
show tables |
查看所有表 |
db.表名.drop() |
删除一个表 |
db.表名.insert() |
插入一条数据 |
db.表名.insertMany() |
插入多条数据 |
db.表名.find() |
查询数据 |
db.表名.updateOne() |
更新一条数据 |
db.表名.updateMany() |
更新多条数据 |
db.表名.update() |
更新一条数据 |
db.表名.deleteOne() |
删除一条数据 |
db.表名.deleteMany() |
删除多条数据 |
三. 常用命令探索 1. 展示数据库
docker exec -it 1c87ff69af43a8253b7ccff671b9fc66a52aa0bdc5fbecbf5b9efb7145550824 /bin/sh
mongo
#查询当前有多少个数据库
> show databases
#显示当前所在的数据库
> db
test
#显示当前所在的数据库表信息
> show tables
users
2. 插入数据
db.users.insert({"_id":"tomxiang","name":"向少","age":1000})
db.users.insert({"_id":"sally","name":"hebinbin","age":1000,"fruit":"apple"})
db.users.insert({"name":"xiaozi","ad":"石家庄市","job":"guanggao"})
查看结果
db.users.find()
{ "_id" : "easydoc", "name" : "易文档", "age" : 18 }
{ "_id" : "tomxiang", "name" : "向少", "age" : 1000 }
{ "_id" : "sally", "name" : "hebinbin", "age" : 1000, "fruit" : "apple" }
{ "_id" : ObjectId("63520158ebc6c7e46c33e464"), "name" : "xiaozi", "ad" : "石家庄市", "job" : "guanggao" }
image.png 2. 插入多条数据
语法格式:db.表名.insertMany({“字段1”:“值1”,“字段2”:“值2”},{“字段1”:“值1”,“字段2”:“值2”})
使用insertMany插入的数据,会自动生产ObjectId
db.book_date.insertMany([
{ "name":"Nginx", "price":25, "num":100, "status":"N" },
{ "name":"ansible", "price":50, "num":200 , "status":"A" },
{ "name":"tomcat", "price":100, "num":150, "status":"T" },
{ "name":"redis", "price":75, "num":320 , "status":"R" },
{ "name":"docker", "price":45, "num":270, "status":"D" }
]);
image.png 3. 插入嵌套数据
> db.nest_date.insertMany( [
... ... { "item": "journal", "qty": 25, "size": { "h": 14, "w": 21, "uom": "cm" }, "status": "A" },
... ... { "item": "notebook", "qty": 50, "size": { "h": 8.5, "w": 11, "uom": "in" }, "status": "A" },
... ... { "item": "paper", "qty": 100, "size": { "h": 8.5, "w": 11, "uom": "in" }, "status": "D" },
... ... { "item": "planner", "qty": 75, "size": { "h": 22.85, "w": 30, "uom": "cm" }, "status": "D" },
... ... { "item": "postcard", "qty": 45, "size": { "h": 10, "w": 15.25, "uom": "cm" }, "status": "A" }
... ... ]);
{
"ackNowledged" : true,
"insertedIds" : [
ObjectId("63524279b7e4788da144a282"),
ObjectId("63524279b7e4788da144a283"),
ObjectId("63524279b7e4788da144a284"),
ObjectId("63524279b7e4788da144a285"),
ObjectId("63524279b7e4788da144a286")
]
}
> db.nest_date.find()
{ "_id" : ObjectId("63524279b7e4788da144a282"), "item" : "journal", "qty" : 25, "size" : { "h" : 14, "w" : 21, "uom" : "cm" }, "status" : "A" }
{ "_id" : ObjectId("63524279b7e4788da144a283"), "item" : "notebook", "qty" : 50, "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "status" : "A" }
{ "_id" : ObjectId("63524279b7e4788da144a284"), "item" : "paper", "qty" : 100, "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "status" : "D" }
{ "_id" : ObjectId("63524279b7e4788da144a285"), "item" : "planner", "qty" : 75, "size" : { "h" : 22.85, "w" : 30, "uom" : "cm" }, "status" : "D" }
{ "_id" : ObjectId("63524279b7e4788da144a286"), "item" : "postcard", "qty" : 45, "size" : { "h" : 10, "w" : 15.25, "uom" : "cm" }, "status" : "A" }
3. 查询数据 1. 基本查询
> db.book_date.find({"status":"N"})
{ "_id" : ObjectId("63524ec3b7e4788da144a287"), "name" : "Nginx", "price" : 199, "num" : 530, "status" : "N", "lastModified" : ISODate("2022-10-21T08:18:11.206Z") }
2. 关键字:
or
查询book_date表status等于T或者price小于50的数据
db.book_date.find({$or: {"status": "T"},{"price": {$lt: 50}}}) { "_id" : ObjectId("635241f3b7e4788da144a281"), "name" : "docker", "price" : 45, "num" : 270, "status" : "D" }
4. 删除数据
语法格式:
删除一个:db.表名.deleteOne({“条件1”:“值1”})
删除多个:db.表名.deleteMany({“条件1”:“值1”})
1. 删除一条数据
> db.book_date.deleteOne({"name":"Nginx"})
{ "ackNowledged" : true, "deletedCount" : 1 }
2. 删除多条数据
> db.book_date.deleteMany({"num":{$lt:200}})
{ "ackNowledged" : true, "deletedCount" : 2 }
5. 修改数据
mongodb更新一条数据后,会在最后增加一个新的字段,记录更新的时间
语法格式:
更新一条数据:db.表名.updateOne({“匹配条件1”:"值"1},{set:{"更新的字段1":"新值1","更新字段2":"新值2"},currentDate:{“lastModified”:true}})
更新多条数据:db.表名.updateMany({“匹配条件1”:"值"1},{set:{"更新的字段1":"新值1","更新字段2":"新值2"},currentDate:{“lastModified”:true}})
使用update更新:db.表名.update({“匹配条件1”:"值"1},{set:{"更新的字段1":"新值1","更新字段2":"新值2"},currentDate:{“lastModified”:true}})
- updateMany = update + {multi: true}
- updateOne = update 或 update + {multi: false} -因为
update
本身的意义不够清楚,所以3.0以后才出现了updateMany
和updateOne
两个替代方法。这个方法没多少要说的,唯一要注意的就是,如果用update
方法的话,不要忘记操作符($set
,$inc
等等),不然……updateMany
和updateOne
则没有这个问题,缺了操作符会直接报错。
1. 更新一条数据
> db.book_date.updateOne({"name":"Nginx"},{$set:{"price":500,"num":230},$currentDate:{"lastModified":true}})
{ "ackNowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
> db.book_date.find({"name":"Nginx"})
{ "_id" : ObjectId("63524ec3b7e4788da144a287"), "name" : "Nginx", "price" : 500, "num" : 230, "status" : "N", "lastModified" : ISODate("2022-10-21T08:00:39.111Z") }
解释:
$currentDate: {
"lastModified": true //增加一个字段,每次更新都记录时间
}
2. 更新多条数据
num小于250的数据,将price修改为99,num修改为530
> db.book_date.updateMany({"num":{$lt:250}},{$set:{"price":99,"num":530},$currentDate:{"lastModified":true}})
{ "ackNowledged" : true, "matchedCount" : 2, "modifiedCount" : 2 }
> db.book_date.find()
{ "_id" : ObjectId("635241f3b7e4788da144a27e"), "name" : "ansible", "price" : 99, "num" : 530, "status" : "A", "lastModified" : ISODate("2022-10-21T08:13:08.799Z") }
{ "_id" : ObjectId("635241f3b7e4788da144a280"), "name" : "redis", "price" : 75, "num" : 320, "status" : "R" }
{ "_id" : ObjectId("635241f3b7e4788da144a281"), "name" : "docker", "price" : 45, "num" : 270, "status" : "D" }
{ "_id" : ObjectId("63524ec3b7e4788da144a287"), "name" : "Nginx", "price" : 99, "num" : 530, "status" : "N", "lastModified" : ISODate("2022-10-21T08:13:08.799Z") }
参考文章
- MongoDB数据库增删改查基本使用
- MongoDB中文手册|官方文档中文版
原文地址:https://cloud.tencent.com/developer/article/2137685