天天看點

【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  or 

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

           

===========

事實上我們還可以用多個條件進行查詢

1: $lt, $lte, $gt, $gte, (<, <=, >, >=)

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

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的文檔。