MongoDb封顶子集合 [英] MongoDb capped sub-collection
问题描述
是否可以创建一个有上限的子集合.我正在尝试做类似的事情:
Is it possible to create a capped sub-collection. I'm trying to do something like:
user = {
name: String,
latest_messages: [String]
}
latest_messages
的上限为10.
如果没有,您有什么建议吗?
If not, what do you suggest as an alternative?
更新1:
似乎唯一手动解决的问题是保持阵列的上限.这是执行此操作的方法:
It appears as though keeping the array capped manually is the only solution. Here's a way to do this:
joe = {name: 'Joe', latest_messages: ['', '', '', '', '', '', '', '', '', '']}
db.users.save(joe)
db.users.update({name: 'Joe'}, {$push: {'latest_messages': 'hello'}})
db.users.update({name: 'Joe'}, {$pop: {'latest_messages': -1}})
db.users.update({name: 'Joe'}, {$push: {'latest_messages': 'world'}})
db.users.update({name: 'Joe'}, {$pop: {'latest_messages': -1}})
关于提高效率的任何建议吗?
Any suggestions on making this more efficient?
更新2:
有一个打开Jira票证,"SERVER-1050" ,要求添加能够将两个操作(推入和弹出)作为一个原子操作来完成.
There's an open Jira ticket, "SERVER-1050" that requests to add the ability to do the two (push & pop) as one atomic operation.
推荐答案
您只能指定上限集合的文档总数. 除非通过应用程序中的某些检查,否则无法限制数组的大小.
You can only specify the overall number of documents of a capped collection. There is no way limit the size of an array unless through some checks in your app.
这篇关于MongoDb封顶子集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!