Monngodb的基本增删改查语句

使用 insert 完成插入操作
操作格式:
db.<集合>.insertOne(<JSON对象>)
db.<集合>.insertMany([<JSON 1>, <JSON 2>, …])
示例:
db.fruit.insertOne({name: “apple”})
db.fruit.insertMany([
{name: “apple”},
{name: “pear”},
{name: “orange”}
])

使用 find 查询文档
● 关于 find:
• find 是 MongoDB 中查询数据的基本指令,相当于 SQL 中的 SELECT
• find 返回的是游标。
● find 示例:
db.movies.find( { “year” : 1975 } ) // 单条件查询
db.movies.find( { “year” : 1989, “title” : “Batman” } ) // 多条件 and 查询
db.movies.find( { $and : [ {“title” : “Batman”}, { “category” : “action” }] } ) // and 的另一种形式
db.movies.find( { KaTeX parse error: Expected 'EOF', got '}' at position 45: …" : "Batman"}] }̲ ) // 多条件 or 查询…ne: 1}}
a > 1 {a: { KaTeX parse error: Expected 'EOF', got '}' at position 6: gt: 1}̲} a >= 1 {a: …gte: 1}}
a < 1 {a: { KaTeX parse error: Expected 'EOF', got '}' at position 6: lt: 1}̲} a <= 1 {a: …lte: 1}}
查询逻辑对照表
SQL MQL
a = 1 AND b = 1 {a: 1, b: 1}或{ KaTeX parse error: Expected 'EOF', got '}' at position 22: …{a: 1}, {b: 1}]}̲ a = 1 OR b =…or: [{a: 1}, {b: 1}]}
a IS NULL {a: { KaTeX parse error: Expected 'EOF', got '}' at position 14: exists: false}̲} a IN (1, 2,…in: [1, 2, 3]}}
查询逻辑运算符
● $lt: 存在并小于
● $lte: 存在并小于等于
● $gt: 存在并大于
● $gte: 存在并大于等于
● $ne: 不存在或存在但不等于
● $in: 存在并在指定数组中
● $nin: 不存在或不在指定数组中
● $or: 匹配两个或多个条件中的一个
KaTeX parse error: Expected '}', got 'EOF' at end of input: …db.fruit.find({ or: [{color: “red”}, {color: “yellow”}]} )
使用 find 搜索数组中的对象
● 考虑以下文档,在其中搜索
db.movies.insertOne( {
“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” }
]
})
• // 查找城市是 Rome 的记录
• db.movies.find({“filming_locations.city”: “Rome”})
使用 find 搜索数组中的对象
● 在数组中搜索子对象的多个字段时,如果使用 $elemMatch,它表示必须是同一个
子对象满足多个条件。考虑以下两个查询:
db.getCollection(‘movies’).find({
“filming_locations.city”: “Rome”,
“filming_locations.country”: “USA”
})
db.getCollection(‘movies’).find({
“filming_locations”: {
$elemMatch:{“city”:“Rome”, “country”: “USA”}
}
})
控制 find 返回的字段
● find 可以指定只返回指定的字段;
● _id字段必须明确指明不返回,否则默认返回;
● 在 MongoDB 中我们称这为投影(projection);
● db.movies.find({“category”: “action”},{“_id”:0, title:1})
不返回_id 返回title
使用 remove 删除文档
● remove 命令需要配合查询条件使用;
● 匹配查询条件的的文档会被删除;
● 指定一个空文档条件会删除所有文档;
● 以下示例:
db.testcol.remove( { a : 1 } ) // 删除a 等于1的记录
db.testcol.remove( { a : { KaTeX parse error: Expected 'EOF', got '}' at position 8: lt : 5 }̲ } ) // 删除a 小于5…set: {from: “China”}})
查询 name 为
apple 的记录
将找到记录的 from
设置为 China
使用 update 更新文档
● 使用 updateOne 表示无论条件匹配多少条记录,始终只更新第一条;
● 使用 updateMany 表示条件匹配多少条就更新多少条;
● updateOne/updateMany 方法要求更新条件部分必须具有以下之一,否则将报错:
s e t / set/ set/unset
p u s h / push/ push/pushAll/$pop
p u l l / pull/ 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

猜你喜欢

转载自blog.csdn.net/qq_41568648/article/details/129781449