使用空值和非空值查询 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()
如果用户输入"目的"值但没有输入"地址城市"

值,则只有这些数据应返回销售目的,并且如果用户输入了"地址.城市"的值,例如

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

现在,数据必须是该特定城市的目的销售。

实际上,我需要这种类型的高级搜索查询,该查询在逻辑条件下处理空或不空的值$and并且我有一长串可能性,应该在用户高级搜索选择上从MongoDB查询数据。

您可能需要

在$in之后查找包含空值的城市列表,如下所示:

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

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

最新更新