在另一个请求中使用来自数据库请求的结果单值

时间:2019-04-30 13:58:26

标签: spring spring-data spring-webflux project-reactor spring-mongodb

我想通过用户名获得一个用户,将该用户分配为注释的作者,然后将该注释保存到数据库中。问题是我得到一个包含用户的Mono,但无法将其分配给user类型的author字段。

我要做什么:

# Get the total number of upvotes cast on the app
# First, get a list of all the upvote values
all_upvotes_list = list(tracks_collection.find( { },
                                { 'upvotes': 1, '_id' :0 }
                            ))

# Iterate through the list and sum the values
all_upvotes = sum(item['upvotes'] for item in all_upvotes_list)

1 个答案:

答案 0 :(得分:0)

userRepository
        // Find the User
        .findByUsername(userPrincipalService.getCurrentUserPrincipal()
                .map { it.username })
        // Map the User to a Note, while setting the note's owner 
        .map { user ->
            noteMapper.fromDTO(noteDTO).apply {
                owner = user
            }
        }
        // Save the Note 
        .flatMap { note -> noteRepository.save(note) }