基本概念

数据库分为TRDB和NoSQL
TRDB包含Oracle(第一款)、MySQL等
NoSQL包含MongoDB、Redis等

NoSQL:是一种非关系型DMS,不需要固定的架构,可以避免joins链接,并且易于扩展。
在这里插入图片描述
MongoDB数据库构成:
数据库<集合<文档<字段
_id:唯一标识,主键
游标:指向查询结果集的指针
数据库:集合容器
集合:文档的分组,存在于单个数据库中
文档:集合中的记录,包含字段名称和值
字段:文档中的 (名称/值) 对

基本操作

MongoDB创建数据库:use DBname

rs:PRIMARY> use Employee
switched to db Employee

创建集合/表:insert()

rs:PRIMARY> db.Employee.insert(
... {
... "编号":1,
... "EmployeeName":"Smith"
... }
... )
WriteResult({ "nInserted" : 1 })
rs:PRIMARY> 

insert()还可以插入数组

rs:PRIMARY> var myEmployee = [ {"编号":2, "EmployeeName":"Smith2"},{"编号":3,"EmployeeName":"Smith3"}]
rs:PRIMARY> db.Employee.insert(myEmployee)
BulkWriteResult({
	"writeErrors" : [ ],
	"writeConcernErrors" : [ ],
	"nInserted" : 2,
	"nUpserted" : 0,
	"nMatched" : 0,
	"nModified" : 0,
	"nRemoved" : 0,
	"upserted" : [ ]
})
rs:PRIMARY> 

查询文档

rs:PRIMARY> db.Employee.find()
{ "_id" : ObjectId("6033295c785939f88516b7f4"), "Employeeid" : 1, "EmployeeName" : "Smith" }
{ "_id" : ObjectId("6033295c785939f88516b7f5"), "Employeeid" : 2, "EmployeeName" : "Mohan" }
{ "_id" : ObjectId("6033295c785939f88516b7f6"), "Employeeid" : 3, "EmployeeName" : "Joe" }
{ "_id" : 10, "EmployeeName" : "Smith" }
rs:PRIMARY> 

条件查询

rs:PRIMARY> db.Employee.find({EmployeeName:"Smith"}).forEach(printjson)
{
	"_id" : ObjectId("6033295c785939f88516b7f4"),
	"Employeeid" : 1,
	"EmployeeName" : "Smith"
}
{ "_id" : 10, "EmployeeName" : "Smith" }
rs:PRIMARY> db.Employee.find({Employeeid:{$gt:2}}).forEach(printjson)
{
	"_id" : ObjectId("6033295c785939f88516b7f6"),
	"Employeeid" : 3,
	"EmployeeName" : "Joe"
}
rs:PRIMARY> 

查询修饰符 limit

rs:PRIMARY> db.Employee.find().limit(2).forEach(printjson)
{
	"_id" : ObjectId("6033295c785939f88516b7f4"),
	"Employeeid" : 1,
	"EmployeeName" : "Smith"
}
{
	"_id" : ObjectId("6033295c785939f88516b7f5"),
	"Employeeid" : 2,
	"EmployeeName" : "Mohan"
}
rs:PRIMARY> 

排序查询:1为升序,-1为降序

rs:PRIMARY> db.Employee.find().sort({Employeeid:-1}).forEach(printjson)
{
	"_id" : ObjectId("6033295c785939f88516b7f6"),
	"Employeeid" : 3,
	"EmployeeName" : "Joe"
}
{
	"_id" : ObjectId("6033295c785939f88516b7f5"),
	"Employeeid" : 2,
	"EmployeeName" : "Mohan"
}
{
	"_id" : ObjectId("6033295c785939f88516b7f4"),
	"Employeeid" : 1,
	"EmployeeName" : "Smith"
}
{ "_id" : 10, "EmployeeName" : "Smith" }
rs:PRIMARY>

查询集合中文档数量count()

rs:PRIMARY> db.Employee.count()
4
rs:PRIMARY>

执行文档修改操作
删除 remove()

rs:PRIMARY> db.Employee.remove({Employeeid:2})
WriteResult({ "nRemoved" : 1 })
rs:PRIMARY> db.Employee.find()
{ "_id" : ObjectId("6033295c785939f88516b7f4"), "Employeeid" : 1, "EmployeeName" : "Smith" }
{ "_id" : ObjectId("6033295c785939f88516b7f6"), "Employeeid" : 3, "EmployeeName" : "Joe" }
{ "_id" : 10, "EmployeeName" : "Smith" }
rs:PRIMARY> 

更新单个/多个值 update()

rs:PRIMARY> db.Employee.update(
... {"Employeeid":1},{$set:{"EmployeeName":"NewSmith"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
rs:PRIMARY> db.Employee.find()
{ "_id" : ObjectId("6033295c785939f88516b7f4"), "Employeeid" : 1, "EmployeeName" : "NewSmith" }
{ "_id" : ObjectId("6033295c785939f88516b7f6"), "Employeeid" : 3, "EmployeeName" : "Joe" }
{ "_id" : 10, "EmployeeName" : "Smith" }
rs:PRIMARY> 

正则匹配 $regex
忽略大小写 $option:"i"

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐