2017-10-09 61 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