skip to Main Content

I am trying to use query search in mongoose when user hast array of objects with different postalCodes.
And I have an object which is called Job.
At job I have postalCode as String.
What I want to achieve it is for each postalCode from users to slice the postalCode on jobs and if possible to match them.
Problem it is that always it is returning empty but in case it should match with the job and return it.
User it is always the logged in user.
So user is the given input to search into the collection of jobs

Here is my Code of 1 user.

"searchFilter" : {
    "remote" : 0,
    "data" : [
        {
            
            "country" : "DEU",
            "searchActive" : false,
            "postalCode" : "123",
            "available" : {
                "$date" : 1664955924380
            }
        },
        {
            
            "country" : "DEU",
            "searchActive" : false,
            "postalCode" : "850",
            "available" : {
                "$date" : 1667165151744
            }
        }
    ]
},

And this is the job.

"postalCode" : "12345",
"country" : "DEU",
    
    

And this is my actual query search.

 let job = await Job.find({
  
                $or: user.searchFilter.data.map((user) => user.postalCode)
                .map((postalCode) => ( {
                      yearSubstring: { $substr: [ "$postalCode", 0, postalCode.length ] },                
              }))

            
});

if (!job) {
   res.status(204).json({ error: "No Data" });
   return;
 }
return res.status(200).send(job);

2

Answers


  1. If I understand correctly, what you want is close to this:

    1. First find the logged user
    2. unwind to divide its data to different documents
    3. Get the relevant jobs for each data item
    4. Keep only data items that has a job.
    5. An optional step is to $group all the relevant data items of the user to one document (can be seen on the playground link below)
    db.users.aggregate([
      {$match: {}}, // here use the logged user identifier
      {$unwind: "$searchFilter.data"},
      {$lookup: {
          from: "jobs",
          let: {postalCode: "$searchFilter.data.postalCode"},
          pipeline: [
            {
              $match: {
                $expr: {
                  $eq: [
                    {$substr: ["$postalCode", 0, {$strLenCP: "$$postalCode"}]},
                    "$$postalCode"
                  ]
                }
              }
            }
          ],
          as: "jobs"
        }
      },
      {$match: {"jobs.0": {$exists: true}}}
    ])
    

    See how it works on the playground example

    Login or Signup to reply.
  2. You can simply use $in operator to find the string from a provided array of strings,

    let job = await Job.find({
      postalCode: { 
        $in: user.searchFilter.data.map(user => user.postalCode) 
      }
    });
    

    Playground

    If you want to search the substring or inside the string then you can use $regex operator,

    let job = await Job.find({
      postalCode: { 
        $regex: user.searchFilter.data.map(user => user.postalCode).join("|")
      }
    });
    

    Playground

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search