5

I'm using the mongo-jackson-wrapper with java and MongoDB. I find an object by querying a field of mine (not the _id field), and then I need to know the _id field value, whether the net result was an update or an insert. However, I get an exception:

com.mongodb.MongoException: No objects to return
    at net.vz.mongodb.jackson.WriteResult.getSavedId(WriteResult.java:97)

The exception comes from the wrapper, not the MongoDB driver itself.

WriteResult<EntityDocument, String> wr 
   = coll.update(DBQuery.is("corefEntityId", corefEntityId), up, true,  false);

What (if anything) is the right way to do this?

Parvin Gasimzade
  • 25,180
  • 8
  • 56
  • 83
bmargulies
  • 97,814
  • 39
  • 186
  • 310

1 Answers1

0

You need to use findAndModify and set returnNew to true

You can view the JavaDoc

This code should do the trick but I haven't tested it.

coll.findAndModify(DBQuery.is("corefEntityId", corefEntityId), null, null, false, up, true, false);

drei01
  • 308
  • 5
  • 15