2016-12-13 4 views
0

文書を更新しようとしていますが、できません。 私はupdateOne()メソッドだけでなく、update()も使ってみました。MongoCollectionの更新<Documnet>

public boolean updateDocument(String docId, String JSONString) { 
    try{ 
     MongoCollection<Document> collection = getCollection("abc"); 
     DBObject queryObject = (DBObject)(collection.find(eq("_id","docId")).first()); 
     if(queryObject==null) 
      return false; 
     else { 
      DBObject updateObject = (DBObject)JSON.parse(JSONString); 
      collection.update(queryObject, updateObject); 
      return true; 
     } 
    }catch (Exception e) { 
     System.out.println(e.getMessage()); 
     return false; 
    } 
} 

エラーは次のように来ている:メソッドの更新(DBOBJECT、DBOBJECTは)私はupdateOneを使用することができますタイプするMongoCollection のために定義されていないがBSONの引数に適用されますが、私はのdbobjectのために使用する必要があります。.. 缶誰任意の解決策を提案できますか?

答えて

0

私はこれを試して、働いた。私はこれだけで指定したクラス全体でMongoCollectionタイプのbeacuseを使う必要があった。

public boolean updateDocument(String docId, String JSONString) { 
    try{ 
     MongoCollection<Document> collection = getCollection("123"); 
     Document queryObject = collection.find(eq("_id",docId)).first();    //finding the document then converting to DBObject type 
     if(queryObject==null) 
      return false; 
     else { 
      Document updatedoc = new Document(); 
      updatedoc = Document.parse(JSONString); 
      collection.updateOne(queryObject,updatedoc); 
      return true; 
     } 
    }catch (Exception e) { 
     System.out.println(e.getMessage()); 
     return false; 
    } 
} 
関連する問題