# Upsert with conditional on Boolean field

**URL:** <https://discuss.dgraph.io/t/upsert-with-conditional-on-boolean-field/13313>\
**Category:** Ratel\
**Tags:** kind:question, area:upsert, dql\
**Created:** [March 20, 2021, 9:38pm UTC](https://discuss.dgraph.io/t/upsert-with-conditional-on-boolean-field/13313 "2021-03-20T21:38:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![JSPromisel](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/jspromisel/32/7305_2.png) [@JSPromisel](https://discuss.dgraph.io/u/JSPromisel)\
**Post date:** [March 20, 2021, 9:38pm UTC](https://discuss.dgraph.io/t/upsert-with-conditional-on-boolean-field/13313/1 "2021-03-20T21:38:21Z")

</div>

I’m trying to write a conditional upsert with DQL that will only cast a vote on a poll if the poll voting is still open and the user does not already have an existing vote, something like:

```auto
upsert {
  query {
    poll(func: uid(pollUID)) {
      pollID as uid
      votingOpen as Poll.votingOpen
    }
    user(func: uid(userUID)) {
      User.pollResponses {
        PollResponse.poll @filter(uid(pollUID)) {
          existingVotes as uid
        }
      }
    }
}
  mutation @if(eq(len(existingVotes), 0) AND eq(val(votingOpen), true)) {
    set {
      # proceed to create new poll vote/response
    }
  }
}

```

The check for the length of existing votes works as expected, but the check of whether the poll is open does not. Am I missing something here? My best guess is that votingOpen variable is really an array of booleans and therefore never equates to true, though I can’t think how I could recreate that logic if that is the case.

Any suggestions would be very welcome, I feel like this should be a pretty reasonable thing to want to be able to do.

---

<div class="post-metadata">

**Author:** ![MichelDiz](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/micheldiz/32/11873_2.png) [@MichelDiz](https://discuss.dgraph.io/u/MichelDiz)\
**Post date:** [March 21, 2021, 5:21pm UTC](https://discuss.dgraph.io/t/upsert-with-conditional-on-boolean-field/13313/2 "2021-03-21T17:21:28Z")

</div>

All logic besides len() should be done in the Query blocks, not in the mutation one.  
e.g.

```auto
votingOpen(func: uid(votingOpen)) @filter(eq(val(Poll.votingOpen), true){
      VO as uid
    }

mutation @if(eq(len(existingVotes), 0) AND eq(len(VO), 1))

```

---

<div class="post-metadata">

**Author:** ![JSPromisel](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/jspromisel/32/7305_2.png) [@JSPromisel](https://discuss.dgraph.io/u/JSPromisel)\
**Post date:** [March 21, 2021, 10:18pm UTC](https://discuss.dgraph.io/t/upsert-with-conditional-on-boolean-field/13313/3 "2021-03-21T22:18:06Z")

</div>

Thanks for that info that is helpful for me better understanding how to structure these types of queries.
