• MongoDB $关键字 关系比较符号 $lt $lte $gt $gte $ne


     

     关系比较符:
       小于:$lt
       小于或等于:$lte
       大于:$gt
       大于或等于:$gte
       不等于:$ne
       属于:$in

    查询中常见的 等于 大于 小于 大于等于 小于等于

    等于 : 在MongoDB中什么字段等于什么值其实就是 " : " 来搞定 比如 "name" : "jack"

    "name" : "jack"
    > db.user.find()
    { "_id" : ObjectId("5ca7a4b0219efd687462f965"), "id" : 1, "name" : "jack", "age" : 73 }
    { "_id" : ObjectId("5ca7a4b7219efd687462f966"), "id" : 2, "name" : "mike", "age" : 84, "gender" : "男" }
    { "_id" : ObjectId("5ca7a4c4219efd687462f967"), "id" : 3, "name" : "peter", "age" : 21 }
    { "_id" : ObjectId("5ca7a4c4219efd687462f968"), "id" : 4, "name" : "xiaogang", "age" : 13, "hobby" : [ "羽毛球", "篮球", "足球" ] }
    { "_id" : ObjectId("5ca7a4c4219efd687462f969"), "id" : 5, "name" : "ben", "age" : 24 }
    { "_id" : ObjectId("5ca7a505219efd687462f96a"), "id" : 6, "name" : "Mary", "age" : 84, "gender" : "男" }

    小于 : 在MongoDB中的 小于 < 号 我们用 : $lt  比如 :  "age" : { $lt : 73 } 就是 得到 "age" 小于 73 的数据

    > db.user.find({age:{$lt:73}})
    { "_id" : ObjectId("5ca7a4c4219efd687462f967"), "id" : 3, "name" : "peter", "age" : 21 }
    { "_id" : ObjectId("5ca7a4c4219efd687462f968"), "id" : 4, "name" : "xiaogang", "age" : 13, "hobby" : [ "羽毛球", "篮球", "足球" ] }
    { "_id" : ObjectId("5ca7a4c4219efd687462f969"), "id" : 5, "name" : "ben", "age" : 24 }

    大于 : 在MongoDB中的 大于 > 号 我们用 : $gt  比如 :  "age" : { $gt : 19 } 就是 得到 "age" 大于 19 的数据

    > db.user.find({age:{$gt:19}})
    { "_id" : ObjectId("5ca7a4b0219efd687462f965"), "id" : 1, "name" : "jack", "age" : 73 }
    { "_id" : ObjectId("5ca7a4b7219efd687462f966"), "id" : 2, "name" : "mike", "age" : 84, "gender" : "男" }
    { "_id" : ObjectId("5ca7a4c4219efd687462f967"), "id" : 3, "name" : "peter", "age" : 21 }
    { "_id" : ObjectId("5ca7a4c4219efd687462f969"), "id" : 5, "name" : "ben", "age" : 24 }
    { "_id" : ObjectId("5ca7a505219efd687462f96a"), "id" : 6, "name" : "Mary", "age" : 84, "gender" : "男" }

    大于等于 : 在MongoDB中的 大于等于 >= 号 我们用 : $gte  比如 :  "age" : { $gte : 13 } 就是 得到 "age" 大于等于 13 的数据

    > db.user.find({age:{$gte:13}})
    { "_id" : ObjectId("5ca7a4b0219efd687462f965"), "id" : 1, "name" : "jack", "age" : 73 }
    { "_id" : ObjectId("5ca7a4b7219efd687462f966"), "id" : 2, "name" : "mike", "age" : 84, "gender" : "男" }
    { "_id" : ObjectId("5ca7a4c4219efd687462f967"), "id" : 3, "name" : "peter", "age" : 21 }
    { "_id" : ObjectId("5ca7a4c4219efd687462f968"), "id" : 4, "name" : "xiaogang", "age" : 13, "hobby" : [ "羽毛球", "篮球", "足球" ] }
    { "_id" : ObjectId("5ca7a4c4219efd687462f969"), "id" : 5, "name" : "ben", "age" : 24 }
    { "_id" : ObjectId("5ca7a505219efd687462f96a"), "id" : 6, "name" : "Mary", "age" : 84, "gender" : "男" }

    小于等于 : 在MongoDB中的 小于等于 <= 号 我们用 : $lte  比如 :  "age" : { $lte : 73 } 就是 得到 "age" 小于等于 73 的数据

    > db.user.find({age:{$lte:73}})
    { "_id" : ObjectId("5ca7a4b0219efd687462f965"), "id" : 1, "name" : "jack", "age" : 73 }
    { "_id" : ObjectId("5ca7a4c4219efd687462f967"), "id" : 3, "name" : "peter", "age" : 21 }
    { "_id" : ObjectId("5ca7a4c4219efd687462f968"), "id" : 4, "name" : "xiaogang", "age" : 13, "hobby" : [ "羽毛球", "篮球", "足球" ] }
    { "_id" : ObjectId("5ca7a4c4219efd687462f969"), "id" : 5, "name" : "ben", "age" : 24 }
    不等于:$ne
    把age不等于73的所有数据显示
    > db.user.find({age:{$ne:73} }  )
    { "_id" : ObjectId("5ca7a4b7219efd687462f966"), "id" : 2, "name" : "mike", "age" : 84, "gender" : "男" }
    { "_id" : ObjectId("5ca7a4c4219efd687462f967"), "id" : 3, "name" : "peter", "age" : 21 }
    { "_id" : ObjectId("5ca7a4c4219efd687462f968"), "id" : 4, "name" : "xiaogang", "age" : 13, "hobby" : [ "羽毛球", "篮球", "足球" ] }
    { "_id" : ObjectId("5ca7a4c4219efd687462f969"), "id" : 5, "name" : "ben", "age" : 24 }
    { "_id" : ObjectId("5ca7a505219efd687462f96a"), "id" : 6, "name" : "Mary", "age" : 84, "gender" : "男" }
  • 相关阅读:
    ThinkPHP教程_PHP框架之ThinkPHP(三)【系统常量与模板替换】
    ThinkPHP教程_PHP框架之ThinkPHP(二)【URL路径访问与模块控制器、URL四种模式、PATHINFO的两种模式、模板与控制器之间的关系】
    ThinkPHP教程_PHP框架之ThinkPHP(一)【入门和介绍、ThinkPHP版本和文件夹规范、项目入口文件】
    Markdown,技术简历编辑的一缕清风...
    Zend Studio 13.5的安装、破解和汉化
    原生JS配合AJAX完成省市县三级联动(带数据库、PHP+Smarty)
    DAL与数据库类型的对应关系
    mysql 学习
    关于MySQL的分区(partion)
    Response.Write,Page.RegisterClientScriptBlock和Page.RegisterStartupScript的区别
  • 原文地址:https://www.cnblogs.com/mingerlcm/p/10660467.html
Copyright © 2020-2023  润新知