# Modify generated query when using Neo4J-OGM

**URL:** <https://community.neo4j.com/t/modify-generated-query-when-using-neo4j-ogm/24332>\
**Category:** Spring Data Neo4j & Neo4j-OGM\
**Tags:** cypher\
**Created:** [August 30, 2020, 10:28pm UTC](https://community.neo4j.com/t/modify-generated-query-when-using-neo4j-ogm/24332 "2020-08-30T22:28:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rajan1106](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/rajan1106/32/3859_2.png) [@rajan1106](https://community.neo4j.com/u/rajan1106)\
**Post date:** [August 30, 2020, 10:28pm UTC](https://community.neo4j.com/t/modify-generated-query-when-using-neo4j-ogm/24332/1 "2020-08-30T22:28:10Z")

</div>

I want to implement a soft delete - deleted\_at flag is null by default and set to the date when node is deleted.  
I would like to add check for deleted\_at is not null in all queries.  
Is there any way to add such checks or modify the query before it is actually executed?

---

<div class="post-metadata">

**Author:** ![tony.chiboucas](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/tony.chiboucas/32/5980_2.png) [@tony.chiboucas](https://community.neo4j.com/u/tony.chiboucas)\
**Post date:** [September 8, 2020, 8:55pm UTC](https://community.neo4j.com/t/modify-generated-query-when-using-neo4j-ogm/24332/2 "2020-09-08T20:55:51Z")

</div>

Negation in a graph database is tricky and expensive. Every query would have to load ALL relevant nodes to check each for a null value, before excluding those you wish to ignore.

All of the following are functionally equivalent to deleting a property:

- Setting a property to `null`: `a.date = null`
- Remove/delete a property: `REMOVE a.date`

All of the following are functionally equivalent to checking that a property is not set:

- Checking that a property is equal to `null`: `WHERE a.date = null`
- Checking that a property does not [exist](https://neo4j.com/docs/cypher-manual/current/functions/predicate/#functions-exists): `WHERE NOT EXISTS(a.date)`
- Checking that a property `is null`: `WHERE a.date IS NULL`

That shows that what you're asking for _is possible_, but not a good thing to _plan_ to build. If your data had a million nodes, with all but one set with a `deleted_at` date, every query would have to load all one million nodes, check that the date was set to remove the node from the running list, to return the single node you cared about.

### What you probably _should_ do

Add two properties:

- `is_active` bool
- `deleted_at` dateTime

Always set the `is_active` when you create your nodes. Set it to `false`, and the `deleted_at` date when you delete. Then the million-node example isn't a problem.

---

<div class="post-metadata">

**Author:** ![rajan1106](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/rajan1106/32/3859_2.png) [@rajan1106](https://community.neo4j.com/u/rajan1106)\
**Post date:** [September 8, 2020, 9:17pm UTC](https://community.neo4j.com/t/modify-generated-query-when-using-neo4j-ogm/24332/3 "2020-09-08T21:17:41Z")

</div>

> [@rajan1106](#):
>
> want to implement a soft delete - deleted\_at flag is null by default and set to the date when node is deleted.  
> I would like to add check for deleted\_at is not null in all queries.  
> Is there any way to add such checks or modify the query before it is actually executed?

Thank you for taking the time to provide your thoughts.  
Looks like I should be able to use the Filter parameter in loadAll query.

---

<div class="post-metadata">

**Author:** ![tony.chiboucas](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/tony.chiboucas/32/5980_2.png) [@tony.chiboucas](https://community.neo4j.com/u/tony.chiboucas)\
**Post date:** [September 8, 2020, 10:40pm UTC](https://community.neo4j.com/t/modify-generated-query-when-using-neo4j-ogm/24332/4 "2020-09-08T22:40:29Z")

</div>

> [@rajan1106](#):
>
> Looks like I should be able to use the Filter parameter in loadAll query.

Be able to? **yes** Should? **no**
