skip to Main Content

I am using prisma with mongoDb for the first time and I want to update a boolean value stored in a collection, but I am not able to find a way/query to update the value from true to false or vise versa…:(

const updateUser = await prisma.user.update({
  where: {
    userToken: token,
  },
  data: {
    isOnline: true,
  },
})

I have this ‘isOnline’ stored as false default and this is what I have tried wrt prisma official documentation, but this did not worked for me

2

Answers


  1. I think you are looking for set

    const updateUser = await prisma.user.update({
      where: {
        userToken: token,
      },
      data: {
        isOnline: {
          set: true
        },
      },
    })
    
    Login or Signup to reply.
  2. Since true and false values could be mistaken as a special instruction in "prisma logics", the response from @Fastnligth should be the correct one -did’t tried that though-.

    Since Prisma ORM implemented MongoDB as an after thought, some of these functionalities might "seem a bit off".

    I’ve arrived here trying to update an embedded field without updating the whole document, just the given field.

    Leaving my two cents in case somebody else is having the same sailing over google ⛵️

    You can do that as follows

    const order = await prisma.order.update({
      where: {
        id: 'some-object-id',
      },
      data: {
        shippingAddress: {
          // Update just the zip field
          update: {
            zip: '41232',
          },
        },
      },
    })
    

    official docs: https://www.prisma.io/docs/concepts/components/prisma-client/composite-types

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