MongoDB MongoCollection:无法更新字段

时间:2017-08-02 14:50:21

标签: 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 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。另外,我可以通过

选择文件

Document d = mongoCollection.find(eq("key", _id)).first();

并且它按预期显示

Document{{_id=5981e702324fb0b50d727fe7, key=2323, asd=[Document{{<some-key-value-pairs>}}]}}

我可以将内部JSONArray作为

JSONArray existingAsd = (JSONArray) d.get("asd");

我可以遍历existingAsd并访问对象。

为什么我无法更新?基本上,我想替换匹配asd的文档中的键key的值。我甚至试过

mongoCollection.updateOne(eq("key", _id),Updates.unset("asd"));
        mongoCollection.updateOne(eq("key", _id),Updates.set("asd", jsonArr));

但是我得到了同样的错误

2 个答案:

答案 0 :(得分:1)

由于MongoDB使用BSON运行,我相信它是因为BSON序列化器没有JSONObject。

尝试使用List<Document>代替:

// 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());

上面印刷的代码:

Before: { "_id" : { "$oid" : "5982cf6f11e67733e5efcea6" }, "key" : 1, "docarray" : [{ "key1" : "val1" }, { "key2" : "val2" }] }
After: { "_id" : { "$oid" : "5982cf6f11e67733e5efcea6" }, "key" : 1, "docarray" : [{ "key3" : "val3" }, { "key4" : "val4" }] }

表明docarray字段已成功更新。

答案 1 :(得分:1)

使用mongoCollection.updateOne(eq("key", _id),new Document("$set", new Document("asd", jsonArr)));