当前位置:Gxlcms > 数据库问题 > mongodb 学习笔记 03 -- 查询表达式

mongodb 学习笔记 03 -- 查询表达式

时间:2021-07-01 10:21:17 帮助过:12人阅读

ne:’billvsme’}}) 名字不是’billvsme’
  • > : db.stu.find({age:{$gt:18}}) 年纪大于18
  • < : db.stu.find({age:{$lt:18}}) 年纪小于18
  • >=: $gte
  • <=: $lte
  • in/not in/all

    • $in :db.goods.find(stu_id:{$in:[93001,93002,93003]}) 学号为93001或者93002或者93003
    • $nin :not in
    • $all :指定的内容都有 db.stu.find({like:{$all:[‘football’,’basketball’]}}) 喜欢篮球跟足球的学生

    or/and/not/nor

    • $or :或
    • $and : 且 例子 db.stu.find({$and:[{age:{$gt:18}},{age:{$lt:22}}]}) 年纪在18到22谁之间
    • $not : 非
    • $nor : 如果你写的条件都不为真,则返回这一条

    exists/mod/type

    • $exists : 例子 db.find({body:{$exists:1}}) 存在body字段的列
    • $mod : 满足求余数,例子 db.stu.find({stu_id:{$mod:[5,0]}}) 取学号是5的倍数的
    • $type : 满足类型 例子 db.stu.find({age:{$type:2}}) age段类型是string的列。2:表示类型代码 具体见http://docs.mongodb.org/manual/reference/operator/query/type/#op._S_type

    where/regex
    (要慎用,因为如果使用,mongo要先把二进制的bson转化成json然后操作,效率低)

    • $where :js表达式为真则为真。例子 db.stu.find({$where:’this.age>18’}) 年龄大于18
    • $regex :正则表达式为真则为真。例子 db.stu.find({name:{$regex:’^bill’}}) 姓名以bill开头的

    mongodb 学习笔记 03 -- 查询表达式

    标签:mongodb   nosql   

    人气教程排行