[英]mongodb limit in the embedded document
我需要創建一個消息系統,一個人可以與許多用戶進行對話。 例如,我開始與user2,user3和user4交談,因此他們中的任何人都可以看到整個對話,如果對話在任何時間都不是私密的,任何參與者都可以將任何其他人添加到對話中。
這是我的想法如何做到這一點。 我正在使用Mongo,我的想法是使用對話框作為實例而不是消息。
架構如下所示:
{
_id : ...., // dialog Id
'private' : 0 // is the conversation private
'participants' : [1, 3, 5, 6], //people who are in the conversation
'msgs' :[
{
'mid' : ...// id of a message
'pid': 1, // person who wrote a message
'msg' : 'tafasd' //message
},
....
{
'mid' : ...// id of a message
'pid': 1, // person who wrote a message
'msg' : 'tafasd' //message
}
]
}
我可以看到這種方法的一些優點 - 在一個大型數據庫中,很容易找到某些特定會話的消息。 - 將人們添加到對話中會很容易。
但這是一個問題,我無法找到解決方案:對話變得太長了(以skype為例)並且他們沒有向你展示所有的對話,他們向你展示了一部分,之后他們正在展示你還有其他消息。 在其他情況下,跳過,限制解決了案例,但我怎么能在這里做到這一點?
如果這不可能你有什么建議嗎?
MongoDB文檔解釋了如何選擇數組元素的子范圍。
db.dialogs.find({"_id": [dialogId]}, {msgs:{$slice: 5}}) // first 5 comments
db.dialogs.find({"_id": [dialogId]}, {msgs:{$slice: -5}}) // last 5 comments
db.dialogs.find({"_id": [dialogId]}, {msgs:{$slice: [20, 10]}}) // skip 20, limit 10
db.dialogs.find({"_id": [dialogId]}, {msgs:{$slice: [-20, 10]}}) // 20 from end, limit 10
您可以使用此技術僅選擇與UI相關的消息。 但是,我不確定這是一個好的架構設計。 您可能需要考慮從“存檔”消息中分離出“可見”消息。 它可能使查詢更容易/更快。
如果您的對話有很多消息,請注意以下事項:
我的建議是:
結果:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.