prisma where not in array

Problem: Prisma where not in array

If you are a developer, you might have come across a situation where you need to filter data based on certain conditions. One of the popular ORM solutions for Node.js and TypeScript is Prisma. However, what if you need to filter data based on a condition that is not present in the array?

Solution 1: Use Prisma's 'not' operator

Prisma provides a powerful 'not' operator to filter data based on negation. You can use this operator to filter out data that doesn't match your condition. Here's an example:


const filteredData = await prisma.user.findMany({
  where: {
    NOT: {
      name: "John"
    }
  }
});

In this example, we are filtering out all the users whose name is not 'John'. This is a simple example, but you can use this operator with other conditions as well.

Solution 2: Use Prisma's 'OR' operator

If you have multiple conditions and one of them is not present in the array, you can use Prisma's 'OR' operator. Here's an example:


const filteredData = await prisma.user.findMany({
  where: {
    OR: [
      { name: "John" },
      { age: { gt: 18 } }
    ]
  }
});

In this example, we are filtering out all the users whose name is 'John' or age is greater than 18. This way, if the name 'John' is not present in the array, we can still filter data based on another condition.

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe