0

I am using the following code to remove all the elements from mongoDB collection with a given parent_id:

final String strQuery = "db.Child.remove({'$query':{'PARENT_ID':'" + parentId + "'}})";
                final Query query = entityManager.createNativeQuery(strQuery, Child.class);
                query.executeUpdate();

However, I am getting the following exception:

Unexpected Exception
com.mongodb.util.JSONParseException:
db.Child.remove({'$query':{'CHILD_ID':'7313c076-dbaa-4557-b80f-68d040b65d82'}})

If I replace remove with find, I get the result back. Dont know what is causing JSON parser error in the aboev mentioned native query.

I am using hibernate-ogm version 4.3 Final with mongo-db 3.2

Obaid Maroof
  • 1,523
  • 2
  • 19
  • 40
  • so the conclusion I have reached is: I can not use `$query` as its deprecated and not supported any more and `.sort()` cursor method is not supported by hibernate OGM 5.0.1. Any other alternative then me sorting it in my code? – Obaid Maroof Oct 02 '16 at 15:52
  • 1
    This is unforunate but it's due to MongoDB incompatible changes from 3.0 to 3.2. OGM 5.0 does not support very well MongoDB 3.2 due to these changes. I fixed it a while ago in the master branch: see https://github.com/hibernate/hibernate-ogm/pull/749/commits/e3d008fa01f98f568d64a9899145aa68e13b0507 . We have plan to release a first alpha of OGM 5.1 soon and it should be pretty safe to use. – Guillaume Smet Oct 03 '16 at 08:51
  • there was me thinking JPA has an em.remove method ... – Neil Stockton Oct 08 '16 at 18:03
  • I need to remove all `childs` with parent `PARENT_ID`. With em.remove, I will need to iterate over all the childs and remove items individually. – Obaid Maroof Oct 10 '16 at 17:36
  • 1
    @ObaidMaroof we released Hibernate OGM 5.0.2 which supports MongoDB 3.2 and should work just fine. HTH. – Guillaume Smet Oct 11 '16 at 10:16
  • You guys are start. Thanks @GuillaumeSmet. It worked for me without any further trouble. – Obaid Maroof Oct 12 '16 at 07:55

1 Answers1

1

Hibernate OGM 4.3 did not support the remove operation for native queries.

You should give OGM 5.0.2.Final a try: it should solve your issue as we added the support for quite a lot of other operations (and a lot of other fixes and improvements).

Guillaume Smet
  • 9,921
  • 22
  • 29
  • After bumping the hibernate OGM version, now I am getting the following: `Caused by: com.mongodb.WriteConcernException: Write failed with error code 2 and error message 'unknown top level operator: $query'` – Obaid Maroof Oct 02 '16 at 13:01
  • ok, removing the `$query` construct seems to have done the trick. Do you have any idea how to use the `$orderBy` construct for the find query? – Obaid Maroof Oct 02 '16 at 13:11
  • Its disappointing to see that the examples in documentation [here](https://docs.jboss.org/hibernate/stable/ogm/reference/en-US/html_single/#jp_ql_queries) is not really working. I am unable to use the `$query` construct as its failing with the above mentioned error. – Obaid Maroof Oct 02 '16 at 13:22
  • 1
    The examples work with MongoDB 3.0 which is the version supported by OGM 5.0. Due to incompatible changes, OGM 5.0 does not really support MongoDB 3.2. It's fixed in master and will be included in the 5.1 release - we hope to release an alpha soon. – Guillaume Smet Oct 03 '16 at 08:58