mongodb 嵌套的超长存储数组是否合适
Is a mongodb nested array of very long storage appropriate
I made a chat system, and all the information in the chatroom is expected to be stored through MongoDB's Document. Each message sent is appended to the room messages array.
数据结构如下:
这个blog认为这是正确的存储方式。
但是the official documentation说每个文件不能超过16MB
- 收纳设计合理吗?
- 还有其他更好的设计吗?
这真的取决于您将如何查询数据,但我认为设计还可以。
将所有消息都放在一个聊天文档中将保证您的一致性并在并发问题上保证您的聊天安全。
关于16MB的限制,我觉得还是可以的
无论如何,出于性能原因,我不会将整个消息历史记录保存在文档中(如果聊天室非常繁忙,查询会变得非常慢),但我会将聊天列表的大小限制为,假设有 100 个元素(最近的元素),我会使用另一种方法来备份整个聊天室历史记录。
作为我自己问题的后续:
I need to control the length of room messages to be less than 100, similar to the queue, first-in, first-out method.
golang的示例代码如下:
query := bson.M{
"room_customer.customer_id": msg.FromUserName,
}
changes := bson.M{
"$push": bson.M{"room_messages": bson.M{"$each": []model.RoomMessage{
{
Msg: msgText,
CreateTime: time.Now(),
},
},
"$slice": -100}},
}
roomCollection.Update(query, changes)
这样可以让聊天室的聊天记录一直保持最多100条消息,希望对遇到类似问题的人有所帮助!
I made a chat system, and all the information in the chatroom is expected to be stored through MongoDB's Document. Each message sent is appended to the room messages array.
数据结构如下:
这个blog认为这是正确的存储方式。
但是the official documentation说每个文件不能超过16MB
- 收纳设计合理吗?
- 还有其他更好的设计吗?
这真的取决于您将如何查询数据,但我认为设计还可以。 将所有消息都放在一个聊天文档中将保证您的一致性并在并发问题上保证您的聊天安全。
关于16MB的限制,我觉得还是可以的
无论如何,出于性能原因,我不会将整个消息历史记录保存在文档中(如果聊天室非常繁忙,查询会变得非常慢),但我会将聊天列表的大小限制为,假设有 100 个元素(最近的元素),我会使用另一种方法来备份整个聊天室历史记录。
作为我自己问题的后续:
I need to control the length of room messages to be less than 100, similar to the queue, first-in, first-out method.
golang的示例代码如下:
query := bson.M{
"room_customer.customer_id": msg.FromUserName,
}
changes := bson.M{
"$push": bson.M{"room_messages": bson.M{"$each": []model.RoomMessage{
{
Msg: msgText,
CreateTime: time.Now(),
},
},
"$slice": -100}},
}
roomCollection.Update(query, changes)
这样可以让聊天室的聊天记录一直保持最多100条消息,希望对遇到类似问题的人有所帮助!