我正在使用updateOne()更新mongo集合文档。我正在传递过滤器,updatepart(均为BSON类型),将updateoptions作为参数传递给updateOne()。并且我以编程方式创建的集合具有分片键(orderId)。但是我的集合的主键是一个复合键(orderId和lineNumber)。我能够插入我的集合中,但是在更新文档时(在主json内插入具有自己的键的subobj)我正在获取java.lang .IllegalArgumentException:无效的BSON字段名称_id.orderId。所以我怀疑这可能是由于分片键配置所致。实际上,它不使用我在updateOne方法中传递的过滤器(过滤器具有复合键)并抛出无效的字段名称错误。谁能对此有所启发。我应该能够用subobj更新文档。
我正在使用monogDb驱动程序3.2.2
注意:我已经尝试过用replaceOne方法代替updateOne,也不能使用replaceOne,因为在我的情况下,我的更新实际上是仅在主json内插入subobj(array)。
碎片收集创建代码 ``
BsonDocument cmd = new BsonDocument();
cmd.append("customAction", new BsonString("createCollection"))
.append("collection", new BsonString("collectionName"))
.append("shardKey", new BsonString("_id.orderId"))
.append("offerThroughput", new BsonInt32(Integer.parseInt("50000")))
; ``
upsert part :(当我连接subobj键时,只需忽略updatePart的逻辑) ``
Bson filter = Filters.eq("_id.orderId", orderId);
Bson filter1 = Filters.eq("_id.lineNumber", lineNumber);
Map<String, String> keyval = mapper.readValue(data, Map.class);
Entry<String, String> itr = keyval.entrySet().iterator().next();
String keyname = itr.getKey();
org.bson.Document newdoc = org.bson.Document.parse(mapper.writeValueAsString(itr.getValue()));
Bson finalfilter = Filters.and(filter,filter1);
Bson updatePart = combine(set(nodeName.concat(".").concat(keyname), newdoc));
UpdateResult result = dbccollection.updateOne( updatePart,finalfilter, new UpdateOptions().upsert(true));
``