【mongoDB-查询】---$exists

1: 如果某个元素存在,则查询用$exists可以查询到

$exists
Syntax: { "field": { $exists: <boolean> } }

When <boolean> is true, $exists matches the documents that contain the field, including documents where the field value is null. If <boolean> is false, the query returns only the documents that do not contain the field.

Example:

db.inventory.find( { qty: { $exists: true, $nin: [ 5, 15 ] } } )

This query will select all documents in the inventory collection where the qty field exists and its value does not equal 5 or 15.

> db.pci_issue.findOne({"buildid":{$exists:true}})
{
    "_id" : ObjectId("5783216415e9e26cad711d31"),
    "bl" : "lte-n",
    "product" : "tdd-fzm",
    "branch" : "trunk",
    "build_opentime" : 1467965225,
    "status" : "CLOSE",
    "followups" : "[7-8] pre-check to Tomasz",
    "opentime" : 1467978420,
    "description" : "[QT1_Blocker][FZM TRUNK]:RFSW misalignment",
    "buildid" : "TLF00_ENB_9999_160708_019275",
    "severity" : "A-Critical",
    "component" : "SCM",
    "closetime" : 1468200060,
    "submitter" : "JiangYukun",
    "test_hierarchy" : [
        "QT;QT1"
    ],
    "catagory" : "Blocker",
    "rootcause" : "Compiling Error",
    "bugid" : "",
    "submitter_team" : "QT",
    "transfer_times" : 0,
    "buildid_close" : "TLF00_ENB_9999_160708_019278",
    "issueid" : 1920
}

===========
事实上我们还可以用多个条件进行查询
1: $lt, $lte, $gt, $gte, (<, <=, >, >=)

> db.pci_issue.find({"issueid":{"$gte":1920, "$lt": 1921}}).pretty()
{
    "_id" : ObjectId("5783216415e9e26cad711d31"),
    "bl" : "lte-n",
    "product" : "tdd-fzm",
    "branch" : "trunk",
    "build_opentime" : 1467965225,
    "status" : "CLOSE",
    "followups" : "[7-8] pre-check to Tomasz",
    "opentime" : 1467978420,
    "description" : "[QT1_Blocker][FZM TRUNK]:RFSW misalignment",
    "buildid" : "TLF00_ENB_9999_160708_019275",
    "severity" : "A-Critical",
    "component" : "SCM",
    "closetime" : 1468200060,
    "submitter" : "JiangYukun",
    "test_hierarchy" : [
        "QT;QT1"
    ],
    "catagory" : "Blocker",
    "rootcause" : "Compiling Error",
    "bugid" : "",
    "submitter_team" : "QT",
    "transfer_times" : 0,
    "buildid_close" : "TLF00_ENB_9999_160708_019278",
    "issueid" : 1920
}

2: $in, $nin, $or, $not
db.users.find({“age”: {“$in”: [30, 35, 40]}})
是查询users的age是30,35,40的成员信息。

db.raffle.find({“$or” : [{“ticket_no”: 545}, {“winner”: true}] })
则是匹配ticket_no是545的,或者winner键值是true的文档。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值