Spring boot 2.0.0被动mongodb无法添加到集合中

时间:2018-03-22 21:59:26

标签: mongodb spring-boot

我的用例非常简单:

我为每个人存储人员和一组笔记,我希望在mongoldb中看起来像这样:

{
    :id: ObjectId(...)
    "name": "Bob"
    "notes": ["nice", "tall"]
}

这是我的尝试:

@Document
@Builder(toBuilder = true)
@AllArgsConstructor
@NoArgsConstructor
@Getter
public class Person {
    @Id
    ObjectId id;
    private String name;
    @Singular
    private Set<String> notes;

}

如果我使用这样的标准存储库:

@Repository
public interface PersonRepository extends ReactiveMongoRepository<Person, ObjectId> {
    Mono<Person> findByName(String name);
}

尝试添加一个像这样的新笔记:

personRepository.findByName(name)
.doOnSuccess( person -> personRepository
    .save(person.toBuilder().note(newNote).build())
    .block() //or subscribe(), doOnSuccess(), doOnError() does not matter
);

在此之后再次从数据库中按名称检索人员时,该注释不存在。

如果不使用构建器,它也无法使用:

person.getNotes().add(newNote)
personRepository.save(person)

这是如何正确完成的?

编辑:

反之亦然,如果我在数据库中插入注释,它们会正确加载。

0 个答案:

没有答案