0

MongoDB Object Structure

    {
    "_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

I want to query some data based on an empty and non-empty string for example

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

if user enters "purpose" value but doesn't enter the "address.city" value then only those data should return that is of the purpose of sale and if there is a value for "address.city" entered by user e.g.

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

Now the data must be of purpose sale of that particular city.

Actually, I need that type of query for advanced search which handle the value if empty or not empty in $and logical condition and I have a long list of possibilities that to should query data from MongoDB on the user advance search selection.

1

1 Answer 1

0

You may need a list of cities to find after $in that includes the null value like this:

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

This will return resutls for both the cities you want and empty value.

Sign up to request clarification or add additional context in comments.

1 Comment

Let suppose we are using a variable to get value, but what if the user doesn't enter a value at all. After all it's for advance search purpose, he may or may not enter the value but if he enter the value then we must query for the data of purpose sell of the user enter city name. db.properties.find({"purpose":"SALE,"address.city":req.body.city).pretty()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.