2017-08-02 2 views
0

Je suis en train de mettre à jour un seul document dans ma collection de MongoDB commeMongoDB MongoCollection: ne peut pas mettre à jour le terrain

JSONArray jsonArr = new JSONArray(); 

/*Some processing to add stuff to jsonArr*/ 

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

mais je reçois

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) 

noter que, voici comment je l'avais créé à l'origine de la document en premier lieu

 Document unprocessedMeta = new Document("key",_id); 

     JSONArray arr = new JSONArray(); 
     /*some processing to add stuff to arr */ 

     unprocessedMeta.append("asd", arr); 
     mongoCollection.insertOne(unprocessedMeta); 

et cela a très bien fonctionné. Notez que la clé asd a une valeur JSONArray. En outre, je peux sélectionner le document par

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

et il montre juste comme prévu

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

et je peux obtenir le JSONArray intérieur comme

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

et je peux itérer sur le existingAsd et accéder aux objets.

Pourquoi ne puis-je pas mettre à jour? Fondamentalement, je veux remplacer la valeur de la clé asd dans le document qui correspond key. J'ai même essayé

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

mais je reçois la même erreur

Répondre

1

Depuis MongoDB fonctionne à l'aide BSON, je crois qu'il est parce qu'il n'y a pas JSONObject à BSON sérialiseur.

Essayez d'utiliser un List<Document> à la place:

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

Le code ci-dessus imprimé:

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

qui a montré que le champ docarray a été mis à jour avec succès.

+0

merci, cela a fonctionné! – AbtPst

1

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

+0

merci, mais maintenant je 'peut pas mettre à jour { "ert": "dvwmv", "zxc": "ININ", "qaz": "mnmnbm", "tt": 24889} com.mongodb.MongoWriteException: Le préfixe dollar ($) du champ '$ numberLong' dans 'imageList.0.tt. $ NumberLong' n'est pas valide pour le stockage.' – AbtPst

+0

cela a-t-il quelque chose à voir avec le fichier/valeur numérique? – AbtPst

+0

Avez-vous un '$ push' (ou une autre expression) dans' $ set'? Pouvez-vous montrer la structure de données que vous souhaitez écrire dans MongoDB? – mgyongyosi