您的位置:首页 > 数据库 > 其它 > 正文

mongodb查询

更多 时间:2014-12-15 类别:数据库 浏览量:1345

mongodb查询

mongodb查询

一、基本查询

1、如果有如下一条记录,用findOne可以查找

  • 
    > db.test.findOne()
        {
             "_id" : ObjectId("4fd58ecbb9ac507e96276f1a"),
             "name" : "stephen",
             "age" : 35,
             "genda" : "male",
             "email" : ""
        } 
    
    		
  • 2、返回指定的文档键值对。下面的示例将只是返回name和age键值对

     > db.test.find({}, {"name":1,"age":1})
       { "_id" : ObjectId("4fd58ecbb9ac507e96276f1a"), "name" : "stephen", "age" : 35 }

    3、指定不返回的文档键值对。下面的示例将返回除name之外的所有键值对

    > db.test.find({}, {"name":0})
        { "_id" : ObjectId("4fd58ecbb9ac507e96276f1a"), "age" : 35, "genda" : "male", "email" : "" }

     

    二、查询条件

    MongoDB提供了一组比较操作符:$lt/$lte/$gt/$gte/$ne,依次等价于</<=/>/>=/!=。

     

    1、返回符合条件age >= 18 && age <= 40的文档

    > db.test.find({"age":{"$gte":18, "$lte":40}})

     

    2、返回条件符合name != "stephen1"

    > db.test.find({"name":{"$ne":"stephen1"}})

     

    3、$in等同于SQL中的in,下面的示例等同于SQL中的in ("stephen","stephen1")

    > db.test.find({"name":{"$in":["stephen","stephen1"]}})

     

    4、和SQL不同的是,MongoDB的in list中的数据可以是不同类型。这种情况可用于不同类型的别名场景

    > db.test.find({"name":{"$in":["stephen",123]}})

     

    5、$nin等同于SQL中的not in,同时也是$in的取反

    > db.test.find({"name":{"$nin":["stephen2","stephen1"]}})

     

    6、$or等同于SQL中的or,$or所针对的条件被放到一个数组中,每个数组元素表示or的一个条件。下面的示例等同于name = "stephen1" or age = 35

    > db.test.find({"$or": [{"name":"stephen1"}, {"age":35}]})

     

    7、混合使用$or和$in

    > db.test.find({"$or": [{"name":{"$in":["stephen","stephen1"]}}, {"age":36}]})

     

    8、$not表示取反,等同于SQL中的not

    > db.test.find({"name": {"$not": {"$in":["stephen2","stephen1"]}}})

     

    三、limit(),skip(),sort() 

    测试数据

  • 
    post1 = {"title":"learn MongoDB", "author":"Wilber", "date":new Date(), "score":90}
    post2 = {"title":"learn English", "author":"Will", "date":new Date(), "score":95}
    post3 = {"title":"learn C#", "author":"Li", "date":new Date(), "score":80}
    post4 = {"title":"learn SQL", "author":"July", "date":new Date(), "score":70}
    post5 = {"title":"learn Node", "author":"Wilber", "date":new Date(), "score":93}
    db.blog.posts.insert(post1)
    db.blog.posts.insert(post2)
    db.blog.posts.insert(post3)
    db.blog.posts.insert(post4)
    db.blog.posts.insert(post5)
    
    users1 = ["Wilber", "Will", "June"]
    users2 = ["Will", "July", "Wilber"]
    users3 = ["James", "Jack", "Will"]
    db.blog.users.insert({"users":users1})
    db.blog.users.insert({"users":users2})
    db.blog.users.insert({"users":users3})
    
    		
  • mongodb查询

  •  
  • SQL 代码   复制
  • 
    > db.blog.posts.find({"author":"Will"})
    
    { "_id" : ObjectId("5479c9f2421b7f1536cfb208"), "title" : "learn English", "author" : "Will", "date" : ISODate("2014-11-
    29T13:28:17.959Z"), "score" : 95 }
    
    > db.blog.posts.find({}, {"title":1, "score":1})
    
    { "_id" : ObjectId("5479c9f2421b7f1536cfb207"), "title" : "learn MongoDB", "score" : 90 }
    { "_id" : ObjectId("5479c9f2421b7f1536cfb208"), "title" : "learn English", "score" : 95 }
    { "_id" : ObjectId("5479c9f2421b7f1536cfb209"), "title" : "learn C#", "score" : 80 }
    { "_id" : ObjectId("5479c9f2421b7f1536cfb20a"), "title" : "learn SQL", "score" : 70 }
    { "_id" : ObjectId("5479c9f4421b7f1536cfb20b"), "title" : "learn Node", "score" : 93 }
    
    > db.blog.posts.find({}, {"date": 0})
    
    { "_id" : ObjectId("5479c9f2421b7f1536cfb207"), "title" : "learn MongoDB", "author" : "Wilber", "score" : 90 }
    { "_id" : ObjectId("5479c9f2421b7f1536cfb208"), "title" : "learn English", "author" : "Will", "score" : 95 }
    { "_id" : ObjectId("5479c9f2421b7f1536cfb209"), "title" : "learn C#", "author" : "Li", "score" : 80 }
    { "_id" : ObjectId("5479c9f2421b7f1536cfb20a"), "title" : "learn SQL", "author" : "July", "score" : 70 }
    { "_id" : ObjectId("5479c9f4421b7f1536cfb20b"), "title" : "learn Node", "author" : "Wilber", "score" : 93 }
    
    				
  •  

    四、null数据类型的查询

     

    1、在进行值为null数据的查询时,所有值为null,以及不包含指定键的文档均会被检索出来

    例如

  • 
    
    			

    > db.test.find({"x":null})

    { "_id" : ObjectId("4fd59d30b9ac507e96276f1b"), "x" : null } { "_id" : ObjectId("4fd59d49b9ac507e96276f1c"), "y" : 1 }

  •  

    2、查询集合c中y的值为null,(仅返回y的值为null的数据,不会返回不存在的)

     

    >db.c.find( { “y” : { $type : 10 } } )

    还有一种写法如下

    >db.c.find({“y”:{“$in”:[null], “$exists”:true}})

    3、查询集合c中y的值不存在(不会返回y的值为null的数据)

    >db.c.find( { “y” : { $exists : false } } )

     

    四、正则查询

    MongoDB中使用了Perl规则的正则语法

  •  
  • > db.test.find({"name":/stephen?/i})
    
    			

    { "_id" : ObjectId("4fd59ed7b9ac507e96276f1d"), "name" : "stephen" }

         { "_id" : ObjectId("4fd59edbb9ac507e96276f1e"), "name" : "stephen1" }

     

  •  

    五、基于数组的查找

    例如,有如下数据

    { "_id" : ObjectId("4fd5a177b9ac507e96276f1f"), "fruit" : [ "apple", "banana", "peach" ] }

    { "_id" : ObjectId("4fd5a18cb9ac507e96276f20"), "fruit" : [ "apple", "kumquat","orange" ] }

    { "_id" : ObjectId("4fd5a1f0b9ac507e96276f21"), "fruit" : [ "cherry", "banana","apple" ] }

    1、数组中所有包含banana的文档都会被检索出来

     

  • 
    
    			

    > db.test.find({"fruit":"banana"})

    { "_id" : ObjectId("4fd5a177b9ac507e96276f1f"), "fruit" : [ "apple", "banana", "peach" ] }

    { "_id" : ObjectId("4fd5a1f0b9ac507e96276f21"), "fruit" : [ "cherry", "banana","apple" ] }

     

  • 2、检索数组中需要包含多个元素的情况,这里使用$all。下面的示例中,数组中必须同时包含apple和banana,但是他们的顺序无关紧要

     

  • 
     > db.test.find({"fruit": {"$all": ["banana","apple"]}})
    
    			

    { "_id" : ObjectId("4fd5a177b9ac507e96276f1f"), "fruit" : [ "apple", "banana", "peach" ] }

    { "_id" : ObjectId("4fd5a1f0b9ac507e96276f21"), "fruit" : [ "cherry", "banana", "apple" ] }

     

  • 3、下面的示例表示精确匹配,即被检索出来的文档,fruit值中的数组数据必须和查询条件完全匹配,即不能多,也不能少,顺序也必须保持一致

     

  • 
    > db.test.find({"fruit":["apple","banana","peach"]})
    
    			

    { "_id" : ObjectId("4fd5a177b9ac507e96276f1f"), "fruit" : [ "apple", "banana", peach" ] }

     

  • 4、下面的示例将匹配数组中指定下标元素的值。数组的起始下标是0

     

  • 
    > db.test.find({"fruit.2":"peach"})
    
    			

    { "_id" : ObjectId("4fd5a177b9ac507e96276f1f"), "fruit" : [ "apple", "banana", peach" ] }

     

  • 5、可以通过$size获取数组的长度,但是$size不能和比较操作符联合使用

     

  • 
    
    			

    > db.test.find({"fruit": {$size : 3}})

    { "_id" : ObjectId("4fd5a177b9ac507e96276f1f"), "fruit" : [ "apple", "banana", "peach" ] }

    { "_id" : ObjectId("4fd5a18cb9ac507e96276f20"), "fruit" : [ "apple", "kumquat","orange" ] }

    { "_id" : ObjectId("4fd5a1f0b9ac507e96276f21"), "fruit" : [ "cherry", "banana","apple" ] }

     

  •  

    标签:mongodb