Java MongoDB MongoCollection:无法更新字段
我正在尝试将mongodb集合中的单个文档更新为Java MongoDB MongoCollection:无法更新字段,java,mongodb,Java,Mongodb,我正在尝试将mongodb集合中的单个文档更新为 JSONArray jsonArr = new JSONArray(); /*Some processing to add stuff to jsonArr*/ mongoCollection.updateOne(eq("key", _id),Updates.set("asd", jsonArr)); 但我明白了 org.bson.codecs.configuration.CodecConfigurationException: Can't
JSONArray jsonArr = new JSONArray();
/*Some processing to add stuff to jsonArr*/
mongoCollection.updateOne(eq("key", _id),Updates.set("asd", jsonArr));
但我明白了
org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class org.json.simple.JSONObject.
at org.bson.codecs.configuration.CodecCache.getOrThrow(CodecCache.java:46)
at org.bson.codecs.configuration.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:63)
at org.bson.codecs.configuration.ChildCodecRegistry.get(ChildCodecRegistry.java:51)
at org.bson.codecs.IterableCodec.writeValue(IterableCodec.java:105)
at org.bson.codecs.IterableCodec.encode(IterableCodec.java:90)
at org.bson.codecs.IterableCodec.encode(IterableCodec.java:37)
at com.mongodb.client.model.BuildersHelper.encodeValue(BuildersHelper.java:35)
at com.mongodb.client.model.Updates$SimpleUpdate.toBsonDocument(Updates.java:442)
at com.mongodb.MongoCollectionImpl.toBsonDocument(MongoCollectionImpl.java:599)
at com.mongodb.MongoCollectionImpl.update(MongoCollectionImpl.java:542)
at com.mongodb.MongoCollectionImpl.updateOne(MongoCollectionImpl.java:381)
at com.mongodb.MongoCollectionImpl.updateOne(MongoCollectionImpl.java:376)
注意,这里是我最初创建文档的方式
Document unprocessedMeta = new Document("key",_id);
JSONArray arr = new JSONArray();
/*some processing to add stuff to arr */
unprocessedMeta.append("asd", arr);
mongoCollection.insertOne(unprocessedMeta);
这一切都很好。
请注意,asd
键的值为JSONArray
。此外,我还可以通过以下方式选择文档:
documentd=mongoCollection.find(eq(“key”),_id)).first()代码>
结果正如预期的那样
Document{{_id=5981e702324fb0b50d727fe7, key=2323, asd=[Document{{<some-key-value-pairs>}}]}}
我可以遍历现有ASD
并访问对象
为什么我不能更新?基本上,我想替换文档中匹配key
的keyasd
的值。我甚至试过
mongoCollection.updateOne(eq("key", _id),Updates.unset("asd"));
mongoCollection.updateOne(eq("key", _id),Updates.set("asd", jsonArr));
但是我得到了相同的错误,因为MongoDB使用BSON进行操作,我相信这是因为BSON序列化程序没有JSONObject
尝试使用列表
:
// id of the document
Document id = new Document("key", 1);
// array of documents
List<Document> docArray = Arrays.asList(
new Document("key1", "val1"),
new Document("key2", "val2"));
// insert the new document
Document doc = new Document("key", 1).append("docarray", docArray);
collection.insertOne(doc);
System.out.println("Before: " + collection.find(id).first().toJson());
// new array of documents
List<Document> docArrayUpdated = Arrays.asList(
new Document("key3", "val3"),
new Document("key4", "val4"));
// update the document with the new array
collection.updateOne(id, Updates.set("docarray", docArrayUpdated));
System.out.println("After: " + collection.find(id).first().toJson());
这表明docarray
字段已成功更新。由于MongoDB使用BSON进行操作,我相信这是因为BSON序列化程序没有JSONObject
尝试使用列表
:
// id of the document
Document id = new Document("key", 1);
// array of documents
List<Document> docArray = Arrays.asList(
new Document("key1", "val1"),
new Document("key2", "val2"));
// insert the new document
Document doc = new Document("key", 1).append("docarray", docArray);
collection.insertOne(doc);
System.out.println("Before: " + collection.find(id).first().toJson());
// new array of documents
List<Document> docArrayUpdated = Arrays.asList(
new Document("key3", "val3"),
new Document("key4", "val4"));
// update the document with the new array
collection.updateOne(id, Updates.set("docarray", docArrayUpdated));
System.out.println("After: " + collection.find(id).first().toJson());
这表明docarray
字段已成功更新。使用mongoCollection.updateOne(eq(“key”),_id),新文档($set),新文档(“asd”,jsonArr))代码>使用mongoCollection.updateOne(eq(“key”),_id),新文档($set),新文档(“asd”,jsonArr))代码>谢谢,但现在我得到了无法更新{“ert”:“dvwmv”,“zxc”:“inin”,“qaz”:“mnmnbm”,“tt”:24889}com.mongodb.MongoWriteException:“imageList.0.tt.$numberLong”中的美元($)前缀字段“$numberLong”对存储无效。
它与数字字段/值有关吗?您有$push
(或其他表达式)在$set
中?您能显示您想写入MongoDB的数据结构吗?谢谢,但现在我得到无法更新{“ert”:“dvwmv”,“zxc”:“inin”,“qaz”:“mnmnbm”,“tt”:24889}com.MongoDB.MongoWriteException:美元($)“imageList.0.tt.$numberLong”中的前缀字段“$numberLong”对存储无效。
它是否与数字字段/值有关?在$set
中是否有$push
(或其他表达式)?能否显示要写入MongoDB的数据结构?
Before: { "_id" : { "$oid" : "5982cf6f11e67733e5efcea6" }, "key" : 1, "docarray" : [{ "key1" : "val1" }, { "key2" : "val2" }] }
After: { "_id" : { "$oid" : "5982cf6f11e67733e5efcea6" }, "key" : 1, "docarray" : [{ "key3" : "val3" }, { "key4" : "val4" }] }