Answer a question

I cannot get a result with aggregation option parameter. Here is my aggregation:-

var emails = getAllEmails();
var listMatchColl = 'list_matches_' + insertedId;
SurveyDL.aggregate([
     { $match: { email: { $in: emails } } },
     { $out: listMatchColl }
 ], {
   allowDiskUse: true
 }).exec(function(err, data) {
  if (err) return console.log('err', err);
    console.log('data',data);
 });
}

When i execute above code it thrown and error i.e,

Error: Arguments must be aggregate pipeline operators at Aggregate.append (/home/vivek/nrich/node_modules/mongoose/lib/aggregate.js:89:11) at new Aggregate (/home/vivek/nrich/node_modules/mongoose/lib/aggregate.js:48:17)

I have also use the alternative way but it still throw same error. Alternative way :-

var emails = getAllEmails();
var listMatchColl = 'list_matches_' + insertedId;
SurveyDL.aggregate([
     { $match: { email: { $in: emails } } },
     { $out: listMatchColl }
 ], {
   allowDiskUse: true
 },function(err, data) {
  if (err) return console.log('err', err);
    console.log('data',data);
 });

Answers

Try setting the allowDiskUse() option for the aggregation query:

var emails = getAllEmails();
var listMatchColl = 'list_matches_' + insertedId;
SurveyDL.aggregate([
    { '$match': { 'email': { '$in': emails } } },
    { '$out': listMatchColl }
 ]).allowDiskUse(true)
   .exec(function(err, data) {
        if (err) return console.log('err', err);
        console.log('data',data);
    }); 

or using the fluent API:

SurveyDL.aggregate()
        .match({ 'email': { '$in': emails } })
        .append({ '$out': listMatchColl })
        .allowDiskUse(true)
        .exec(function(err, data) {
            if (err) return console.log('err', err);
            console.log('data',data);
        });

You might be running over the 16MB aggregation size limit and the allowDiskUse() option is not sufficient if your data will be > 16MB since it just lets you use sort when your data is large. Consider using the aggregation cursor form to access your aggregated documents:

var cursor = SurveyDL.aggregate([
    { $match: { email: { $in: emails } } }    
 ]).cursor({ batchSize: 1000 }).exec();

cursor.each(function(error, doc) {
  // use doc
}); 

Logo

MongoDB社区为您提供最前沿的新闻资讯和知识内容

更多推荐