我正在使用带有 Micronaut 的 mongoDb 并尝试插入、获取、删除和更新记录。我已经按照这里的指南https://github.com/ilopmar/micronaut-mongo-reactive-sample
由于我没有在 MongoDB 中创建数据库,
Micronaut 配置
mongodb:
uri: "mongodb://${MONGO_HOST:localhost}:${MONGO_PORT:27017/FeteBird-Product}"
存储库
@Singleton
public class Repository<T> implements IRepository<T>{
private final MongoClient mongoClient;
public Repository(MongoClient mongoClient) {
this.mongoClient = mongoClient;
}
@Override
public MongoCollection<T> getCollection(String collectionName, Class<T> typeParameterClass) {
return mongoClient
.getDatabase("FeteBird-Product")
.getCollection(collectionName, typeParameterClass);
}
}
插入操作
public Flowable<List<Product>> findByFreeText(String text) {
LOG.info(String.format("Listener --> Listening value = %s", text));
try {
Product product = new Product();
product.setDescription("This is the test description");
product.setName("This is the test name");
product.setPrice(100);
product.setId(UUID.randomUUID().toString());
Single.fromPublisher(this.repository.getCollection("product", Product.class).insertOne(product))
.map(item -> product);
} catch (Exception ex) {
System.out.println(ex);
}
return Flowable.just(List.of(new Product()));
}
没有记录插入或创建数据库,我做错了什么?