Mongodb学习笔记五、查询操作

今天阅读了官方文档(查询功能的教程):

给出链接:http://docs.mongodb.org/manual/tutorial/query-documents/

MongoDB中使用db.collection.find()从集合中搜寻文档。它返回获取到的文档的一个游标(_id这个键一直都会返回)。

一、查询一个集合中的所有文档

空的查询文档({})将查询出集合中的所有文档。

db.inventory.find( {} )

查询时,空文档和不指定查询文档的作用是一样的。下面这个查询等价于上面的查询:

db.inventory.find()
二、等式查询

使用查询文档  { <field>: <value> } 来查询所有包含<field>字段,且值为<value>的文档。

三、根据查询操作符来指定查询条件

下面的查询将查找到inventory集合中type字段的值为'food' or 'snacks'的所有文档:

db.inventory.find( { type: { $in: [ 'food', 'snacks' ] } } )
其实就是IN操作啦。
四、AND条件

下面的查询将查找出type字段的值为foot并且price字段的值小于9.95的所有文档。

db.inventory.find( { type: 'food', price: { $lt: 9.95 } } )
ps:"$lt"(小于) 就是less than的缩写啦
五、OR条件
db.inventory.find(
   {
     $or: [ { qty: { $gt: 100 } }, { price: { $lt: 9.95 } } ]
   }
)

ps:"$gt"(大于) 就是greater than的缩写

六、同时使用AND和OR条件
db.inventory.find(
   {
     type: 'food',
     $or: [ { qty: { $gt: 100 } }, { price: { $lt: 9.95 } } ]
   }
)
内嵌文档
七、内嵌文档的精确匹配
db.inventory.find(
    {
      producer:
        {
          company: 'ABC123',
          address: '123 Street'
        }
    }
)

八、在内嵌文档中根据字段相等匹配

producer是个内嵌文档,包含了字段 company,还有可能包含其它字段。

下面的查询使用点记法来查找所有的producer中的company字段值为并且value为'ABC123'的所有文档。

db.inventory.find( { 'producer.company': 'ABC123' } )


数组

当字段中有数组的时候,可以指定精确地数组或者指定数组中的值来匹配。如果字段中有嵌入文档,你可以用点记法来查询嵌入文档中的字段。

如果使用$elemMatch操作符指定了多条查询条件,数组必须包含至少一条元素满足所有条件。

如果指定了多条查询条件,并且没有使用$elemMatch操作符,那么,一些数组元素的结合,不是某一个单独的元素的必须条件,必须满足所有条件(就是所写的数组要和查找的数组完全一致啦,实在不会翻译了)。

ps:$elemMatch操作符用法:

{ <field>: { $elemMatch: { <query1>, <query2>, ... } } }
db.scores.find(
   { results: { $elemMatch: { $gte: 80, $lt: 85 } } }
)
db.survey.find(
   { results: { $elemMatch: { product: "xyz" } } }
)
db.survey.find(
   { "results.product": "xyz" }
)

九、精确匹配一个数组

查询数组,使用文档{ <field>: <value> },<value>是要匹配的数组,数组的元素和顺序要完全一致。

下面的例子将查询出字段ratings是个数组,并且包含了三个元素,5,8和9,并且按此顺序排列的所有元素。

db.inventory.find( { ratings: [ 5, 8, 9 ] } )

十、匹配数组中的元素

db.inventory.find( { ratings: 5 } )

十一、匹配数组中的特殊元素

ratings数组第一个元素是5的文档

db.inventory.find( { 'ratings.0': 5 } )

对数组元素进行多条件查询


十二、只有单个元素满足条件

使用$elemMatch操作符来指定对数组元素的多条件查询,所查询的结果至少有一个元素满足所有条件。


下面的例子将查询出ratings数组至少包含一条大于5并且小于9的元素的文档。

db.inventory.find( { ratings: { $elemMatch: { $gt: 5, $lt: 9 } } } )
上面的操作返回下面的文档,其ratings数组包含了满足条件的元素8.
{ "_id" : 5, "type" : "food", "item" : "aaa", "ratings" : [ 5, 8, 9 ] }
{ "_id" : 7, "type" : "food", "item" : "ccc", "ratings" : [ 9, 5, 8 ] }

十三、满足条件的元素的组合

其实就是OR条件啦。

下面的例子将返回大于5或者小于9的所有文档。

db.inventory.find( { ratings: { $gt: 5, $lt: 9 } } )


嵌入文档的数组

先假设有如下文档:

{
  _id: 100,
  type: "food",
  item: "xyz",
  qty: 25,
  price: 2.5,
  ratings: [ 5, 8, 9 ],
  memos: [ { memo: "on time", by: "shipping" }, { memo: "approved", by: "billing" } ]
}

{
  _id: 101,
  type: "fruit",
  item: "jkl",
  qty: 10,
  price: 4.25,
  ratings: [ 5, 9 ],
  memos: [ { memo: "on time", by: "payment" }, { memo: "delayed", by: "shipping" } ]
}

一、使用数组下标来匹配嵌入文档的字段

db.inventory.find( { 'memos.0.by': 'shipping' } )

上面查询返回的结果如下:

{
   _id: 100,
   type: "food",
   item: "xyz",
   qty: 25,
   price: 2.5,
   ratings: [ 5, 8, 9 ],
   memos: [ { memo: "on time", by: "shipping" }, { memo: "approved", by: "billing" } ]
}

二、不指定数组下标来查询字段

db.inventory.find( { 'memos.by': 'shipping' } )

上面的查询将返回所有的记录。


对文档的数组指定多条件

三、使用$elemMatch操作符指定多个条件来在嵌入文档中查询满足所有条件的文档。

db.inventory.find(
   {
     memos:
       {
          $elemMatch:
            {
               memo: 'on time',
               by: 'shipping'
            }
       }
    }
)
返回结果如下:

{
   _id: 100,
   type: "food",
   item: "xyz",
   qty: 25,
   price: 2.5,
   ratings: [ 5, 8, 9 ],
   memos: [ { memo: "on time", by: "shipping" }, { memo: "approved", by: "billing" } ]
}

四、满足条件的元素的组合

db.inventory.find(
  {
    'memos.memo': 'on time',
    'memos.by': 'shipping'
  }
)
以上查询将返回所有的记录。


补充:

null查询

null不仅仅匹配自身,而且匹配“不存在的”,所以,这种匹配还会返回缺少这个键的文档。

如果仅仅想要匹配键值为Null的文档,既要坚持该键值是否为null,还要通过"$exists"条件判定键值已经存在:

db.b.find({"z":{"$in":[null], "$exists":true}})

ps:meiyou "$eq"操作符,但是"$in"只有一个元素也可以实现。


“$not” 返回不匹配。

“$mod” 取模


MongoDB使用Perl兼容的正则表达式库来匹配正则表达式。查询条件中可以使用。


“$all”  通过多个元素来匹配数组

db.food.find({fruit:{$all:["apple", "banana"]}}) 将查找既有“banana”又有“apple”的文档。


$size  查询指定长度的数组

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


$slice返回一个数组的子集合

db.blog.posts.findOne(criteria, {"comments":{"$slice":10}}) 返回前十条评论

也可以这么写:“$slice”:[23, 10]   从弟23个元素开始,返回后面十个元素。

也可以:“$slice:-1” 返回最后一条


limit、ship、和sort

db.b.find().limit(3) 返回满足条件的最多三个元素。

db.b.find().skip(3) 略过满足条件的前三个文档,返回剩余的文档。

sort 排序,升序是1,降序是-1,如:db.b.find().sort({username:1, age:-1})    "username"升序,“age”降序。


date降序并返回前100页:

db.foo.find().sort({"date":-1}).limit(100)




指定查询返回字段:

Syntax Description
<field>: <1 or true> Specify the inclusion of a field.
<field>: <0 or false> Specify the suppression of the field.

db.inventory.find( { type: 'food' }, { item: 1, qty: 1, _id:0 } )

上面的例子将只返回符合条件的item,qty字段


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值