简体   繁体   中英

Multiple field pass to get the query failed

I am trying to find the list of users based on the different queries. Like if I pass firstname or lastname or location I want data based on one of them or all of them but I've failed to get the details. And also there are 2 nested fields I want to pass for the check. These are here "otherDetails.nationality": "Indian" and "professionalDetails.type": "Cook" . Whenever I pass them a single query check I get the desired data but Whenever I put all of them in the $or aggregation method it fails and gives me all the data in database. I was stuck here for 2 days and couldn't solve it. A help will be appreciated

here is a glimpse of the database data. Don't go with the same its one record I copied multiple times and I also removed some of the fields I make it clean

[{
        "_id": "604670a613a06f0017702d88",
        "firstname": "Babulal ",
        "lastname": "Gadri",
        "mobile": "00000000000",
        "otherDetails": {
            "nationality": "Indian"
        },
        "professionalDetails": {
            "type": "Cook"
        }
}

{
        "_id": "604670a613a06f0017702d8a",
        "firstname": "Babulal ",
        "lastname": "Gadri",
        "mobile": "00000000000",
        "otherDetails": {
            "nationality": "Indian"
        },
        "professionalDetails": {
            "type": "Cook"
        }
}
{
        "_id": "604670a613a06f0017702d8b",
        "firstname": "Babulal ",
        "lastname": "Gadri",
        "mobile": "00000000000",
        "otherDetails": {
            "nationality": "Indian"
        },
        "professionalDetails": {
            "type": "Cook"
        }
}
{
        "_id": "604670a613a06f0017702d88",
        "firstname": "Babulal ",
        "lastname": "Gadri",
        "mobile": "00000000000",
        "otherDetails": {
            "nationality": "Indian"
        },
        "professionalDetails": {
            "type": "Cook"
        }
}
{
        "_id": "604670a613a06f0017702d8c",
        "firstname": "Babulal ",
        "lastname": "Gadri",
        "mobile": "00000000000",
        "otherDetails": {
            "nationality": "Indian"
        },
        "professionalDetails": {
            "type": "Chef"
        }
}

]

Here is the function I am doing

searchCookForNotification: async function (req, res) {
    let { type, nationality, firstname, lastname, location } = req.query;

    var db = Person.getDatastore().manager;

    var cooks = await db
      .collection(Person.tableName)
      .find({
        $or: [
          { firstname },
          { lastname },
          { location },
          { "professionalDetails.type": type },
          { "otherDetails.nationality": nationality },
        ],
      })
      .toArray();

    console.log("Length: ", cooks.length);

    return res.successResponse(
      cooks,
      200,
      null,
      true,
      "Cooks found successfully"
    );
  },
};

This is just happening because the code itself is unstable, if one of the parameters does not exist in "query" for example "location" is missing then the condition you create inside the $or query will this following query:

{ location: undefined }

Which from the sample you gave is matches all documents. I imagine you just want to build the query dynamically based on given input:

let { type, nationality, firstname, lastname, location } = req.query;
var db = Person.getDatastore().manager;

const orCondArray = [];
if (firstname) {
    orCondArray.push({ firstname })
}
if (lastname) {
    orCondArray.push({ lastname })
}
if (location) {
    orCondArray.push({ location })
}
if (type) {
    orCondArray.push({ "professionalDetails.type": type })
}
if (nationality) {
    orCondArray.push({ "otherDetails.nationality": nationality })
}

if (orCondArray.length) {
  var cooks = await db
    .collection(Person.tableName)
    .find({$or: orCondArray})
    .toArray();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM