我想在我的 MongoDB 集合中构建倒排列表。集合看起来像这样:
{ "word" : 2, "docToPos" : { "1" : [ 0 ] } }
{ "word" : 5, "docToPos" : { "1" : [ 1 ] } }
{ "word" : 1, "docToPos" : { "1" : [ 2 ], "2" : [ 1 ] } }
{ "word" : 9, "docToPos" : { "2" : [ 2, 43, 1246 ] } }
word
是字典中的某个 id 并且docToPos
映射document
到position
- 例如,单词 2 在文档 1 中的位置 1,单词 9 在文档 2 中的位置 2、43 和 1246。
我想添加到数据库的每个新文档只是一个带有单词 id 的数组:
[23, 43, 75, 18, ... ]
所以使用spring-mongo我有这个java代码:
for (int i=0; i < array.length; i++) {
invertedListDao.upsert(array[i], documentId, i);
}
(upsert方法由我实现)
此解决方案有效,但如果文档有 100 000 个单词,则需要 100 000 次查询才能到达 mongo。
所以最后,我的问题是:有没有一种方法可以更快地做到这一点?例如:一次查询整个数组并在数据库中执行?我知道里面有eval
函数mongo
,但是里面没有mongo-spring