以下は、mongo シェルで適切に実行できるクエリです。
db.students.update({ _id : 139 }, {$pull : { scores: {type :'homework' }}})
Java プログラム / Mongo Spring Template を使用して同じことを実行する必要があります。誰かがこれを行う方法を教えてもらえますか?
以下は、mongo シェルで適切に実行できるクエリです。
db.students.update({ _id : 139 }, {$pull : { scores: {type :'homework' }}})
Java プログラム / Mongo Spring Template を使用して同じことを実行する必要があります。誰かがこれを行う方法を教えてもらえますか?
クエリは次のようになります。
UpdateResult updateResult = collection.updateOne(eq("_id", 123),
new Document("$pull", new Document("scores",
new Document("type", "homework"))
)
);
ここには、インターフェイス全体と updateOne のドキュメントがあります。
データベースとの接続も必要なので、このクイック ツアーに従って作業を完了することをお勧めします。
編集
spring mongo で同じことを行うには、( docsに記載されているように) 次のようなことができます。
import static org.springframework.data.mongodb.core.query.Criteria.where;
import static org.springframework.data.mongodb.core.query.Query;
import static org.springframework.data.mongodb.core.query.Update;
...
WriteResult wr = mongoTemplate.updateFirst(new Query(where("accounts.accountType").is(Account.Type.SAVINGS)),
new Update().pull("students.$.scores", new Document("type", "homework")), Account.class);
PS: この回答はおそらくそのままでは機能しません。より大きなコード スニペットを提供するためのより良いヘルプが必要な場合は、これは単なる例です。