设为首页 收藏本站
查看: 878|回复: 0

[经验分享] MongoDB语法实践

[复制链接]

尚未签到

发表于 2017-10-27 09:14:28 | 显示全部楼层 |阅读模式
####简单操作过程
基本查询:
        构造查询数据:
        db.test.insert({name:"stephen",age:35,genda:"male",email:"stephen@hotmail.com"})
        db.test.insert({name:"Stephen",age:35,genda:"male",email:"stephen@hotmail.com"})
        db.test.insert({name:"stephen1",age:35,genda:"male",email:"stephen@hotmail.com"})
        db.test.insert({name:"stephen",age:36,genda:"male",email:"stephen@hotmail.com"})
        db.test.insert({name:"stephen",age:37,genda:"male",email:"stephen@hotmail.com"})
        --多条件查询,等同于SQL语句的where name = "stephen" and age = 35
        db.test.find({name:"stephen",age:35})
        { "_id" : ObjectId("59f1ae4388f3edba94091894"), "name" : "stephen", "age" : 35, "genda" : "male", "email" : "stephen@hotmail.com" }
   --返回指定的文档键值对,只返回name和age键值对
    db.test.find({},{name:1,age:1})
    { "_id" : ObjectId("59f1ae4388f3edba94091894"), "name" : "stephen", "age" : 35 }
    { "_id" : ObjectId("59f1ae8588f3edba94091895"), "name" : "stephen", "age" : 36 }
    { "_id" : ObjectId("59f1ae8a88f3edba94091896"), "name" : "stephen", "age" : 37 }
2、查询条件
    MongoDB提供了一组比较操作符:$lt/$lte/$gt/$gte/$ne,依次等价于</<=/>/>=/!=
        --返回符合条件age >= 18 && age <= 40的文档       
        db.test.find({age:{$gte:35,$lte:36}})
    { "_id" : ObjectId("59f1ae4388f3edba94091894"), "name" : "stephen", "age" : 35, "genda" : "male", "email" : "stephen@hotmail.com" }
    { "_id" : ObjectId("59f1ae8588f3edba94091895"), "name" : "stephen", "age" : 36, "genda" : "male", "email" : "stephen@hotmail.com" }
        --返回指定的键值对条件age >= 18 && age <= 40的文档
        db.test.find({age:{$gte:35,$lte:36}},{name:1,age:1})
        { "_id" : ObjectId("59f1ae4388f3edba94091894"), "name" : "stephen", "age" : 35 }
    { "_id" : ObjectId("59f1ae8588f3edba94091895"), "name" : "stephen", "age" : 36 }
        --返回条件符合name != "stephen1"
        db.test.find({name:{$ne:"stephen"}})
    { "_id" : ObjectId("59f1b20588f3edba94091898"), "name" : "stephen1", "age" : 35, "genda" : "male", "email" : "stephen@hotmail.com" }
    --$in等同于SQL中的in,下面的示例等同于SQL中的in ("stephen","stephen1")
        db.test.find({name:{$in:["stephen","stephen2"]}})
        --下面的示例等同于name = "stephen1" or age = 35
        db.test.find({$or:[{name:"stephen1"},{age:36}]})
        { "_id" : ObjectId("59f1ae8588f3edba94091895"), "name" : "stephen", "age" : 36, "genda" : "male", "email" : "stephen@hotmail.com" }
    { "_id" : ObjectId("59f1b20588f3edba94091898"), "name" : "stephen1", "age" : 35, "genda" : "male", "email" : "stephen@hotmail.com" }
        --下面的示例演示了如何混合使用$or和$in
        db.test.find({$or:[{name:{$in:["stephen1","setphen12"]}},{age:36}]})
3、NULL数据类型的查询
    --在进行值为null数据的查询时,所有值为null,以及不包括指定键的文档均会被检索出来
    db.test.find({"x":null})
        db.test.find({a:null})
    { "_id" : ObjectId("59f1ae4388f3edba94091894"), "name" : "stephen", "age" : 35, "genda" : "male", "email" : "stephen@hotmail.com" }
    { "_id" : ObjectId("59f1ae8588f3edba94091895"), "name" : "stephen", "age" : 36, "genda" : "male", "email" : "stephen@hotmail.com" }
    { "_id" : ObjectId("59f1ae8a88f3edba94091896"), "name" : "stephen", "age" : 37, "genda" : "male", "email" : "stephen@hotmail.com" }
    { "_id" : ObjectId("59f1b1fd88f3edba94091897"), "name" : "stephen", "age" : 35, "genda" : "male", "email" : "stephen@hotmail.com" }
    { "_id" : ObjectId("59f1b20588f3edba94091898"), "name" : "stephen1", "age" : 35, "genda" : "male", "email" : "stephen@hotmail.com" }
        --再有就是通过$exists判断指定键是否存在
        db.test.find({x:{$in:[null],$exists:true}})
    { "_id" : ObjectId("59f1c40b88f3edba94091899"), "x" : null }
4、正则查询
    db.test.find()       
        --正则语法
        db.test.find({name:/1/})
    { "_id" : ObjectId("59f1b20588f3edba94091898"), "name" : "stephen1", "age" : 35, "genda" : "male", "email" : "stephen@hotmail.com" }
        --i表示忽略大小写
        db.test.find({name:/stephen?/i})
5、数组数据查询
    db.wqq.insert({name:["aa","bb","cc"]})
        db.wqq.insert({name:["aa","ee","cc"]})
    db.wqq.insert({name:["ff","ee","cc"]})
        --数组中所有包含banana的文档都会被检索出来
        db.wqq.find({name:"aa"})
        { "_id" : ObjectId("59f1c60388f3edba9409189c"), "name" : [ "aa", "bb", "cc" ] }
    { "_id" : ObjectId("59f1c6b988f3edba9409189d"), "name" : [ "aa", "ee", "cc" ] }
        --检索数组中需要包含多个元素的情况,这里使用$all。下面的示例中,数组中必须同时包含ff和ee
         db.wqq.find({name:{$all:["ff","cc"]}})
         { "_id" : ObjectId("59f1c6ba88f3edba9409189e"), "name" : [ "ff", "ee", "cc" ] }
        --精确匹配即被检索出来的文档,name值中的数组数据必须和查询条件完全匹配,即不能多,也不能少,顺序也必须保持一致
    db.wqq.find({name:["ff","ee","cc"]})       
        { "_id" : ObjectId("59f1c6ba88f3edba9409189e"), "name" : [ "ff", "ee", "cc" ] }
        --匹配数组中指定下标元素的值,数组的起始下标是0
        db.wqq.find({"name.1":"bb"})
    { "_id" : ObjectId("59f1c60388f3edba9409189c"), "name" : [ "aa", "bb", "cc" ] }
        --可以通过$size获取数组的长度,但是$size不能和比较操作符联合使用
        db.wqq.find({"name":{$size:3}})
    { "_id" : ObjectId("59f1c60388f3edba9409189c"), "name" : [ "aa", "bb", "cc" ] }
    { "_id" : ObjectId("59f1c6b988f3edba9409189d"), "name" : [ "aa", "ee", "cc" ] }
    { "_id" : ObjectId("59f1c6ba88f3edba9409189e"), "name" : [ "ff", "ee", "cc" ] }
        --如果需要检索size > n的结果,不能直接使用$size,只能是添加一个额外的键表示数据中的元素数据,在操作数据中的元素时,需要同时更新size键的值
        --为后面的实验构造数据
        db.wqq.update({}, {"$set": {"size":3}},false,true)
    WriteResult({ "nMatched" : 3, "nUpserted" : 0, "nModified" : 3 })
    > db.wqq.find()
    { "_id" : ObjectId("59f1c60388f3edba9409189c"), "name" : [ "aa", "bb", "cc" ], "size" : 3 }
    { "_id" : ObjectId("59f1c6b988f3edba9409189d"), "name" : [ "aa", "ee", "cc" ], "size" : 3 }
    { "_id" : ObjectId("59f1c6ba88f3edba9409189e"), "name" : [ "ff", "ee", "cc" ], "size" : 3 }
        --每次添加一个新元素,都要原子性的自增size一次
        db.wqq.update({},{$push:{name:123},$inc:{size:1}},0,1)
    WriteResult({ "nMatched" : 3, "nUpserted" : 0, "nModified" : 3 })
    > db.wqq.find()
    { "_id" : ObjectId("59f1c60388f3edba9409189c"), "name" : [ "aa", "bb", "cc", 123 ], "size" : 4 }
    { "_id" : ObjectId("59f1c6b988f3edba9409189d"), "name" : [ "aa", "ee", "cc", 123 ], "size" : 4 }
    { "_id" : ObjectId("59f1c6ba88f3edba9409189e"), "name" : [ "ff", "ee", "cc", 123 ], "size" : 4 }
        --通过$slice返回数组中的部分数据。"$slice":2表示数组中的前两个元素
        db.wqq.find({},{name:{$slice:2},size:0})   //若为负数则是最后2个元素
    { "_id" : ObjectId("59f1c60388f3edba9409189c"), "name" : [ "aa", "bb" ] }
    { "_id" : ObjectId("59f1c6b988f3edba9409189d"), "name" : [ "aa", "ee" ] }
    { "_id" : ObjectId("59f1c6ba88f3edba9409189e"), "name" : [ "ff", "ee" ] }
        --$slice : [2,1],表示从第二个2元素开始取1个,如果获取数量大于2后面的元素数量,则取后面的全部数据
        db.wqq.find({},{name:{$slice:[2,1]}})
        db.wqq.find({},{name:{$slice:[2,1]},size:0})
   { "_id" : ObjectId("59f1c60388f3edba9409189c"), "name" : [ "cc" ] }
   { "_id" : ObjectId("59f1c6b988f3edba9409189d"), "name" : [ "cc" ] }
   { "_id" : ObjectId("59f1c6ba88f3edba9409189e"), "name" : [ "cc" ] }
6、内嵌文档查询
    --当嵌入式文档为数组时,需要$elemMatch操作符来帮助定位某一个元素匹配的情况,否则嵌入式文件将进行全部的匹配
    --即检索时需要将所有元素都列出来作为查询条件方可
    db.aa.insert({comments:[{author:"sharesoe",score:3},{author:"mary",score:6}]})
    WriteResult({ "nInserted" : 1 })
    > db.aa.find()
    { "_id" : ObjectId("59f1cc8e88f3edba9409189f"), "comments" : [ { "author" : "sharesoe", "score" : 3 }, { "author" : "mary", "score" : 6 } ] }
          --
        db.aa.find({comments:{$elemMatch:{author:"sharesoe",score:{$gte:3}}}})
    { "_id" : ObjectId("59f1cc8e88f3edba9409189f"), "comments" : [ { "author" : "sharesoe", "score" : 3 }, { "author" : "mary", "score" : 6 } ] }


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-406028-1-1.html 上篇帖子: mongo启动报错 下篇帖子: mongo创建新用户与数据库
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表