addVocabToList() 実装は次のようになります:
MongoCollection<Document> collection = database.getCollection("lists");
Document updatedDocument = collection.findOneAndUpdate(
Filters.eq("name", listName),
new Document("$push",
new BasicDBObject("terms", new BsonString(newVocabTerm))
.append("definitions", new BsonString(newDefinition))),
new FindOneAndUpdateOptions().returnDocument(ReturnDocument.AFTER));
そのコードは次のようになります:
- name =
listNameのドキュメントを検索します -
newVocabTermの値を追加しますtermsに 配列 -
newDefinitionの値を追加しますdefinitionsへ 配列 - 更新されたドキュメントを返します(この部分はオプションです)