如何在聚合MongoDB中设置'cursor'选项 [英] How to set 'cursor' option in Aggregate MongoDB

查看:1091
本文介绍了如何在聚合MongoDB中设置'cursor'选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的API服务器上有一个Aggregate命令.在我将MongoDB更新到3.6.3之前,它一直运行良好.现在,我得到了这样的错误:'cursor'选项是必需的,除了带有explain参数的聚合. 这是我的示例:

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);
            }
        ); 

推荐答案

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 

                   });

在版本3.4中进行了更改:MongoDB 3.6删除了不带游标选项的聚合命令,除非该命令包含解释选项.除非您包含说明选项,否则必须指定光标选项.示例:

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: {}.

要指示具有非默认批处理大小的游标,请使用游标:{batchSize:}.

To indicate a cursor with a non-default batch size, use cursor: { batchSize: }.

下面的示例对articles集合执行聚合操作,以计算出现在集合中的标签数组中每个不同元素的计数.有关更多详细信息,请参见 https://docs.mongodb.com/manual/reference/command/aggregate/

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: { }
} )

这篇关于如何在聚合MongoDB中设置'cursor'选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆