まず「OBJECTID」
Document set = new Document("$set", new Document("firstName","newValue"));
yourMongoCollection.updateOne(new Document("_id",objectId), set);
私の値を再構成/再フォーマット/再入力するには、
findAndModify
と
ではなく、
update
とするだけです。ここで
をお楽しみください... C &のpのために、完全に動作する例です。
Boolean updateValue(DB db, DBCollection collection, String id, String key, Object newValue)
{
DBCollection collection = db.getCollection(<collection name>);
// Identify your required document (id, key, etc...)
DBObject query = new BasicDBObject("_ID",<ID or key value>);
DBObject update = new BasicDBObject("$set", new BasicDBObject(key, newValue));
//These flags will guarantee that you'lls get the updated result
DBObject result = collection.findAndModify(query, null, null, false, update,true, true);
//Just for precaution....
if(result == null)
return false;
return result.get(key).equals(newValue);
}
のための2番目のステートメントは何ですか? com.mongodb.DBCollectionクラスにappendという関数がないようです。しかし、2番目のステートメントがなければ正常に動作します。ありがとう –
2番目のステートメントは、あなたが単一の更新で望むほど多くの「セット」を追加できることを示しています。それは私の間違いだった、someCollection.appendの代わりにset.appendする必要があります、私は私の答えを更新しました。 –
こんにちは、これは正解ではなく、最後の値だけを設定します。私が下に投稿した答えを見てください。 –