mongodb - How do query among many collections then write result to corresponding collection -
if want query users' age > 18,
and export result corresponding collection,
how rewriteing following script?
the following psuedo code
source_collections = ["user_1", "user_2", ..., "user_999"] output_collections = ["result_1", "result_2", ..., "result_999"] pipeline = [ { "$match":{"age": > 18} } { "$out" : output_collections } ] cur = db[source_collections].runcommand('aggregate', {pipeline: pipeline,allowdiskuse: true})
the script you're looking like:
var prefix_source = 'user_'; var prefix_output = 'result_'; var source_collections = []; var output_collections = []; var numcollections = 999; (var = 1; <= numcollections; i++) { source_collections.push(prefix_source + i); output_collections.push(prefix_output + i); } var pipeline = [{'$match': {age: {'$gt': 18}}}, {'$out': ''}]; (var currentcollection = 0; currentcollection < source_collections.length; currentcollection++) { pipeline[pipeline.length - 1]['$out'] = output_collections[currentcollection]; var cur = db[source_collections[currentcollection]].runcommand('aggregate', {pipeline: pipeline,allowdiskuse: true}); }
and while you're @ it, var cur = ...
line simplified to
db[source_collections[currentcollection]].aggregate(pipeline, {allowdiskuse: true});
note: i've added piece generates arrays you, i'm sure you're not looking write them hand :d
Comments
Post a Comment