简体   繁体   中英

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

I think you are looking for set

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM