zoukankan      html  css  js  c++  java
  • Mongodb文档查询

    MongoDB 查询数据的语法格式如下:

    db.collection.find(query, projection)

    • query :可选,使用查询操作符指定查询条件
    • projection :可选,使用投影操作符指定返回的键。查询时返回文档中所有键值, 只需省略该参数即可(默认省略)。

         

    比较操作:

    操作

    格式

    等于

    {<key>:<value>}

    小于

    {<key>:{$lt:<value>}}

    小于或等于

    {<key>:{$lte:<value>}}

    大于

    {<key>:{$gt:<value>}}

    大于或等于

    {<key>:{$gte:<value>}}

    不等于

    {<key>:{$ne:<value>}}

         

    MongoDB AND 条件

    MongoDB 的 find() 方法可以传入多个键(key),每个键(key)以逗号隔开,及常规 SQL 的 AND 条件。

    语法格式如下:

    >db.col.find({key1:value1, key2:value2})

         

    MongoDB OR 条件

    MongoDB OR 条件语句使用了关键字 $or,语法格式如下:

    >db.col.find(
    
       {
    
          $or: [
    
                  {key1: value1}, {key2:value2}
    
          ]
    
       }
    
    )
    
    1.    > db.person.find()
    2. { "_id" : ObjectId("592ffd872108e8e79ea902b0"), "name" : "zjf", "age" : 30 }
    3. { "_id" : ObjectId("593011c8a92497992cdfac10"), "name" : "xhj", "age" : 28 }
    4. { "_id" : ObjectId("59301270a92497992cdfac11"), "name" : "zzj", "age" : 2 }
    5. > db.person.find(age:{$gt:5})
    6. 2017-06-01T21:12:33.114+0800 E QUERY??? SyntaxError: Unexpected token :
    7. > db.person.find({age:{$gt:5}})
    8. { "_id" : ObjectId("592ffd872108e8e79ea902b0"), "name" : "zjf", "age" : 30 }
    9. { "_id" : ObjectId("593011c8a92497992cdfac10"), "name" : "xhj", "age" : 28 }
    10. > db.person.find({age:2,name:'zzj'})
    11. { "_id" : ObjectId("59301270a92497992cdfac11"), "name" : "zzj", "age" : 2 }
    12. > db.person.find({$or:[{age:2},{name:'xhj'}]})
    13. { "_id" : ObjectId("593011c8a92497992cdfac10"), "name" : "xhj", "age" : 28 }
    14. { "_id" : ObjectId("59301270a92497992cdfac11"), "name" : "zzj", "age" : 2 }

         

    MongoDB $type 操作符

       

    描述

    在本章节中,我们将继续讨论MongoDB中条件操作符 $type。

    $type操作符是基于BSON类型来检索集合中匹配的数据类型,并返回结果。

    MongoDB 中可以使用的类型如下表所示:

    类型

    数字

    Double

    1

    String

    2

    Object

    3

    Array

    4

    Binary data

    5

    Undefined

    6

    Object id

    7

    Boolean

    8

    Date

    9

    Null

    10

    Regular Expression

    11

    JavaScript

    13

    Symbol

    14

    JavaScript (with scope)

    15

    32-bit integer

    16

    Timestamp

    17

    64-bit integer

    18

    Min key

    255

    Max key

    127

         

    1. > db.person.insert({name:'my second child',age:'i do not know'})
    2. WriteResult({ "nInserted" : 1 })
    3. > db.person.find()
    4. { "_id" : ObjectId("592ffd872108e8e79ea902b0"), "name" : "zjf", "age" : 30 }
    5. { "_id" : ObjectId("593011c8a92497992cdfac10"), "name" : "xhj", "age" : 28 }
    6. { "_id" : ObjectId("59301270a92497992cdfac11"), "name" : "zzj", "age" : 2 }
    7. { "_id" : ObjectId("593015fda92497992cdfac12"), "name" : "my second child", "age" : "i do not know" }
    8. > db.person.find({age:{$type:2}})
    9. { "_id" : ObjectId("593015fda92497992cdfac12"), "name" : "my second child", "age" : "i do not know" }

         

    MongoDB Limit() 方法

    如果你需要在MongoDB中读取指定数量的数据记录,可以使用MongoDB的Limit方法,limit()方法接受一个数字参数,该参数指定从MongoDB中读取的记录条数。

    语法

    limit()方法基本语法如下所示:

    >db.COLLECTION_NAME.find().limit(NUMBER)
    

         

    MongoDB Skip() 方法

    我们除了可以使用limit()方法来读取指定数量的数据外,还可以使用skip()方法来跳过指定数量的数据,skip方法同样接受一个数字参数作为跳过的记录条数。

    语法

    skip() 方法脚本语法格式如下:

    >db.COLLECTION_NAME.find().limit(NUMBER).skip(NUMBER)
    

    实例

    以上实例只会显示第二条文档数据

    >db.col.find({},{"title":1,_id:0}).limit(1).skip(1)
    
    { "title" : "Java 教程" }
    

         

    MongoDB sort()方法

    在MongoDB中使用使用sort()方法对数据进行排序,sort()方法可以通过参数指定排序的字段,并使用 1 和 -1 来指定排序的方式,其中 1 为升序排列,而-1是用于降序排列。

    语法

    sort()方法基本语法如下所示:

    >db.COLLECTION_NAME.find().sort({KEY:1})
    

    MongoDB findOne()方法

    与find不一样的是,只查询出一条数据就停止查询了。

    $all匹配所有

    1. > db.data.save({x:[1,2]})
    2. WriteResult({ "nInserted" : 1 })
    3. > db.data.save({x:[1,2,3]})
    4. WriteResult({ "nInserted" : 1 })
    5. > db.data.save({x:[2,3]})
    6. WriteResult({ "nInserted" : 1 })
    7. > db.data.find();
    8. { "_id" : ObjectId("593177646a6bb0f03293efe1"), "x" : 0 }
    9. { "_id" : ObjectId("593177646a6bb0f03293efe2"), "x" : 1 }
    10. { "_id" : ObjectId("593177646a6bb0f03293efe3"), "x" : 2 }
    11. { "_id" : ObjectId("593177646a6bb0f03293efe4"), "x" : 3 }
    12. { "_id" : ObjectId("593177646a6bb0f03293efe5"), "x" : 4 }
    13. { "_id" : ObjectId("593177646a6bb0f03293efe6"), "x" : 5 }
    14. { "_id" : ObjectId("593177646a6bb0f03293efe7"), "x" : 6 }
    15. { "_id" : ObjectId("593177646a6bb0f03293efe8"), "x" : 7 }
    16. { "_id" : ObjectId("593177646a6bb0f03293efe9"), "x" : 8 }
    17. { "_id" : ObjectId("593177646a6bb0f03293efea"), "x" : 9 }
    18. { "_id" : ObjectId("59317e6c6a6bb0f03293efeb"), "x" : [ 1, 2 ] }
    19. { "_id" : ObjectId("59317e736a6bb0f03293efec"), "x" : [ 1, 2, 3 ] }
    20. { "_id" : ObjectId("59317e786a6bb0f03293efed"), "x" : [ 2, 3 ] }
    21. > db.data.find({x:[1,2]});
    22. { "_id" : ObjectId("59317e6c6a6bb0f03293efeb"), "x" : [ 1, 2 ] }
    23. > db.data.find({x:{$all:[1,2]}});
    24. { "_id" : ObjectId("59317e6c6a6bb0f03293efeb"), "x" : [ 1, 2 ] }
    25. { "_id" : ObjectId("59317e736a6bb0f03293efec"), "x" : [ 1, 2, 3 ] }

    $exists 某一列是否有存在

    与是否为空不一样 如果某一列存在的值是null 那么他是存在但是为空的。

    1.   > db.data.save({x:1,y:2});
    2. WriteResult({ "nInserted" : 1 })
    3. > db.data.find({y:{$exists:ture}})
    4. 2017-06-02T23:07:01.390+0800 E QUERY ReferenceError: ture is not defined
    5.     at (shell):1:26
    6. > db.data.find({y:{$exists:true}})
    7. { "_id" : ObjectId("59317efe6a6bb0f03293efee"), "x" : 1, "y" : 2 }
    8. > db.data.save({x:1,y:null});
    9. WriteResult({ "nInserted" : 1 })
    10. > db.data.find({y:{$exists:true}})
    11. { "_id" : ObjectId("59317efe6a6bb0f03293efee"), "x" : 1, "y" : 2 }
    12. { "_id" : ObjectId("59317fd76a6bb0f03293efef"), "x" : 1, "y" : null }

    $in 和判断是否为空

    查询是否为空会把不存在的也查询出来 所以有时候要结合$exists一起来查询

    > db.data.find({y:null});

    { "_id" : ObjectId("593177646a6bb0f03293efe1"), "x" : 0 }

    { "_id" : ObjectId("593177646a6bb0f03293efe2"), "x" : 1 }

    { "_id" : ObjectId("593177646a6bb0f03293efe3"), "x" : 2 }

    { "_id" : ObjectId("593177646a6bb0f03293efe4"), "x" : 3 }

    { "_id" : ObjectId("593177646a6bb0f03293efe5"), "x" : 4 }

    { "_id" : ObjectId("593177646a6bb0f03293efe6"), "x" : 5 }

    { "_id" : ObjectId("593177646a6bb0f03293efe7"), "x" : 6 }

    { "_id" : ObjectId("593177646a6bb0f03293efe8"), "x" : 7 }

    { "_id" : ObjectId("593177646a6bb0f03293efe9"), "x" : 8 }

    { "_id" : ObjectId("593177646a6bb0f03293efea"), "x" : 9 }

    { "_id" : ObjectId("59317e6c6a6bb0f03293efeb"), "x" : [ 1, 2 ] }

    { "_id" : ObjectId("59317e736a6bb0f03293efec"), "x" : [ 1, 2, 3 ] }

    { "_id" : ObjectId("59317e786a6bb0f03293efed"), "x" : [ 2, 3 ] }

    { "_id" : ObjectId("59317fd76a6bb0f03293efef"), "x" : 1, "y" : null }

    > db.data.find({y:{$in:null,$exists:true}});

    Error: error: {

        "$err" : "Can't canonicalize query: BadValue $in needs an array",

        "code" : 17287

    }

    > db.data.find({y:{$in:[null],$exists:true}});

    { "_id" : ObjectId("59317fd76a6bb0f03293efef"), "x" : 1, "y" : null }

    $ne:不等于

    $nin:no in

    $mod:取模运算

    1. > db.data.find({x:{$mod:[3,1]}});
    2. { "_id" : ObjectId("593177646a6bb0f03293efe2"), "x" : 1 }
    3. { "_id" : ObjectId("593177646a6bb0f03293efe5"), "x" : 4 }
    4. { "_id" : ObjectId("593177646a6bb0f03293efe8"), "x" : 7 }
    5. { "_id" : ObjectId("59317e6c6a6bb0f03293efeb"), "x" : [ 1, 2 ] }
    6. { "_id" : ObjectId("59317e736a6bb0f03293efec"), "x" : [ 1, 2, 3 ] }
    7. { "_id" : ObjectId("59317efe6a6bb0f03293efee"), "x" : 1, "y" : 2 }
    8. { "_id" : ObjectId("59317fd76a6bb0f03293efef"), "x" : 1, "y" : null }

    $size 数组元素个数

    $not 非

    正则表达式支持:

    1. > db.person.find();
    2. { "_id" : ObjectId("593011c8a92497992cdfac10"), "name" : "xhj", "age" : 30, "address" : DBRef("address", ObjectId("59314b07e693aae7a5eb72ab")) }
    3. { "_id" : ObjectId("59301270a92497992cdfac11"), "name" : "zzj", "age" : 2 }
    4. { "_id" : ObjectId("593015fda92497992cdfac12"), "name" : "my second child", "age" : "i do not know" }
    5. { "_id" : ObjectId("592ffd872108e8e79ea902b0"), "name" : "zjf", "age" : 30, "address" : { "province" : "河南省", "city" : "南阳市", "building" : "桐柏县" } }
    6. > db.person.find({name:/z/});
    7. { "_id" : ObjectId("59301270a92497992cdfac11"), "name" : "zzj", "age" : 2 }
    8. { "_id" : ObjectId("592ffd872108e8e79ea902b0"), "name" : "zjf", "age" : 30, "address" : { "province" : "河南省", "city" : "南阳市", "building" : "桐柏县" } }
    9. > db.person.find({name:/^z/});
    10. { "_id" : ObjectId("59301270a92497992cdfac11"), "name" : "zzj", "age" : 2 }
    11. { "_id" : ObjectId("592ffd872108e8e79ea902b0"), "name" : "zjf", "age" : 30, "address" : { "province" : "河南省", "city" : "南阳市", "building" : "桐柏县" } }
    12. > db.person.find({name:/^z..$/});
    13. { "_id" : ObjectId("59301270a92497992cdfac11"), "name" : "zzj", "age" : 2 }
    14. { "_id" : ObjectId("592ffd872108e8e79ea902b0"), "name" : "zjf", "age" : 30, "address" : { "province" : "河南省", "city" : "南阳市", "building" : "桐柏县" } }
    15. > db.person.find({name:{$not:/^z..$/}});
    16. { "_id" : ObjectId("593011c8a92497992cdfac10"), "name" : "xhj", "age" : 30, "address" : DBRef("address", ObjectId("59314b07e693aae7a5eb72ab")) }
    17. { "_id" : ObjectId("593015fda92497992cdfac12"), "name" : "my second child", "age" : "i do not know" }
    18. >

     嵌入文档上的查询:

    精确匹配:db.users.find( { favorites: { artist: "Picasso", food: "pizza" } } )

    匹配嵌入文档的属性:db.users.find( { "favorites.artist": "Picasso" } )

    数组查询

    精确匹配:db.users.find( { badges: [ "blue", "black" ] } )

    匹配包含一个数组元素:db.users.find( { badges: "black" } )

    匹配指定元素:db.users.find( { "badges.0": "black" } )

    内嵌文档数组:

    匹配书第一个使用者的的年龄小于等于55的书:db.book.find( { 'users.0.age': { $lte: 55 } } )
    匹配书使用者中包含至少一个的年龄小于等于55的书:db.book.find( { 'users.age': { $lte: 55 } } )
    匹配书使用者中包含至少一个的年龄小于等于55并且为男性的书:db.book.find( { users: { $elemMatch: { age: { $lte: 55 }, sex: 'man' } } } )
    匹配书使用者中包含至少一个的年龄小于等于55并且包含至少一个使用者为男性的书:db.book.find( { "users.age": { $lte: 55 }, "users.sex": 'man' } )
    匹配书使用者中有3个的书:db.book.find( { 'users': { $size:3 } } ) 注意$size只支持等值查询
    
    

     限制返回的列:

    db.person.find(age:{$gt:5},{age:1,name:1})

    返回_id,name,age列。

    db.person.find(age:{$gt:5},{age:1,name:1,address:{$slice:10}})

    返回_id,name,age列,address列(这里假设它为数组)只返回前10条。

    db.person.find(age:{$gt:5},{age:1,name:1,address:{$slice:-10}})

    返回_id,name,age列,address列(这里假设它为数组)只返回最后10条。

    db.person.find(age:{$gt:5},{age:1,name:1,address:{$slice:[20,10]}})

    返回_id,name,age列,address列(这里假设它为数组)只返回 从第20条开始之后的10条。

     

     正则表达式查询:

    db.person.find({'name':/zjf|xhj/i})

     

  • 相关阅读:
    Nim or not Nim? hdu3032 SG值打表找规律
    Maximum 贪心
    The Super Powers
    LCM Cardinality 暴力
    Longge's problem poj2480 欧拉函数,gcd
    GCD hdu2588
    Perfect Pth Powers poj1730
    6656 Watching the Kangaroo
    yield 小用
    wpf DropDownButton 源码
  • 原文地址:https://www.cnblogs.com/xiaolang8762400/p/6930896.html
Copyright © 2011-2022 走看看