问题
I have an Aggregate command on my API Server. It worked well until I updated my MongoDB to 3.6.3. Now I get this kind of error:"The 'cursor' option is required, except for aggregate with the explain argument". This is my example:
ArchiveReq.aggregate({
$project: {
projectId: 1,
projectName: 1,
shortDescription: 1,
numOfStudents: 1,
creationDate: 1,
matches: {$ne: ['$creationDate', '$updateDate']}
}
},
function (err, Requests) {
if (err)
return res.send(err)
res.json(Requests);
}
);
回答1:
ArchiveReq.aggregate([
$project: {
projectId: 1,
projectName: 1,
shortDescription: 1,
numOfStudents: 1,
creationDate: 1,
matches: {$ne: ['$creationDate', '$updateDate']}
}
],
{
cursor: { batchSize: 0 }
}
).exec(function(error, cursor) {
// use cursor
});
Changed in version 3.4: MongoDB 3.6 removes the use of aggregate command without the cursor option unless the command includes the explain option. Unless you include the explain option, you must specify the cursor option.Example:
To indicate a cursor with the default batch size, specify cursor: {}.
To indicate a cursor with a non-default batch size, use cursor: { batchSize: }.
The following example performs an aggregate operation on the articles collection to calculate the count of each distinct element in the tags array that appears in the collection.For more details refer https://docs.mongodb.com/manual/reference/command/aggregate/
db.runCommand( {
aggregate: "articles",
pipeline: [
{ $project: { tags: 1 } },
{ $unwind: "$tags" },
{ $group: { _id: "$tags", count: { $sum : 1 } } }
],
cursor: { }
} )
回答2:
if u want to use aggregate function in mongo 3.6
you can try this
let resArchiveReq= await ArchiveReq.aggregate([
{ ... }
]).cursor({}).exec().toArray()
return res.json({ result: resArchiveReq })
来源:https://stackoverflow.com/questions/49172981/how-to-set-cursor-option-in-aggregate-mongodb