mongodb查询之查询条件

2,953次阅读
没有评论

共计 1458 个字符,预计需要花费 4 分钟才能阅读完成。

mongodb查询之查询条件

1、gtgte ltlte

如果直接记忆这些不太好记,但是你要是知道其中的意思就好记了,gte 就是greater than equal 也就是大于等于的意思,这样好记多了

db.test.find({ "friends" : {"$gte":32} })


{ 
    "_id" : ObjectId("5afd34052c63ae1a943bdb27"), 
    "name" : "hello", 
    "friends" : 34.0, 
    "enemies" : 2.0, 
    "favor" : [
        "2", 
        "3", 
        "4", 
        "6", 
        "7", 
        "8", 
        "9", 
        "10", 
        "1"
    ], 
    "mul" : 1.0
}
{ 
    "_id" : ObjectId("5afd347c2c63ae1a943bdb29"), 
    "name" : "joe", 
    "friends" : 34.0, 
    "enemies" : 4.0, 
    "mul" : 1.0
}

2、 orin

in 是用来判定一个键的值在给出的数据当中,or则是用来关联多个键-只要满足其中一个条件就可以

db.test.find({ "friends" : {"$in":[34,3]} })
{ 
    "_id" : ObjectId("5afd34052c63ae1a943bdb27"), 
    "name" : "hello", 
    "friends" : 34.0, 
    "enemies" : 2.0, 
    "favor" : [
        "2", 
        "3", 
        "4", 
        "6", 
        "7", 
        "8", 
        "9", 
        "10", 
        "1"
    ], 
    "mul" : 1.0
}
{ 
    "_id" : ObjectId("5afd347c2c63ae1a943bdb29"), 
    "name" : "joe", 
    "friends" : 34.0, 
    "enemies" : 4.0, 
    "mul" : 1.0
}
{ 
    "_id" : ObjectId("5b042463c42e07823911338e"), 
    "name" : 25.0, 
    "friends" : 3.0
}

下面演示一下or的使用

db.test.find({"$or": [{"enemies" : 4.0},{"enemies" : 2.0}]} )
{ 
    "_id" : ObjectId("5afd34052c63ae1a943bdb27"), 
    "name" : "hello", 
    "friends" : 34.0, 
    "enemies" : 2.0, 
    "favor" : [
        "2", 
        "3", 
        "4", 
        "6", 
        "7", 
        "8", 
        "9", 
        "10", 
        "1"
    ], 
    "mul" : 1.0
}
{ 
    "_id" : ObjectId("5afd347c2c63ae1a943bdb29"), 
    "name" : "joe", 
    "friends" : 34.0, 
    "enemies" : 4.0, 
    "mul" : 1.0
}

3、$not

not条件是元条件查询,可以放在其他查询条件的外面,凌驾于其他查询条件之上

db.test.find({"enemies":{"not":{"mod":[3,1]}} } )
{ 
    "_id" : ObjectId("5afd34052c63ae1a943bdb27"), 
    "name" : "hello", 
    "friends" : 34.0, 
    "enemies" : 2.0, 
    "favor" : [
        "2", 
        "3", 
        "4", 
        "6", 
        "7", 
        "8", 
        "9", 
        "10", 
        "1"
    ], 
    "mul" : 1.0
}
{ 
    "_id" : ObjectId("5b042463c42e07823911338e"), 
    "name" : 25.0, 
    "friends" : 3.0
}

4、条件语义

经常我们会遇到的 情况是查询出符合条件的文档,并对其中的文档进行修改。那么就是修改器和条件共存,条件语句是针对内层文档的键,修改器是外层文档的键

正文完
请博主喝杯咖啡吧!
post-qrcode
 
admin
版权声明:本站原创文章,由 admin 2018-05-25发表,共计1458字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)
验证码