征服 Mongodb 之 Modifier增强

通过find/findOne做了简单查询,恍惚间发觉好像少点什么,似乎过于简单。这里做个补充,简单说说各种$的关键字:$lt、$gt、$lte、$gte等等。

发现自己又给自己挖了个坑,又深又广,需要早点跳出来喘口气。保密

集群配置相关链接:

征服 Mongodb 之 安装与系统服务配置

征服 Mongodb 之 主从复制&集群复制

基本操作相关链接:

征服 Mongodb 之 常用命令、基本数据类型  

征服 Mongodb 之 Modifier初识

征服 Mongodb 之 Modifier增强

征服 Mongodb 之 CRUD

    我感觉似乎被这本书(《MongoDB权威指南》) 绕进去了,如果按照书上这么拆分,很容易把这些MongoDB的Modifier当作是只有Update才特有的部分。

    一、“或”

  •  $or

   有关于“且”关系的查询,详见“CRUD”那一篇,这里单说OR查询。

> db.user.findOne({"$or" :[{"age" : 30},{"uid" : "u1234567890"}]})
{
        "_class" : "org.zlex.mongodb.domain.User",
        "_id" : ObjectId("50fe4f627252799620eee0db"),
        "address" : "上海",
        "age" : 30,
        "email" : [
                "[email protected]",
                "[email protected]",
                "[email protected]",
                "[email protected]"
        ],
        "uid" : "u1234567890"
}

    {"age" : 30},{"uid" : "u1234567890"} 满足条件其一。

    

  • $not
> db.user.findOne({"age" : {"$not":{"$gt":40}}})
{
        "_class" : "org.zlex.mongodb.domain.User",
        "_id" : ObjectId("50fe4f627252799620eee0db"),
        "address" : "上海",
        "age" : 30,
        "email" : [
                "[email protected]",
                "[email protected]",
                "[email protected]",
                "[email protected]"
        ],
        "uid" : "u1234567890"
}

    age不大于40的数据检索

    二、“范围”

  • $lt、$gt、$lte、$gte

     这里指的是$lt、$gt、$lte、$gte这些稀奇古怪的标识符的特有用途。其实只要对XML格式熟悉,对这些缩写应该并不陌生。$lt、$gt、$lte、$gte与之对应的数学符号是<、>、<=、>=。

> db.user.findOne({"age" : {"$gte":20,"$lte":50}})
{
        "_class" : "org.zlex.mongodb.domain.User",
        "_id" : ObjectId("50fe4f627252799620eee0db"),
        "address" : "上海",
        "age" : 30,
        "email" : [
                "[email protected]",
                "[email protected]",
                "[email protected]",
                "[email protected]"
        ],
        "uid" : "u1234567890"
}

    等同于:  where age >= 20 and age <= 50

  • $in、$nin

    如果想查询一个键的多个值,可以用$in

> db.user.findOne({"email" : {"$in":["[email protected]","[email protected]"]}})
{
        "_class" : "org.zlex.mongodb.domain.User",
        "_id" : ObjectId("50fe4f627252799620eee0db"),
        "address" : "上海",
        "age" : 30,
        "email" : [
                "[email protected]",
                "[email protected]",
                "[email protected]",
                "[email protected]"
        ],
        "uid" : "u1234567890"
}

    与之相反的是$nin

> db.user.findOne({"email" : {"$nin":["[email protected]","[email protected]"]}})
{
        "_class" : "org.zlex.mongodb.domain.User",
        "_id" : ObjectId("50fe4f627252799620eee0db"),
        "address" : "上海",
        "age" : 30,
        "email" : [
                "[email protected]",
                "[email protected]",
                "[email protected]",
                "[email protected]"
        ],
        "uid" : "u1234567890"
}

  

   OK,本次突击任务基本完成。眨眼

   有关Modifier标识符,详见http://cn.docs.mongodb.org/manual/reference/operator/

 

集群配置相关链接:

征服 Mongodb 之 安装与系统服务配置

征服 Mongodb 之 主从复制&集群复制

基本操作相关链接:

征服 Mongodb 之 常用命令、基本数据类型  

征服 Mongodb 之 Modifier初识

征服 Mongodb 之 Modifier增强

征服 Mongodb 之 CRUD

猜你喜欢

转载自snowolf.iteye.com/blog/1797313