【问题标题】:Query Mongodb with empty and non-empty values使用空值和非空值查询 Mongodb
【发布时间】:2018-12-19 16:44:51
【问题描述】:

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()

现在数据必须是针对特定城市的销售。

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

【问题讨论】:

标签: mongodb mongodb-query


【解决方案1】:

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

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

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

【讨论】:

  • 假设我们正在使用一个变量来获取值,但是如果用户根本没有输入值怎么办。毕竟是为了提前搜索,他可能会输入值,也可能不会输入值,但是如果他输入了值,那么我们必须查询用户输入城市名称的目的销售数据。 db.properties.find({"目的":"SALE,"address.city":req.body.city).pretty()
猜你喜欢
  • 2010-11-19
  • 1970-01-01
  • 1970-01-01
  • 2013-02-11
  • 2012-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多