数据库的创建
use dbname
Insert
- insertOne:插入一条数据到collection中
db.inventory.insertOne(
{ item: "canvas", qty: 100, tags: ["cotton"], size: { h: 28, w: 35.5, uom: "cm" } }
)
- insertMany:插入多条数据到collection中
db.inventory.insertMany([
{ item: "journal", qty: 25, tags: ["blank", "red"], size: { h: 14, w: 21, uom: "cm" } },
{ item: "mat", qty: 85, tags: ["gray"], size: { h: 27.9, w: 35.5, uom: "cm" } },
{ item: "mousepad", qty: 25, tags: ["gel", "blue"], size: { h: 19, w: 22.85, uom: "cm" } }
])
- insert:db.inventory.insertMany 即可以插入一条数据也可以插入多条数据
Query
- 查询当前collection中的所有数据
db.collection.find({})
- 查询当前collection中name=zhangsan的数据
db.collection.find( { "name": "zhangsan" } ) // 等同于 select * from tableName where name="zhangsan"
- 查询当前collection中status=A或status=D的数据 【与关系型数据库中的子查询类似】
db.collection.find( { status: { $in : [ "A", "D" ] } })
- 复合查询 and
db.collection.find( { status: "A",qty: {$lt:30} } ) // 等同于 select * from tableName where status="A" and qty<30
- 复合查询 or
db.collection.find( { $or: [ { status: "A" },{ qty: { $lt:30 } } ] } ) //等同于 select * from tableName where status="A" or qty<30
- 复合查询 模糊查询
db.lhc.find( { status: "A", $or:[ { qty: { $lt:30 } },{ item: /^p/} ] } ) // 等同于select * from tableName where status ="A" and (qty < 30 or item like '%p')
- 查询一个数据,参数可为空
db.collection.findOne({}) // 等同与select * from tableName limit 0,1
db.collection.findOne( { status: "A" } ) // 等同于select * from tableName where status ="A" limit 0,1
- 完全匹配查询
当查询条件是size:{ h: 14, w: 21, uom: “cm” }时,也要求各参数的顺序也要一样
sb.collection.find( { size: { h: 14, w: 21, uom: "cm" } } )
- 嵌套查询
db.collection.find( { "size.h": { $lt: 15} } ) //查询size中h字段的值小于15的数据
db.collection.find( { "size.h": { $lt: 15 }, "size.uom": "in", status: "D" }) // 多个嵌套查询
数组的查询
- 匹配一个数组
要在数组上指定相等条件,请使用查询文档{:},其中是要匹配的确切数组,包括元素的顺序
db.collection.find( { tags: ["red", "blank"] } ) // 查询包含red和blank字段的数组。且考虑顺序
db.collection.find( { tags : { $all : [ "red","blank" ] } } ) // 查询包含red和blank的数组,不考虑顺序和其他的元素
- 查询数组
db.collection.find( { tags: "red" } ) // 查询tags数组中包含字段red的数据
db.collection.find( { dim_cn: { $gt: 25 } } ) // 查询dim_cn数组中,其值大于25的数据
db.collection.find(dim_cm:{$gt:15,$lt:20}) // 查询dim_cm数组中,其中一个大于15 ,另一个小于20的数据
db.collection.find( { dim_cm: { $elemMatch: { $gt:22, $lt: 30 } } } ) // 查询至少一条数据符合条件大于22 小于30
db.collectio.find( { "dim_cm.1": { $gt: 25 } } ) // 查询dim_cm数组中第二个元素大于25的数据
db.collection.find( { "tags": { $size: 3 } } ) // 查询tags数组中有三个元素的数据
- 查询数组中的文档
数据源
db.inventory.insertMany( [
{ item: "journal", instock: [ { warehouse: "A", qty: 5 }, { warehouse: "C", qty: 15 } ] },
{ item: "notebook", instock: [ { warehouse: "C", qty: 5 } ] },
{ item: "paper", instock: [ { warehouse: "A", qty: 60 }, { warehouse: "B", qty: 15 } ] },
{ item: "planner", instock: [ { warehouse: "A", qty: 40 }, { warehouse: "B", qty: 5 } ] },
{ item: "postcard", instock: [ { warehouse: "B", qty: 15 }, { warehouse: "C", qty: 35 } ] }
]);
- 查询inventory数组中的符合warehouse=A且qty=5的数据,且考虑顺序
db.inventory.find( { "instock": { warehouse: "A", qty: 5 } } )
db.inventory.find( { "instock.qty": 5, "instock.warehouse": "A" } ) // 不考虑顺序
- 查询inventory数组中,每个对象中qty的值小于20的所有数据
db.inventory.find( { "instock.qty": { $lte: 20 } } )
- 查询inventory数组中,第一个对象的qty值小于20的数据
db.inventory.find( { "instock.0.qty": { $lte: 20 } } )
- 查询inventory数组中,同时满足warehouse=A且qty=5的对象
db.inventory.find( {"instock": { $elemMatch: { qty: 5, warehouse: "A" } } } )
- 查询inventory数组中,同时满足qty>10且qty<20的对象
db.inventory.find( {"instock": { $elemMatch: { qty: {$gt: 10, $lte: 20 } } } } )
db.inventory.find( {"instock.qty": { $gt: 10, $lte: 20 } } )
使用条件查询并返回指定的字段
- 查询inventory数组中,status=A并返回_id,item和status字段
1:代表返回指定的字段
db.inventory.find( { status: "A" }, { item: 1, status: 1 } )
- 查询inventory数组中,status=A并返回item和status字段
0:在返回的字段中,要排除的字段
db.inventory.find( { status: "A" }, { item: 1, status: 1, _id:0 } )
- 查询inventory数组中,status=A并返回item和size数组中的h字段
db.inventory.find( { status: "A" }, { item: 1, status: 1, "size.h": 1 } )
- 查询inventory数组中,status=A并排除size数组中的h字段
db.inventory.find( { status: "A" }, { "size.h": 0 } )
- 查询inventory数组中,status=A并返回item、status和instock数组中的最后一个对象
db.inventory.find( { status: "A" }, { item: 1, status: 1, instock: { $slice: -1 } } )
查询数据的value值为null或key为不存在的数据
- 数据源
db.inventory.insertMany([
{ _id: 1, item: null },
{ _id: 2 }
])
- 查询不包含item字段的数据
db.inventory.find( { item: null } )
结果
<!--{ "_id" : 1, "item" : null }-->
<!--{ "_id" : 2 }-->
- 查找item字段为null数据
<!--10在mongodb中代表null-->
db.inventory.find( { item: { $type: 10 } } )
- 查找不包含item字段的数据
db.inventory.find( {item: { $exists: false } } )
在mongo shell中进行迭代操作
- 对数据进行迭代
var a = db.inventory.find({})
while(a.hasNext()) {
printjson(a.next())
}
- 通过下标进行迭代
var myCursor = db.inventory.find( {} );
var documentArray = myCursor.toArray();
var myDocument = documentArray[1];
printjsono(myDocument)
Update
- 修改一条数据:修改size中h字段的值为20,status字段的值为Z,且添加修改时间,条件为item=planner
update inventory set h=20,status='Z' where item='planner'
db.inventory.updateOne({
item: "planner",
},
{
$set: {
"size.h": 20,
status: "Z"
},
$currentDate: {lastModified:true}
})
- 修改多条数据:当qty<100时,修改size数组中的h的值为666,status=‘D’
update inventory set h=666,status='D' where qty<100
db.inventory.updateMany({qty:{$lte:50}},
{
$set: {"size.h":666,status: "D"},
$currentDate: {lastModified: true}
})
- 替换文档
db.inventory.replaceOne(
{ item: "paper" },
{ item: "paper", instock: [ { warehouse: "A", qty: 60 }, { warehouse: "B", qty: 40 } ] }
)
- 总结
- updateOne(): 对单个文档的修改
- updateMany():对多个文档地修改
- update():对单个文档的操作
Delete
- 删除多个
db.collection.deleteMany({}} //等同于 delete * from tableName
指定条件并进行多个删除
db.inventory.deleteMany( { "size.h": { $lte: 20 } } ) // 等同于 delete * from tableName where h<20
- 删除一个
db.inventory.deleteOne({item:"planner"}) // 等同于 delete * from tableName where item='planner'
批量写入操作
当对有序的数据进行写入操作时,若写入过程中出现error,则停止后续的写入操作
当对无序的数据进行写入操作的时候,若写入过程中出现error,mongoDB会继续执行批量写入操作
- 批量写入的方法
- insertOne
- updateOne
- updateMany
- replaceOne
- deleteOne
- deleteMany
SQL与MOngoDB关系对映关系
- 常见查询语句的对应