新しい値がレコード内の別のフィールドの値に依存する mongodb のレコードを更新する必要があります。
たとえば、次のコレクションが与えられた場合
{id: 1, list: []}
{id: 2, list: [6]}
{id: 3, list: []}
{id: 4, list: [6]}
{id: 5, list: []}
言えるようになりたい:
if ('id' is in [1, 3, 5]) => add the value '6' to 'list'
else => remove the value '6' from 'list'
結果は次のようになります。
{id: 1, list: [6]}
{id: 2, list: []}
{id: 3, list: [6]}
{id: 4, list: []}
{id: 5, list: [6]}
これは、次のように 2 つの更新クエリで実行できますが、1 で実行しようとしています。
db.collection.update({$in: [1, 3, 5]}, {$addToSet: {list: 6}})
db.collection.update({$in: [2, 4]}, {$pull: {list: 6}})
コードは Java です。