Spring Data MongoDB:BigInteger到ObjectId的转换
问题描述:
我在使用Spring Data MongoDB进行更新查询时遇到问题。我将一些对象的_id检索为BigInteger值。然后我想进行以下查询:
I have a problem with update query using Spring Data MongoDB. I retrieve some object's _id as BigInteger value. Then I want to make following query:
Query query = new Query(Criteria.where("_id").is(id));
Update update = new Update();
update.set("version",version);
mongoOperations.updateFirst(query, update, Audit.class);
查询部分无法匹配任何文档,因为传递给的id值为()
必须以某种方式转换为ObjectId。我找不到关于这种转换的任何文档。将不胜感激任何帮助。
Query part fails to match any documents since id value passed to is()
somehow must be converted to ObjectId. I can't find any documentation on this kind of conversion. Will appreciate any help.
ps:SpringData Mongodb版本1.2
p.s.: SpringData Mongodb version 1.2
答
你可以转换它也可以手动:
You can convert it also manually:
ObjectId convertedId = new ObjectId(bigInteger.toString(16));
Query query = new Query(Criteria.where("_id").is(convertedId));