如何使用 Spring Data 创建上限集合? - MongoDB

发布于 2024-12-11 18:28:43 字数 398 浏览 3 评论 0 原文

我正在做一个简单的项目。我正在使用 SpringData 和 MongoDB。

创建正常集合的一切都很完美,但现在我必须注册信息,我的意思是日志记录功能。

所以我在 mongo 文档中读到了这个:

上限集合提供了一种高性能的存储方式 将文档记录在数据库中。将对象插入未索引的对象 capped collection 将接近记录到 a 的速度 文件系统。此外,通过内置的 FIFO 机制,您可以 不存在使用过多磁盘空间进行日志记录的风险。

我觉得太棒了!这就是我需要的,但我有疑问。可以用 SpringData 创建这种类型的集合吗???我在 SpringData 文档中找不到任何内容。

有人知道这件事吗?

谢谢

I'm working on a simple project. I'm using SpringData and MongoDB.

Everything is perfect creating normal collections, but now I have to register information, I mean a logging functionality.

So I read this in the mongo documentation:

Capped collections provide a high-performance means for storing
logging documents in the database. Inserting objects in an unindexed
capped collection will be close to the speed of logging to a
filesystem. Additionally, with the built-in FIFO mechanism, you are
not at risk of using excessive disk space for the logging.

I thought great! this is what I need, but I have a doubt. Is posible to create this kind of collections with SpringData??? I couldn't find anything in SpringData documentation.

Someone knows something about this?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

染年凉城似染瑾 2024-12-18 18:28:43

有一个方法 createCollection(…) 采用 CollectionOptions 参数,您可以在其中指定要限制的集合:

// The 'true' is setting it to capped
CollectionOptions options = new CollectionOptions(null, 50, true);
mongoOperations.createCollection("myCollection", options);

将这些选项公开给 可能是个好主意@Document 注释在构建映射上下文时自动处理它们,但我们通常得到人们的反馈,希望手动处理这些集合设置和索引操作,而不需要太多的自动行为。如果您希望看到支持,请随意打开 JIRA。

There's a method createCollection(…) taking a CollectionOptions argument where you can specify a collection to be capped:

// The 'true' is setting it to capped
CollectionOptions options = new CollectionOptions(null, 50, true);
mongoOperations.createCollection("myCollection", options);

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.

朮生 2024-12-18 18:28:43

CollectionOptions options = new CollectionOptions(null, 5000, true) 现已弃用。相反,您应该使用以下代码:

CollectionOptions options = CollectionOptions.empty()
                    .capped().size(5242880)
                    .maxDocuments(5000)

不要忘记指定大小。有关详细信息,请参阅可尾游标

CollectionOptions options = new CollectionOptions(null, 5000, true) is now deprecated. Instead you should use the following code:

CollectionOptions options = CollectionOptions.empty()
                    .capped().size(5242880)
                    .maxDocuments(5000)

Don't forget to specify the size. For more info see Tailable Cursors.

吃颗糖壮壮胆 2024-12-18 18:28:43

如果您有 spring-data 创建的集合(例如:reservation),您可以轻松地将其转换为 capped,就像这样:

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

阅读 mongodb 手册:https://docs.mongodb.com/manual/reference/command/convertToCapped/

ps: @Tailable 注释非常性感,它可以帮助您跟踪该集合的更新并使用反应式编程原理对其更改做出反应

If you have collection created by spring-data (for example: reservation), you can easily convert it to capped, just like so:

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

read mongodb manual: https://docs.mongodb.com/manual/reference/command/convertToCapped/

ps: @Tailable annotation is very sexy, it can help you track updates for that collection and react on it changes using reactive programming principles

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文