プログラムの以前のバージョンからのいくつかの既存のフィールドを持つプレーヤーデータのデータベースがあります。古いドキュメントの例:
{
"playername": "foo"
}
しかし、新しいバージョンで生成されたプレーヤー ドキュメントは次のようになります。
{
"playername": "bar",
"playercurrency": 20
}
問題は、クエリを実行しようとすると、playercurrency
が存在しないfoo
ために NullPointerException が発生することです。に保存できる他のデータを邪魔することなく、フィールドをに追加したいと考えています。例を使用していくつかのコードを試しました:playercurrency
foo
playercurrency
foo
foo
$exists
players.updateOne(new Document("playername", "foo"), new Document("$exists", new Document("playername", "")));
players.updateOne(new Document("playername", "foo"), new Document("$exists", new Document("playercurrency", 20)));
私の考えでは、それplayercurrency
が存在しないという理由だけで更新され、存在するために放置されるというplayername
ことです。これは私の最初の MongoDB プロジェクトの 1 つであり、できる限り多くのことを学びたいので、exists をひどく間違って使用している可能性があります。