mongodb-crud基本操作
删库
> db.dropDatabase()
创建集合(表)
> db.createCollection('a')
> db.createCollection('b')
直接插入文档,集合会自动创建
> db.c.insert({username: 'mongodb'})
> show collections
删除集合
> db.a.drop()
重命名集合
> db.b.renameCollection("bb")
插入文档
> db.fruit.insertOne({name: "apple"})
{
"acknowledged" : true,
"insertedId" : ObjectId("64954b7814048ce7b7db5861")
}
> db.fruit.insertMany([{name: "apple"}, {name: "pear"}, {name: "orange"}])
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("64954bf814048ce7b7db5862"),
ObjectId("64954bf814048ce7b7db5863"),
ObjectId("64954bf814048ce7b7db5864")
]
}
> for(i=0;i<10000;i++){db.log.insert({"uid":i,"name":"mongodb","age":6,"date":new Date()}); }
find 查找
## 查询 log 集合所有信息
> db.log.find()
> show collections
## 指定单个条件查询
> db.log.find({uid: 15})
## 指定多个条件查询,相当于 and
> db.log.find({uid: 15,age: 6})
## 指定多个条件查询,以 or 方式
> db.log.find({ $or: [{uid: 15},{uid: 16},{uid: 17}]})
MySQL 和 mongodb 条件表达式对比
MySQL | mongodb |
---|---|
a=1 | |
a<>1 | {a:{$ne:1}} |
a>1 | {a:{$gt:1}} |
a>=1 | {a:{$ge:1}} |
a<1 | {a:{$lt:1}} |
a<=1 | {a:{$le:1}} |
a=1 and b=1 | {a:1,b:1} 或 {$and:[{a:1},{b:1}]} |
a=1 or b=1 | {$or: [{a:1},{b:2}]} |
a is null | {a:{$exists:false}} |
a in (1,2,3) | {a:{$in:[1,2,3]}} |
通过 find 搜索子文档
> db.fruit.find()
{ "_id" : ObjectId("64954b7814048ce7b7db5861"), "name" : "apple" }
{ "_id" : ObjectId("64956d22e8de26ee5060da9d"), "name" : "apple", "from" : { "country" : "china", "province" : "guangdong" } }
{ "_id" : ObjectId("64956e1de8de26ee5060da9e"), "name" : "apple", "color" : [ "red", "green" ] }
{ "_id" : ObjectId("64956e1de8de26ee5060da9f"), "name" : "mango", "color" : [ "yellow", "green" ] }
> db.fruit.find({"from.country": "china"})
{ "_id" : ObjectId("64956d22e8de26ee5060da9d"), "name" : "apple", "from" : { "country" : "china", "province" : "guangdong" } }
通过 find 搜索数组
> db.fruit.find({color:"red"})
{ "_id" : ObjectId("64956e1de8de26ee5060da9e"), "name" : "apple", "color" : [ "red", "green" ] }
> db.fruit.find({$or: [{color:"red"},{color:"yellow"}]})
{ "_id" : ObjectId("64956e1de8de26ee5060da9e"), "name" : "apple", "color" : [ "red", "green" ] }
{ "_id" : ObjectId("64956e1de8de26ee5060da9f"), "name" : "mango", "color" : [ "yellow", "green" ] }
通过 find 搜索数组中的对象
> db.movies.find({"filming_locations.city":"Rome"})
{ "_id" : ObjectId("64956f6ce8de26ee5060daa0"), "title" : "Raiders of the Lost Ark", "filming_locations" : [ { "city" : "Los Angeles", "state" : "CA", "country" : "USA" }, { "city" : "Rome", "state" : "Lazio", "country" : "Italy" }, { "city" : "Florence", "state" : "SC", "country" : "USA" } ] }
{ "_id" : ObjectId("64964e705374a9c8332c9f7c"), "title" : "11111", "filming_locations" : [ { "city" : "bj", "state" : "CA", "country" : "CHN" }, { "city" : "Rome", "state" : "Lazio", "country" : "Italy" }, { "city" : "tlp", "state" : "SC", "country" : "USA" } ] }
> db.getCollection('movies').find({"filming_locations.city":"Rome","filming_locations.country":"USA"})
{ "_id" : ObjectId("64956f6ce8de26ee5060daa0"), "title" : "Raiders of the Lost Ark", "filming_locations" : [ { "city" : "Los Angeles", "state" : "CA", "country" : "USA" }, { "city" : "Rome", "state" : "Lazio", "country" : "Italy" }, { "city" : "Florence", "state" : "SC", "country" : "USA" } ] }
{ "_id" : ObjectId("64964e705374a9c8332c9f7c"), "title" : "11111", "filming_locations" : [ { "city" : "bj", "state" : "CA", "country" : "CHN" }, { "city" : "Rome", "state" : "Lazio", "country" : "Italy" }, { "city" : "tlp", "state" : "SC", "country" : "USA" } ] }
> db.getCollection('movies').find({"filming_locations":{$elemMatch:{"city":"bj","country":"CHN"}}})
{ "_id" : ObjectId("64964e705374a9c8332c9f7c"), "title" : "11111", "filming_locations" : [ { "city" : "bj", "state" : "CA", "country" : "CHN" }, { "city" : "Rome", "state" : "Lazio", "country" : "Italy" }, { "city" : "tlp", "state" : "SC", "country" : "USA" } ] }
通过find 指定返回的字段
> db.movies.find({},{"_id":0, title:1})
{ "title" : "Raiders of the Lost Ark" }
{ "title" : "11111" }
_id 默认是自动返回的,需要指定不返回。
通过remove 删除文档记录
## 删除匹配条件的文档记录
> db.fruit.remove({name:"apple"})
## 删除所有的文档记录
> db.fruit.remove({})
更新文档记录
> db.fruit.updateOne({name:"apple"},{$set:{from:"usa"}})
使用 updateOne 表示无论条件匹配多少条记录,始终只更新第一条;
使用 updateMany 表示条件匹配多少条就更新多少条;
updateOne/updateMany 方法要求更新条件部分必须具有以下之一,否则将报错:
-
$set/$unset
-
$push/$pushAll/$pop
-
$pull/$pullAll
-
$addToSet
$push: 增加一个对象到数组底部
$pushAll: 增加多个对象到数组底部
$pop: 从数组底部删除一个对象
$pull: 如果匹配指定的值,从数组中删除相应的对象
$pullAll: 如果匹配任意的值,从数据中删除相应的对象
$addToSet: 如果不存在则增加一个值到数组
删除集合
> db.fruit.drop()
删除数据库
> use test
> db.dropDatabase()
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构