使用空值和非空值查询Mongodb

时间:2017-10-09 19:36:16

标签: mongodb mongodb-query

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的数据。

1 个答案:

答案 0 :(得分:0)

您可能需要在$ in之后找到包含空值的城市列表,如下所示:

1)    Load data into staging
2)    Clean/Convert by copying to a second staging table defined using the desired datatypes. Good data copied over, bad data left behind
3)    Copy data from the "clean" table to the "live" table

这将返回您想要的城市和空值的结果。