MongoDB的基本操作
使用 insert 完成插入操作
操作格式: db.<集合>.insertOne(<json对象>) db.<集合>.insertMany([, , …])
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | > db.fruit.insertOne({name: "apple" }) 插入的语句 返回信息 { "acknowledged" : true , "insertedId" : ObjectId( "5e980a7e05d499f414a4e839" ) } >db.fruit.insertMany([ {name: "apple" }, {name: "pear" }, {name: "orange" } ... ]) 插入的语句 返回信息 { "acknowledged" : true , "insertedIds" : [ ObjectId( "5e980a8205d499f414a4e83a" ), ObjectId( "5e980a8205d499f414a4e83b" ), ObjectId( "5e980a8205d499f414a4e83c" ) ] } |
查看插入的数据
1 2 3 4 5 6 | > db.fruit. find () { "_id" : ObjectId( "5e980a7e05d499f414a4e839" ), "name" : "apple" } { "_id" : ObjectId( "5e980a8205d499f414a4e83a" ), "name" : "apple" } { "_id" : ObjectId( "5e980a8205d499f414a4e83b" ), "name" : "pear" } { "_id" : ObjectId( "5e980a8205d499f414a4e83c" ), "name" : "orange" } { "_id" : ObjectId( "5e980a8b05d499f414a4e83d" ), "name" : "apple" } |
关于 find: find 是 MongoDB 中查询数据的基本指令,相当于 SQL 中的 SELECT 。 find 返回的是游标
find 示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | db.fruit. find ( { "name" : "apple" } ) #只有一个条件的查询 { "_id" : ObjectId( "5e980a7e05d499f414a4e839" ), "name" : "apple" } { "_id" : ObjectId( "5e980a8205d499f414a4e83a" ), "name" : "apple" } { "_id" : ObjectId( "5e980a8b05d499f414a4e83d" ), "name" : "apple" } > db.fruit. find ( { "name" : "apple" , "_id" : ObjectId( "5e980a8b05d499f414a4e83d" ) } ) #多条件并存and { "_id" : ObjectId( "5e980a8b05d499f414a4e83d" ), "name" : "apple" } >db.fruit. find ( {$and:[{ "name" : "apple" },{ "_id" : ObjectId( "5e980a8b05d499f414a4e83d" )}] } ) #多添加并存(都满足)and,另一种写法 { "_id" : ObjectId( "5e980a8b05d499f414a4e83d" ), "name" : "apple" } db.fruit. find ( {$or:[{ "name" : "apple" },{ "_id" : ObjectId( "5e980a8205d499f414a4e83c" )}] } ) #or多条件只满足一个条件即可 { "_id" : ObjectId( "5e980a7e05d499f414a4e839" ), "name" : "apple" } { "_id" : ObjectId( "5e980a8205d499f414a4e83a" ), "name" : "apple" } { "_id" : ObjectId( "5e980a8205d499f414a4e83c" ), "name" : "orange" } { "_id" : ObjectId( "5e980a8b05d499f414a4e83d" ), "name" : "apple" } > db.fruit. find ({ "name" : /^o/ } ) 这则匹配 { "_id" : ObjectId( "5e980a8205d499f414a4e83c" ), "name" : "orange" } |
查询条件对照表
SOL | mol |
a = 1 | {a: 1} |
a <> 1 | {a: {$ne: 1}} |
a > 1 | {a: {$gt: 1}} |
a >= 1 | {a: {$gte: 1}} |
a < 1 | {a: {$lt: 1}} |
a <= 1 | {a: {$lte: 1}} |
a = 1 AND b = 1 | {a: 1, b: 1}或{$and: [{a: 1}, {b: 1}]} |
a = 1 OR b = 1 | {$or: [{a: 1}, {b: 1}]} |
a IS NULL | {a: {$exists: false}} |
a IN (1, 2, 3) | {a: {$in: [1, 2, 3]}} |
查询逻辑运算符
● $lt: 存在并小于
● $lte: 存在并小于等于
● $gt: 存在并大于
● $gte: 存在并大于等于
● $ne: 不存在或存在但不等于
● $in: 存在并在指定数组中
● $nin: 不存在或不在指定数组中
● $or: 匹配两个或多个条件中的一个
● $and: 匹配全部条件
使用 find 搜索子文档
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | > db.fruit.drop() #删除操作 true > db.fruit.insertOne({ ... name: "apple" , ... from: { ... country: "China" , ... province: "Guangdon" ... } ... }) 重新插入 返回的信息 { "acknowledged" : true , "insertedId" : ObjectId( "5e99090d05d499f414a4e83e" ) } |
考虑以下查询的意义:
1 2 3 4 5 | >db.fruit. find ( { "from.country" : "China" } ) 语句表示查询一个子文档from.country 返回 { "_id" : ObjectId( "5e99090d05d499f414a4e83e" ), "name" : "apple" , "from" : { "country" : "China" , "province" : "Guangdon" } } > db.fruit. find ( { "from" : {country: "China" } } )语句表示我要找from而且值是{country: "China" } 无返回 |
使用find搜索数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | > db.fruit.insert([ ... { "name" : "Apple" , color: [ "red" , "green" ] }, ... { "name" : "Mango" , color: [ "yellow" , "green" ] } ... ]) 插入 返回 BulkWriteResult({ "writeErrors" : [ ], "writeConcernErrors" : [ ], "nInserted" : 2, "nUpserted" : 0, "nMatched" : 0, "nModified" : 0, "nRemoved" : 0, "upserted" : [ ] }) > db.fruit. find ({color: "red" }) # color值包含red { "_id" : ObjectId( "5e990b9305d499f414a4e83f" ), "name" : "Apple" , "color" : [ "red" , "green" ] } { "_id" : ObjectId( "5e990efa05d499f414a4e841" ), "name" : "Apple" , "color" : [ "red" , "green" ] } > db.fruit. find ({$or: [{color: "red" }, {color: "yellow" }]} ) 或的关系也是包含 { "_id" : ObjectId( "5e990b9305d499f414a4e83f" ), "name" : "Apple" , "color" : [ "red" , "green" ] } { "_id" : ObjectId( "5e990b9305d499f414a4e840" ), "name" : "Mango" , "color" : [ "yellow" , "green" ] } { "_id" : ObjectId( "5e990efa05d499f414a4e841" ), "name" : "Apple" , "color" : [ "red" , "green" ] } { "_id" : ObjectId( "5e990efa05d499f414a4e842" ), "name" : "Mango" , "color" : [ "yellow" , "green" ] } |
使用 find 搜索数组中的对象
1 2 3 4 5 6 7 8 9 10 11 12 | > db.movies.insertOne( { "title" : "Raiders of the Lost Ark" , "filming_locations" : [ { "city" : "Los Angeles" , "state" : "CA" , "country" : "USA" }, { "city" : "Rome" , "state" : "Lazio" , "country" : "Italy" },<br> { "city" : "Florence" , "state" : "SC" , "country" : "USA" } ] }) 语句 返回 { "acknowledged" : true , "insertedId" : ObjectId( "5e9910fa05d499f414a4e843" ) } |
// 查找城市是 Rome 的记录
1 2 3 | db.movies. find ({ "filming_locations.city" : "Rome" }) { "_id" : ObjectId( "5e9910fa05d499f414a4e843" ), "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" } ] } |
使用 find 搜索数组中的对象在数组中搜索子对象的多个字段时,如果使用 $elemMatch,它表示必须是同一个 子对象满足多个条件。考虑以下两个查询:
1 2 3 4 5 6 7 | > db.getCollection( 'movies' ). find ({ "filming_locations.city" : "Rome" , "filming_locations.country" : "USA" }) 去重查询语句 返回 { "_id" : ObjectId( "5e9910fa05d499f414a4e843" ), "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" } ] } |
控制 find 返回的字段 ● find 可以指定只返回指定的字段; ● _id字段必须明确指明不返回,否则默认返回; ● 在 MongoDB 中我们称这为投影(projection);
1 2 3 | > db.movies. find ({},{ "_id" :0, title:1}) #0表示不显示1表示显示 返回 { "title" : "Raiders of the Lost Ark" } |
db.movies.find().pretty()与db.movies.find()的区别
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | > db.movies. find ().pretty() 查询返回 { "_id" : ObjectId( "5e9910fa05d499f414a4e843" ), "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" } ] } > db.movies. find () 查询返回 { "_id" : ObjectId( "5e9910fa05d499f414a4e843" ), "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" } ] } |
使用 remove 删除文档
remove 命令需要配合查询条件使用;
匹配查询条件的的文档会被删除;
指定一个空文档条件会删除所有文档;
1 2 3 4 | db.testcol.remove( { a : 1 } ) // 删除a 等于1的记录 db.testcol.remove( { a : { $lt : 5 } } ) // 删除a 小于5的记录 db.testcol.remove( { } ) // 删除所有记录 db.testcol.remove() // 报错 |
使用 update 更新文档
Update 操作执行格式:db.<集合>.update(<查询条件>, <更新字段>)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | > db.fruit.insertMany([ {name: "apple" }, {name: "pear" }, {name: "orange" } ]) 语句 返回 { "acknowledged" : true , "insertedIds" : [ ObjectId( "5e9917fe05d499f414a4e847" ), ObjectId( "5e9917fe05d499f414a4e848" ), ObjectId( "5e9917fe05d499f414a4e849" ) ] } > > db.fruit. find () 查看 { "_id" : ObjectId( "5e9917fe05d499f414a4e847" ), "name" : "apple" } { "_id" : ObjectId( "5e9917fe05d499f414a4e848" ), "name" : "pear" } { "_id" : ObjectId( "5e9917fe05d499f414a4e849" ), "name" : "orange" } > db.fruit.updateOne({name: "apple" }, {$ set : {from: "China" }})更新添加查询 name 为apple 的记录;将找到添加的 from设置为 China { "acknowledged" : true , "matchedCount" : 1, "modifiedCount" : 1 } > db.fruit. find () 查看 { "_id" : ObjectId( "5e9917fe05d499f414a4e847" ), "name" : "apple" , "from" : "China" } { "_id" : ObjectId( "5e9917fe05d499f414a4e848" ), "name" : "pear" } { "_id" : ObjectId( "5e9917fe05d499f414a4e849" ), "name" : "orange" } |
使用 update 更新文档
● 使用 updateOne 表示无论条件匹配多少条记录,始终只更新第一条;
● 使用 updateMany 表示条件匹配多少条就更新多少条;
● updateOne/updateMany 方法要求更新条件部分必须具有以下之一,否则将报错:
• $set/$unset
• $push/$pushAll/$pop
• $pull/$pullAll
• $addToSet
● // 报错 db.fruit.updateOne({name: "apple"}, {from: "China"})
使用 update 更新数组
● $push: 增加一个对象到数组底部
● $pushAll: 增加多个对象到数组底部
● $pop: 从数组底部删除一个对象
● $pull: 如果匹配指定的值,从数组中删除相应的对象
● $pullAll: 如果匹配任意的值,从数据中删除相应的对象
● $addToSet: 如果不存在则增加一个值到数组
使用 drop 删除集合
● 使用 db.<集合>.drop() 来删除一个集合
● 集合中的全部文档都会被删除
● 集合相关的索引也会被删除 db.colToBeDropped.drop()
使用 dropDatabase 删除数据库
● 使用 db.dropDatabase() 来删除数据库
● 数据库相应文件也会被删除,磁盘空间将被释放
use tempDB
db.dropDatabase()
show collections // No collections
show dbs // The db is gone
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏