如何使用Spring Data创建一个有上限的集合? -MongoDB [英] How to Create a capped collection with Spring Data? - MongoDB

查看:135
本文介绍了如何使用Spring Data创建一个有上限的集合? -MongoDB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

所以我在mongo文档中阅读了此内容:

So I read this in the mongo documentation:

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

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.

我认为太棒了!这是我所需要的,但我对此表示怀疑.是否可以使用SpringData创建此类集合???我在SpringData文档中找不到任何内容.

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.

有人对此有所了解吗?

谢谢

推荐答案

有一种方法createCollection(…)带有CollectionOptions参数,您可以在其中指定要设置上限的集合:

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);

将那些选项暴露在@Document注释中以在构建映射上下文时自动进行处理可能是一个好主意,但是我们通常会得到人们希望手动处理这些集合设置和索引操作的反馈许多自欺欺人的行为.随时打开JIRA,以防您仍然希望获得支持.

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.

这篇关于如何使用Spring Data创建一个有上限的集合? -MongoDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆