spring - 使用 Spring 数据 MongoDB @Document 创建一个上限集合

我正在尝试使用 MongoDB 在 spring-data 中提供响应式支持。我正在使用 spring-boot 2.0.0。

通常我会在我的项目中编写这样的域对象:

@Document
public class PriceData {
    ......
}

使用这个 spring-data 它将在 MongoDB 中创建一个名为 priceData 的集合。如果我想自定义它,那么我会使用 collection 属性:

@Document(collection = "MyPriceData")

由于我想尝试对 MongoDB 的响应式支持,我想创建一个有上限的集合,以便我可以使用 @Tailable 游标查询。

我可以在我的 MongoDB 数据库中按照指定 here 创建一个上限集合:

CollectionOptions options = new CollectionOptions(null, 50, true);
mongoOperations.createCollection("myCollection", options);

db.runCommand({ convertToCapped: 'MyPriceData', size: 9128 })

如果我使用一些外部 MongoDB 数据库,我可以只运行一次此命令,这不是一个大问题。但是如果我使用嵌入式 MongoDB,那么我会将它放在一个类中,该类将在每次启动期间执行。

无论哪种方式,我都会在第一个请求之前创建一个集合。所以我想知道是否有一种方法,我可以向 spring-data-mongodb 指定我需要一个上限集合而不是常规集合。 不幸的是 @Document 在这种情况下没有帮助。

最佳答案

所以下面是来自Oliver

Might be a good idea to have those options exposed to the @Document annotation to automatically take care of them when building the mapping context but we generally got the feedback of people wanting to manually handle those collection setup and indexing operations without too much automagic behavior. Feel free to open a JIRA in case you'd like to see that supported nevertheless.

这是回到 2011 .迄今为止,它似乎仍然是正确的。如果你真的需要使用注解来处理它,你应该打开一个 JIRA 票

https://stackoverflow.com/questions/48418995/

相关文章:

php - 如何单击表条目并在另一个 mongodb 集合(php/jquery)中搜索它

javascript - MongoError : Unknown modifier: $pushA

javascript - 使用 Nodejs 在 mongodb 中存储文件

mongodb - 如何从 Mongoose 中的对象 ID 获取创建日期?

mongodb - Mongod 服务启动退出,代码为 100

regex - Mongo 正则表达式搜索的索引范围

node.js - 'this' 在 Mongoose 预保存 Hook 中未定义

java - Spring数据mongo在Query中使用OR

mongodb - 如何在复杂的 Collection-Object 中附加新文档

mongodb - 为什么直接搜索 MongoDB 分片的全文搜索比通过集群管理器 (mongos)