【问题标题】:Accesing data from different collections of mongodb via springboot通过 Spring Boot 从不同的 mongodb 集合中访问数据
【发布时间】:2019-03-11 05:17:43
【问题描述】:

我一直在尝试在 Spring 启动应用程序中加入两个不同的 MongoDB 集合,以便在单个 @GetMapping 调用中获取数据。

这些是我的application.properties

spring.data.mongodb.uri=mongodb://localhost:27017/alpha1
#spring.data.mongodb.uri=mongodb://username:password@host:port/database
server.port = 4000

这就是我的存储库的样子:

package com.example.demo.repository;

import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;

import com.example.demo.model.Person;

@Repository
public interface PersonRepository extends MongoRepository<Person, String>{
    public Person findByContId(String firstName);
    public Person findByUid(String uid);
}

问题是我需要创建另一个关于用户提要的集合,并且它的数据应该在同一个方法中获取。

【问题讨论】:

    标签: mongodb spring-boot spring-data spring-data-mongodb


    【解决方案1】:

    您需要为相同的内容创建另一个存储库并在任何需要的地方获取它

    @Repository
    public interface UserFeedsRepository extends MongoRepository<UserFeeds, String>{
       public List<UserFeeds> findAll();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-11
      • 2014-01-08
      • 2017-02-08
      相关资源
      最近更新 更多