Search code examples
mongodbmongoosegraphqlapollo

MongoDB - find() with 5 filters and conditions gives different results than no conditions


I'm wrote and tested a query to retrieve the documents and it works just fine it gets exactly what I asked for and when I take out each of the filters it retrieves the corrent documents. Now instead of manually commenting each filter I want to base it on the index as it goes down, so at index = 4 it should use all filters and at index = 3 it should use one less filter until at index = 0 where it should just get the documents.

The code that works just fine:

    users = await User.find({
      $and: ignoreUsers.map((id) => {
        return { _id: { $ne: new ObjectId(id) } };
      }),
      objectives: { $in: objectives },
      ranking: ranking,
      expLevel: { $gte: expLevel - 1, $lte: expLevel + 1 },
      industries: { $in: industries },
      "skills.self": { $in: skills },
      interests: { $in: interests },
    });

This produces 1 document which is correct, however when I add the if conditions it produces 5 documents which is not corrent. The ranking.<variable> are numbers 1-4 so if the index = 4 then all the if conditions should be true right? but none of them are being executed. only the objectives and ranking filters work, the whole $expr conditions are being ignored

        let index = 4;

        // supposed to be in while loop but testing just once does not produce just the 1 document it's supposed to

        const fetchCondUsers = await User.find({
          $and: ignoreUsers.map((id) => {
            return { _id: { $ne: new ObjectId(id) } };
          }),
          objectives: { $in: objectives },
          
          $expr: {
            $cond: {
              if: { $gte: [index, 0] },
              then: {
                ranking: ranking,
              },
              else: "$$REMOVE",
            },
            $cond: {
              if: { $gte: [index, ranking.expLevel] },
              then: {
                $and: [
                  { $gte: ["$expLevel", expLevel - 1] },
                  { $lte: ["$expLevel", expLevel + 1] },
                ],
              },
              else: "$$REMOVE",
            },
            $cond: {
              if: { $gte: [index, ranking.industry] },
              then: {
                industries: { $in: ["$industries", industries] },
              },
              else: "$$REMOVE",
            },
            $cond: {
              if: { $gte: [index, ranking.skills] },
              then: {
                skills: { $in: ["$skills", skills] },
              },
              else: "$$REMOVE",
            },
            $cond: {
              if: { $gte: [index, ranking.interests] },
              then: {
                interests: { $in: ["$interests", interests] },
              },
              else: "$$REMOVE",
            },
          },
        }).limit(limit);

Ideally this is what I'm trying to do

      users = await User.find({
        $and: ignoreUsers.map((id) => {
          return { _id: { $ne: new ObjectId(id) } };
        }),
        objectives: { $in: objectives },
        if (index === 0) ranking: ranking,
        if (index === 1) expLevel: { $gte: expLevel - 1, $lte: expLevel + 1 },
        if (index === 2) industries: { $in: industries },
        if (index === 3) "skills.self": { $in: skills },
        if (index === 4) interests: { $in: interests },
      });

Solution

  • So after staring in to the abyss I realized javascript........ AN OBJECT IS BEING PASSED IN TO THE find()... So I simply just took out the object in to its own variable and broke it down like so:

          while (users.length < limit) {
            const query = {
              $and: ignoreUserObjectIDs.map((oID) => {
                return { _id: { $ne: oID } };
              }),
              objectives: { $in: objectives },
            };
    
            if (index >= 0) {
              query["ranking"] = ranking;
            }
            if (index >= ranking.expLevel) {
              query["expLevel"] = { $gte: expLevel - 1, $lte: expLevel + 1 };
            }
            if (index >= ranking.industry) {
              query["industries"] = { $in: industries };
            }
            if (index >= ranking.skills) {
              query["skills.self"] = { $in: skills };
            }
            if (index >= ranking.interests) {
              query["interests"] = { $in: interests };
            }
    
            const fetchCondUsers = await User.find(query).limit(
              limit - users.length
            );
    
            users = users.concat(fetchCondUsers);
            ignoreUserObjectIDs = ignoreUserObjectIDs.concat(
              fetchCondUsers.map((user) => {
                return new ObjectId(user.id);
              })
            );
    
            if (index === 0) break;
            index--;
          }
    

    In my defence I'm working on 3 different projects in three different languages so I tend to forget about the simple things... Anyways, if anyone else had similar issues, here you go!