MongoDB数据库的特点及命令总结 (2)

insert(),update(),save()的使用区别:
insert()只能新增没有的id,如果该id值存在,则会报错
update()可以修改指定字段,而save()不能

--------------------MongoDB数据库的查询 (find())
1.find()不指定查询条件,则默认查询全部

2.findOne()只查询第一条数据

3.比较运算符(主要是数字之间的比较)
大于:$gt,大于或等于:$gte
小于:$lt,小于或等于:$lte
不等于:$ne

4.逻辑运算符 表示多个独立条件的逻辑关系
默认是and链接
db.stu.find({age:18,hometwon:"桃花岛"})
# 表示两个独立的条件逻辑关系and
db.stu.find({$and:
[{age:18},{hometown:"桃花岛"}]
})
# 表示两个独立的条件逻辑关系or或
db.stu.find({$or:[{age:18},{hometown:"桃花岛"}]})

# 范围运算符in
db.stu.find({age:{$in:[18,20,16,40,45]}})
db.stu.find({hometown:{$in:["桃花岛","大理","蒙古"]}})

正则表达式
用"//"表示通过正则表达式查询
db.stu.find({name:/^黄/})

用regex来表示正则表达式查询
db.stu.find({name:{$regex:"^黄"}})

7. 自定义函数查询
# 迭代每一个文档并判断该文档的 hometown是否不等于 蒙古,如果条件成立则符合查询
db.stu.find({$where : function() {return this.hometown != "蒙古"} })

 

 

 

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpjydy.html