Sukhchain Singh
Sukhchain Singh

Reputation: 11

How can I update an already stored boolean value in prisma with mongoDB?

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

Upvotes: 1

Views: 3005

Answers (2)

Ian Sebastian
Ian Sebastian

Reputation: 447

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

Upvotes: 0

Fastnlight
Fastnlight

Reputation: 1168

I think you are looking for set

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

Upvotes: 1

Related Questions