概要:
-
数据的新增的方式
-
数据的查询
-
数据的修改删除
-
全文索引查询
-
数据的新增的方式
关于Mongodb数据插入的说明 -
数据库的新增不需要序先设计模型结构,插入数据时会自动创建。
-
同一个集合中不同数据字段结构可以不一样
插入相关方法:
//插入单条
db.friend.insertOne({name:“wukong”,sex:“man”});
// 插入多条
db.friend.insertMany([
{name:“wukong”,sex:“man”},{name:“diaocan”,sex:“woman”,age:18,birthday:new Date(“1995-11-02”)},{name:“zixiao”,sex:“woman”}
]);
// 指定ID
db.friend.insert([
{_id:1,name:“wokong”,sex:“man”,age:1},
{_id:2,name:“diaocan”,sex:“women”,birthday:new Date(“1988-11- 11”)}
])
2、数据的查询
概要:
a. 基于条件的基础查询
b.
a
n
d
、
and、
and、or、
i
n
、
in、
in、gt (>)、
g
t
e
(
>
=
)
、
gte (>=)、
gte(>=)、lt (<)、$lte (<=) 运算符
c. 基于 sort skip limit 方法实现排序与分页
d. 嵌套查询
e. 数组查询
f. 数组嵌套查询
基础查询:
#基于ID查找
db.emp.find({_id:1101})
#基于属性查找
db.emp.find({“name”:“鲁班”})
#&& 运算 与大于 运算
db.emp.find({“job”:“讲师”,“salary”:{KaTeX parse error: Expected 'EOF', got '}' at position 8: gt:8000}̲}) #in 运算 db.e…in:[“讲师”,“客服部”]}})
#or 运算
db.emp.find({$or:[{job:“讲师” },{job:“客服部”}] })
排序与分页:
// sort skip limit
db.emp.find().sort({dep:1,salary:-1}).skip(5).limit(2)
嵌套查询:
错误示例:无结果
db.student.find({grade:{redis:87,dubbo:90 });
#错误示例:无结果
db.student.find({grade:{redis:87,dubbo:90,zookeper:85} })
基于复合属性查找 时必须包含其所有的值 并且顺序一至
db.student.find({grade:{redis:87,zookeper:85,dubbo:90} })
#基于复合属性当中的指定值 查找。注:名称必须用双引号
db.student.find({“grade.redis”:87});
db.student.find({“grade.redis”:{"$gt":80}});
数组查询:
db.subject.insertMany([
{_id:“001”,name:“陈霸天”,subjects:[“redis”,“zookeper”,“dubbo”]},
{_id:“002”,name:“张明明”,subjects:[“redis”,“Java”,“mySql”]},
{_id:“003”,name:“肖炎炎”,subjects:[“mySql”,“zookeper”,“bootstrap”]},
{_id:“004”,name:“李鬼才”,subjects:[“Java”,“dubbo”,“Java”]},
])
#无结果
db.subject.find({subjects:[“redis”,“zookeper”]})
#无结果
db.subject.find({subjects:[“zookeper”,“redis”,“dubbo”]})
与嵌套查询一样,必须是所有的值 并且顺序一至
db.subject.find({subjects:[“redis”,“zookeper”,“dubbo”]})
$all 匹配数组中包含该两项的值。注:顺序不作要求
db.subject.find({subjects:{"$all": [“redis”,“zookeper”]}})
注:
简化数组查询
db.subject.find({subjects:“redis”})
简化数组查询 ,匹配数组中存在任意一值。与$all相对应
db.subject.find({subjects:{$in: [“redis”,“zookeper”]}})
数组嵌套查询:
#基础查询 ,必须查询全部,且顺序一至
db.subject2.find({subjects:{name:“redis”,hour:12} })
#指定查询第一个数组 课时大于12
db.subject2.find({“subjects.0.hour”:{KaTeX parse error: Expected 'EOF', got '}' at position 6: gt:12}̲}) #查询任科目 课时大于1…gt:12}})
$elemMatch 元素匹配,指定属性满足,且不要求顺序一至
db.subject2.find({subjects:{$elemMatch:{name:“redis”,hour:12}}})
数组中任意元素匹配 不限定在同一个对象当中
db.subject2.find({“subjects.name”:“mysql”,“subjects.hour”:120})
修改
#设置值
db.emp.update({_id:1101} ,{ $set:{salary:10300} })
#自增
db.emp.update({_id:1101} ,{ $inc:{salary:200}})
#基于条件 更新多条数据
只会更新第一条
db.emp.update({“dep”:“客服部”},{$inc:{salary:100}})
更新所有 匹配的条件
db.emp.updateMany({“dep”:“客服部”},{$inc:{salary:100}})
3、数据的修改与删除
修改
#设置值
db.emp.update({_id:1101} ,{ $set:{salary:10300} })
#自增
db.emp.update({_id:1101} ,{ $inc:{salary:200}})
#基于条件 更新多条数据
只会更新第一条
db.emp.update({“dep”:“客服部”},{$inc:{salary:100}})
更新所有 匹配的条件
db.emp.updateMany({“dep”:“客服部”},{$inc:{salary:100}})
删除:
// 基于查找删除
db.emp.deleteOne({_id:1101})
// 删除整个集合
db.project.drop()
// 删除库
db.dropDatabase()
4、全文索引
索引的创建
db.project.createIndex({name:“text”,description:“text”})
基于索引分词进行查询
db.project.find({KaTeX parse error: Expected '}', got 'EOF' at end of input: text:{search:“java jquery”}})
基于索引 短语
db.project.find({KaTeX parse error: Expected '}', got 'EOF' at end of input: text:{search:"“Apache ZooKeeper”"}})
过滤指定单词
db.project.find({KaTeX parse error: Expected '}', got 'EOF' at end of input: text:{search:“java apache -阿里”}})
查看执行计划
db.project.find({KaTeX parse error: Expected '}', got 'EOF' at end of input: text:{search:“java -阿里”}}).explain(“executionStats”)