2017-10-09 134 views
1

MongoDB的對象結構查詢的MongoDB空和非空值

{ 
    "_id" : ObjectId("59db626f6944c019616eb9cf"), 
    "category" : "Flat", 
    "purpose" : "SALE", 
    "price" : 2000, 
    "area" : 2, 
    "lotArea" : 2, 
    "description" : "nothing", 
    "features" : { 
     "beds" : 3, 
     "baths" : 3, 
     "totalRooms" : 2, 
     "dining" : true, 
     "furnishing" : true, 
     "flooring" : true, 
     "servantQuarter" : true, 
     "waterHeating" : true, 
     "ceiling" : true, 
     "cooling" : "Central", 
     "heating" : "Central", 
     "installedAppliances" : [ 
      "nothing" 
     ] 
    }, 
    "dealerId" : [ ], 
    "images" : [ ], 
    "loc" : { 
     "latitude" : "33.652324769150894", 
     "longitude" : "72.95517927486878" 
    }, 
    "address" : { 
     "city" : "Islamabad", 
     "house_no" : "1", 
     "street_no" : "1", 
     "sector" : "G-13", 
     "area_description" : "commercial" 
    }, 
    "__v" : 0 
} 
{ 
    "_id" : ObjectId("59db62a06944c019616eb9d0"), 
    "category" : "Flat", 
    "purpose" : "SALE", 
    "price" : 2001, 
    "area" : 1, 
    "lotArea" : 1, 
    "description" : "nothing", 
    "features" : { 
     "beds" : 2, 
     "baths" : 2, 
     "totalRooms" : 2, 
     "flooring" : true, 
     "furnishing" : true, 
     "ceiling" : true, 
     "waterHeating" : true, 
     "servantQuarter" : true, 
     "dining" : true, 
     "cooling" : "AC", 
     "heating" : "Central", 
     "installedAppliances" : [ 
      "nothing" 
     ] 
    }, 
    "dealerId" : [ ], 
    "images" : [ ], 
    "loc" : { 
     "latitude" : "33.65243977011859", 
     "longitude" : "72.9547689790985" 
    }, 
    "address" : { 
     "city" : "Islamabad", 
     "house_no" : "11", 
     "street_no" : "1", 
     "sector" : "G-13", 
     "area_description" : "dd" 
    }, 
    "__v" : 0 
} 
{ 
    "_id" : ObjectId("59db7ae1bfbdd82adf9c5ddc"), 
    "category" : "Flat", 
    "purpose" : "SALE", 
    "price" : 20000, 
    "area" : 2, 
    "lotArea" : 2, 
    "description" : "nothing", 
    "features" : { 
     "beds" : 2, 
     "baths" : 2, 
     "totalRooms" : 1, 
     "ceiling" : true, 
     "furnishing" : true, 
     "flooring" : true, 
     "waterHeating" : true, 
     "dining" : true, 
     "servantQuarter" : true, 
     "cooling" : "AC", 
     "heating" : "Heaters", 
     "installedAppliances" : [ 
      "nothing" 
     ] 
    }, 
    "dealerId" : [ ], 
    "images" : [ ], 
    "loc" : { 
     "latitude" : "33.664966530995855", 
     "longitude" : "72.99625174581297" 
    }, 
    "address" : { 
     "city" : "Islamabad", 
     "house_no" : "1", 
     "street_no" : "1", 
     "sector" : "G-11", 
     "area_description" : "commercial" 
    }, 
    "__v" : 0 

我想查詢基於空非空字符串,例如一些數據

db.properties.find({"purpose":"SALE,"address.city":""}).pretty() 

,如果用戶輸入「的宗旨「值,但不輸入」address.city「的值,那麼只有這些數據應該返回,這是出於銷售目的,並且如果用戶輸入的」address.city「有值,例如

db.properties.find({"purpose":"SALE,"address.city":"Islamabad"}).pretty() 

現在數據必須是特定城市的特定銷售。

其實我需要這種查詢類型的查詢處理值,如果在$和邏輯條件中爲空或不爲空,並且我有一個很長的可能性列表,應該從用戶高級搜索選擇中的mongodb查詢數據。

+0

這看起來非常類似於https://stackoverflow.com/questions/42315649 –

回答

0

您可能需要一個城市列表$後發現,其中包括像這樣的空值:

db.properties.find({"purpose":"SALE,"address.city":{$in : [null, "City1", "City2"]}}); 

這將返回resutls兩個你想要的城市和空值。

+0

讓我們假設我們使用一個變量來獲得價值,但是如果用戶根本沒有輸入一個值。畢竟它是爲了提前搜索目的,他可能會也可能不會輸入值,但如果他輸入值,那麼我們必須查詢用戶輸入城市名稱的目的銷售數據。 db.properties.find({ 「目的」: 「SALE」 address.city「:req.body.city).pretty() – Sohail