2017-08-02 96 views
0

我想在我的MongoDB集合更新单个文档MongoDB的MongoCollection:无法更新场

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并访问对象。

为什么不能更新?基本上,我想替换与key匹配的文档中的密钥asd的值。我甚至试过

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

,但我得到了同样的错误

回答

1

因为MongoDB的操作使用BSON,我相信这是因为没有对的JSONObject BSON序列化。

尝试使用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领域已成功更新。

+0

谢谢,工作! – AbtPst

1

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

+0

谢谢,但现在我得到 '无法更新{“ert”:“dvwmv”,“zxc”:“inin”,“qaz”:“mnmnbm”,“tt”:24889} com.mongodb.MongoWriteException: 'imageList.0.tt。$ numberLong'中的美元($)前缀字段'$ numberLong'对于存储无效。' – AbtPst

+0

它与数字字段/值有什么关系吗? – AbtPst

+0

'$ set'里面有'$ push'(或其他表达式)吗?你可以显示你想写入MongoDB的数据结构吗? – mgyongyosi