MongoDB 2.4 引入了 Capped Arrays:
http ://docs.mongodb.org/manual/release-notes/2.4/#limit-number-of-elements-in-an-array
考虑以下文档结构(带有嵌套数组):
thread {
"_id" : ObjectId("5191039d48ef54c24a000002"),
"messages" : [
{"content" : "hello", "sent_at": "ISODate("2013-05-13T15:15:44.162Z")},
{"content" : "hi again", "sent_at": "ISODate("2013-05-13T15:15:48.162Z")}
]
}
使用新的封顶数组功能,如何在添加新消息(例如第 51 条)时删除最旧的消息?
换句话说,如何将嵌套数组“消息”限制为 50 个项目?