MongoDB学习日记(五):CRUD - find

基本语法

> db.collection.find({})

( 注意:MongoDB 中 find() 中的 { } 的内容是一个文本对象 document )

另外你可以使用 pretty( ) 方法使输出结果格式化

> db.collection.find({}).pretty()

特殊符号

MongoDB 中的 “>”、”<”等采用的是 “$” + “gt” 这样的代码,同样And、Or、In、NotIn也类似:

  • > : $gt
  • >= : $gte
  • < : $lt
  • <= : $lte
  • != : $ne
  • = : 空
  • And : 空
  • Or : $or
  • In : $in
  • NotIn : $nin

示例:

/* find age > 20 */
> db.user.find( 
>     { age : { $gt : 20 } } 
> )

/* find age >= 20 */
> db.user.find( 
>     { age : { $gte : 20 } } 
> )

/* find age < 20 */
> db.user.find(
>     { age : { $lt : 20 } }
> )

/* find age <= 20 */
> db.user.find(
>     { age : { $lte : 20 } } 
> )

/* find age != 20 */
> db.user.find(
>     { age : { $ne : 20 } } 
> )

/* find age == 20 */
> db.user.find(
>     { age : 20 } 
> )

/* find name == "admin" and age == 20 */
> db.user.find(
>     { name : "admin" , age : 20 }
> )

/* find age == 25 and age == 20 */
> db.user.find(
>     { $or : [ 
>         { age : 20 } ,
>         { age : 25 }
>     ] }
> )

/* find age in 20 , 25 , 30 */
> db.user.find(
>     { 
>         age : { $in : [ 20 , 25 , 30] }
>     }
> )

/* find age NotIn 20 , 25 , 30 */
> db.user.find(
>     { 
>         age : { $nin : [ 20 , 25 , 30] }
>     }
> )

(注:MongoDB 客户端支持注释:/**/)


Embedded Documents

如果你存的 document 中有 document,结构如下:

user {
    ...
    address : { 
        privonce : "shanghai",
        city : "shanghai"
    }
}

那么你查询的时候就需要注意了,如果你想要指定查询 city 为上海的 user,你有两种查询方式:

  1. 通过 address 查询:查询时必须与 address 的内容一致,顺序也不能错
  2. 通过 address.city 查询 : 查询时必须加上 “” 不然会报错
> db.user.find( 
>   {
>     address : { 
>       privonce : "shanghai",
>       city : "shanghai"
>     }
>   }
> )

> db.user.find( 
>   {
>     "address.city" : "shanghai"
>   }
> )

Arrays

结构 :

user {
    ...
    group : [ "news" , "sport" ],
    number : [ 1 , 2 , 3 ],
    parents : [ 
        { name : "father" , age : 50 } , 
        { name : "mother" , age : 45 } 
    ]
}

查询方法(以 number 为例):

  • 全文匹配:number : [ 1 , 2 , 3 ]
  • 等于某个属性的值(特殊符号):number : 1
  • 下标检索:"number.0" : 1
  • $elemMatch:number : { $elemMatch : { $gt : 1 , $lt : 3 } }

如果是 parents 这样的结构 :

  • 下标的某个属性的值:"parents.0.age" : 50
  • 某个属性的值 :"parents.age" : 50
  • $elemMatch :parents : { $elemMatch : { name : "father" , age : 50 } }
  • 特殊符号 :"parents.0.age" : 50 ,"parents.0.name" : father

正则表达式

MongoDB 还可以通过正则表达式查询

/* find startWith a and endWith m (查询 name 以字母 a 开头,以字母 m 结尾的) */
> db.user.find(
>   { 
>     name : /^a/ ,
>     name : /m$/
>   }
> )

注:我个人觉得这个版本的有问题,如下图:
这里写图片描述
图中的 name = joe 的结果应该得不到,但是却被查出来了,不知道是我的正则不符合 MongoDB 的规范还是 BUG。在官方教程中也没找到该部分的规则(全英文你懂的),所以我不建议使用正则来查询。


where

看到 where 大家应该都比较熟悉,在 MongoDB 中的语法是 $where ,需要注意的是这个where是一个函数 function 。

> db.user.find(
>   { $where : function() 
>     {
>       return this.name = "admin"
>     } 
>   }
> )

代码的意思是:查询 name = “admin” 的所有结果


$exists

这个是补充的,有必要讲讲。

db.user.find(
  { name : { $exist : 1 } }
)

db.user.find(
  { name : { $exist : true } }
)

上面两种用法都可以使用。一听名字就应该知道{ name : { $exist : 1 } } 的意思是查询存在属性为 name 的所有 Document,那么当 $exist : 0 或 false 时就是查询不存在该字段的所有 Document 了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
为什么会这样[user_mongo@nosql01 replicaset]$ cd /opt [user_mongo@nosql01 opt]$ ll total 0 drwxr-xr-x. 3 root root 25 Mar 16 17:08 servers drwxr-xr-x. 2 root root 51 Mar 16 17:10 software [user_mongo@nosql01 opt]$ tar -zxvf /opt/software/mongodb-linux-x86_64-rhel70-4.4.12.tgz -C /opt/servers/mongodb_demo/replicaset/ mongodb-linux-x86_64-rhel70-4.4.12/LICENSE-Community.txt tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/LICENSE-Community.txt: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/MPL-2 tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/MPL-2: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/README tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/README: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/THIRD-PARTY-NOTICES tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/THIRD-PARTY-NOTICES: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/bin/install_compass tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/bin/install_compass: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/bin/mongo tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/bin/mongo: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/bin/mongod tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/bin/mongod: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/bin/mongos tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/bin/mongos: Cannot open: No such file or directory tar: Exiting with failure status due to previous errors [user_mongo@nosql01 opt]$ tar -zcvf /opt/software/mongodb-linux-x86_64-rhel70-4.4.12.tgz -C /opt/servers/mongodb_demo/replicaset/ tar: Cowardly refusing to create an empty archive Try `tar --help' or `tar --usage' for more information.
06-01

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值