从特定文档开始检索文档

时间:2017-06-14 18:14:16

标签: mongodb mongodb-query

从MongoDB中的以下排序集合中,如何创建新集合,使其包含包含{"hash": "a", "key1": "val1", "key2": "val2"}, {"hash": "b", "key1": "val3", "key2": "val4"}, {"hash": "c", "key1": "val5", "key2": "val6"}, {"hash": "d", "key1": "val7", "key2": "val8"}

的文档之后的所有文档
{"hash": "c", "key1": "val5", "key2": "val6"},
{"hash": "d", "key1": "val7", "key2": "val8"}

所以我正在尝试创建一个包含以下内容的新集合:

$ cat file.csv
"PT3","THE TORONTO-DOMINION BANK","2016-05-24T00:36:42Z","ANZ BANK NEW ZEALAND LIMITED","HZSN7FQBPO5IEWYIGC72","MAS,CA.ON.OSC,ASIC*,AAAA"
"xyz","THE TORONTO","2016-05-24T00:36:42Z","ANZ BANK NEW ZEALAND LIMITED","HZSN7FQBPO5IEWYIGC72","MAS,CA.ON.OSC,ASIC*,AAAA"

我可以使用find()检索包含{“hash”:“b”}的文档,但这不能帮助我遍历原始集合中的后续元素

1 个答案:

答案 0 :(得分:1)

如果我理解你是如何正确地对哈希值进行排序的话,那么下面的代码将按照你想要的方式进行:

db.getCollection('test').aggregate([{$match: {hash: {$gt: 'b'}}}, 
{$out: 'new'}])

只需替换集合的名称和要将结果放入的新集合($out运算符指定后者)。