简体   繁体   English

MongoDB BSON 文档大小异常

[英]MongoDB BSON Document Size exception

I have created a chat Mongoose model for my chat API.我为我的聊天 API 创建了一个聊天 Mongoose model。 I stored each chat json to messages field of the chat model.我将每个聊天 json 存储到聊天 model 的消息字段中。 I think that when the messages field filled with json the document size increase and it will exceed the 16MB capacity.我认为当用 json 填充消息字段时,文档大小会增加,并且会超过 16MB 的容量。 In that moment I want to create a new document and I want to save the chat messages to that document and i will track these document in other document.在那一刻,我想创建一个新文档,并将聊天消息保存到该文档中,然后我将在其他文档中跟踪这些文档。 So how can i handle the exceed error.那么我该如何处理超出错误。 I think that i can handle with that error by using try/catch for that reason what is the name of this exception?我认为我可以通过使用 try/catch 来处理该错误,因为这个异常的名称是什么?

Note: I just store the text message there is no binary mesage like video or photograph.注意:我只存储文本消息,没有像视频或照片这样的二进制消息。

With mongo is preferable to handle large collection of small document instead of small collection of big documents.使用 mongo 处理小文档的大集合而不是大文档的小集合更可取。

You should change your chat model which must look like this for the moment:你应该改变你的聊天 model ,它现在看起来应该是这样的:

// chat model
{
    _id: ObjectId("/..."),
    title: "...",
    messages: [
        {_id: 1, text: "...."},
        {_id: 2, text: "...."},
        ....
    ]
}

To something like this:对于这样的事情:

// chat model
{
    _id: ObjectId("/..."),
    title: "..."
}

With a collection of messages references the chat model使用一组消息引用聊天 model

 // messages collections
 {
  _id: ObjectId("...."),
  chat_id: "...",
  text: "..."
},

{
  _id: ObjectId("...."),
  chat_id: "...",
  text: "...
},

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM