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:
我正在使用带有java和MongoDB的mongo-jackson-wrapper。我通过查询我的一个字段(而不是_id字段)找到一个对象,然后我需要知道_id字段值,无论结果是更新还是插入。但是,我得到一个例外:
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.
异常来自包装器,而不是MongoDB驱动程序本身。
WriteResult<EntityDocument, String> wr
= coll.update(DBQuery.is("corefEntityId", corefEntityId), up, true, false);
What (if anything) is the right way to do this?
什么(如果有的话)是正确的方法吗?
1 个解决方案
#1
0
You need to use findAndModify and set returnNew to true
您需要使用findAndModify并将returnNew设置为true
You can view the JavaDoc
您可以查看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);
coll.findAndModify(DBQuery.is(“corefEntityId”,corefEntityId),null,null,false,up,true,false);
#1
0
You need to use findAndModify and set returnNew to true
您需要使用findAndModify并将returnNew设置为true
You can view the JavaDoc
您可以查看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);
coll.findAndModify(DBQuery.is(“corefEntityId”,corefEntityId),null,null,false,up,true,false);