我在 MongoDB 中表示数据时遇到问题。我正在使用这种模式设计,其中日期和单词的组合是唯一的。
{'date':2-1-2011,
'word':word1'
users = [user1, user2, user3, user4]}
{'date':1-1-2011,
'word':word2'
users = [user1, user2]}
有固定数量的日期,大约 200 个;每个日期可能有 100k+ 字;和 100k+ 用户。
我用这样的算法插入记录:
while records exist:
message, user, date = pop a record off a list
words = set(tokenise(message))
for word in words:
collection1.insert({'date':date, 'word':word}, {'user':user})
collection2.insert('something similar')
collection3.insert('something similar again')
collection4.insert('something similar again')
然而,这种模式导致了非常大的集合,并且糟糕的性能非常糟糕。我在四个集合中的每一个中都插入了不同的信息,因此这是对数据库的大量操作。
我正在考虑以这样的格式表示数据,其中设置了单词和用户数组。
{'date':'26-6-2011',
'words': [
'word1': ['user1', 'user2'],
'word2': ['user1']
'word1': ['user1', 'user2', 'user3']]}
这背后的想法是减少数据库操作的数量。因此,对于算法的每个循环,我只为每个集合执行一次更新。但是,我不确定如何对此执行更新/更新,因为在算法的每个循环中,我可能需要插入一个新单词、用户或两者。
任何人都可以推荐一种更新此文档的方法,或者任何人都可以建议替代模式吗?
谢谢