简体   繁体   English

API 将数据保存在错误的 MongoDB 集合中

[英]API saving data in the wrong MongoDB collection

Hi I am following this guide for creating API.嗨,我正在按照指南创建 API。 I have similar code to what is in this guide.我有与本指南中类似的代码。 Now I am trying to create another API within the same Project with the data stored in a separate collection.现在我正在尝试在同一个项目中创建另一个 API,并将数据存储在单独的集合中。 But Whenever I make any API calls, it always uses the same database.但是每当我进行任何 API 调用时,它总是使用相同的数据库。 I cannot figure out where the database location is configured.我无法弄清楚数据库位置的配置位置。 Can I please get help on finding out where I can configure the database connectons to my Java program?能否请我帮忙找出在哪里可以将数据库连接配置到我的 Java 程序? Here is my code.这是我的代码。

@CrossOrigin(origins = "http://localhost:4200")
@RestController
@RequestMapping("/api/v1")
public class TicketController {
    @Autowired
    private TicketRepository ticketRepository;
    @Autowired
    private TicketGeneratorService ticketGeneratorService;
    
    @GetMapping("/tickets")
    @ApiOperation(value = "Retrieves all Tickets in the database")
    public List<Ticket> getAllTickets() {
        return ticketRepository.findAll();
    }
    
    @PostMapping("/tickets/create")
    @ApiOperation(value = "Adds a Ticket to the database")
    public Ticket createTicket(@Valid @RequestBody Ticket ticket) {
        ticket.setId(ticketGeneratorService.generateSequence(Ticket.SEQUENCE_NAME));
        return ticketRepository.save(ticket);
    }   
    
}

Here is my application.properties file这是我的 application.properties 文件

This is the code I have under my application.properties file.这是我的 application.properties 文件下的代码。 I do not understand how this can be incorrect.我不明白这怎么可能是不正确的。 employeeDatabase is the Database I am using, and inside it I am trying to use two different collections. employeeDatabase 是我正在使用的数据库,在其中我尝试使用两个不同的 collections。 All the data I create and retrieve from only comes from one of the collections.我创建和检索的所有数据仅来自 collections 之一。 How can I get access to the other collection?我怎样才能访问其他集合?

# MONGODB (MongoProperties)
spring.data.mongodb.uri=mongodb://localhost:27017/employeeDatabase

employeeDatabase is the database which includes the collections that I am using. employeeDatabase 是包含我正在使用的 collections 的数据库。

Here is a list of my classes.这是我的课程列表。 在此处输入图像描述

Check your application.properties for the name of the database, chances are you forgot to change the name from the first API.检查您的application.properties以获取数据库的名称,您可能忘记从第一个 API 更改名称。

If you wish to have multiple mongoDBs in your project, check this answer for guidance.如果您希望项目中有多个 mongoDB,请查看此答案以获取指导。

If you are using only one database, but documents get written to the same collection of the first API, chances are you didn't change the model class in the second API. If you are using only one database, but documents get written to the same collection of the first API, chances are you didn't change the model class in the second API.

Create a new model for a new collection:为新集合创建一个新的 model:

import org.springframework.data.mongodb.core.mapping.Document;

@Document(collection = "collection_name")
public class CollectionName { ... }

And use this model for writing to the database now.现在使用这个 model 写入数据库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM