I am using meteor to make a query( for a publication) to find all the Results
generated by Workers
which has finished their work:
Results
has the following structure:
result example 1:
{
_id: "sldf234sdf"
result_a:0,
result_b:0
}
result example 2:
{
_id: "ghjwef23qql"
result_a:0,
result_b:0
}
Workers
is defined as:
{
_id: "iweyr23s"
results:["sldf234sdf", "ghjwef23qql"], //here is a list of
tag:'running'
}
Here is what I am trying to do:
// 1), I want to find all the workers which is finished with their tag
const workers = Workers.find({tag:'done'});
// 2), I want to get the resultid arrays in all the workers, then combine it into a big array
const results_id_arrays = workers[0].results + workers[1].results + ...
const results = Results.find({_id:{$in: results_id_arrays }});
So, my question is, how to make a mongodb query to implement the second step?
答案 0 :(得分:0)
你可以使用强大的underscorejs
。还有一个流星包。查看文档here。
//fetch results
const workers = Workers.find({tag:'done'}, {results: 1, _id: 0}).fetch();
//pluck only the ids, without field name
const plucked = _.pluck(workers, 'results');
//use plucked ids to find results
return Results.find({_id:{$in: plucked }});
答案 1 :(得分:0)
这是你在找什么?
你可以得到这样的结果ID:
const results_id_arrays = Workers.aggregate({$project:{a:'$results'}},
{$unwind:'$a'},
{$unwind:'$a'},
{$group:{_id:'a',res:{$addToSet:'$a'}}}).map(function(e) {return e.res})
然后
const results = Results.find({_id:{$in: results_id_arrays }});
这有用吗?