mongodb - MongoDB中聚合($match)和查找之间的区别?

聚合函数内部使用的 $match 操作符和 Mongodb 中的常规 find 有什么区别?

为什么 find 函数不允许像聚合函数那样重命名字段名称? 例如总的来说,我们可以传递以下字符串:

{ "$project" : { "OrderNumber" : "$PurchaseOrder.OrderNumber" , "ShipDate" : "$PurchaseOrder.ShipDate"}}

而 find 不允许这样做。

为什么聚合输出不作为 DBCursor 或 List 返回?还有为什么我们不能统计返回的文件?

谢谢。

最佳答案

Why does not the aggregate output return as a DBCursor or a List?

创建聚合框架是为了解决需要 map-reduce 的简单问题。

此框架通常用于计算需要完整 db 作为输入和少量文档作为输出的数据。

What is the difference between the $match operator used inside the aggregate function and the regular find in Mongodb?

正如您所说,其中一个区别是返回类型。查找操作输出返回为 DBCursor

其他区别:

  • 聚合结果必须为under 16MB .如果您使用 shards , 完整数据must be collected in a single point在第一个 $group 之后或 $sort .
  • $match 唯一的目的是提高聚合的能力,但它还有一些其他用途,比如提高聚合性能。

and also why can't we get a count of the documents that are returned?

你可以。只需计算结果数组中的元素数量或将以下命令添加到管道末尾:

{$group: {_id: null, count: {$sum: 1}}}

Why doesn't the find function allow renaming the field names like the aggregate function?

MongoDB 很年轻,功能还在不断涌现。也许在未来的版本中,我们将能够做到这一点。重命名字段在 aggregation 中比在 find 中更重要。

编辑(2014 年 2 月 26 日):

MongoDB 2.6 aggregation operations will return a cursor .

编辑(2014 年 4 月 9 日):

MongoDB 2.6 was released with the predicted aggregation changes .

https://stackoverflow.com/questions/16552433/

相关文章:

java - 编码对象时未使用 MongoDB BSON 编解码器

javascript - 如何重定向到另一个网页?

node.js - 模拟/测试 Mongodb 数据库 Node.js

javascript - var functionName = function() {} vs f

javascript - 如何从数组中删除特定项目?

mongodb - 使用 mongodb 或 cassandra 的空间数据

javascript - 如何检查元素是否隐藏在 jQuery 中?

mongodb - 使用 sphinx 搜索与 mongodb 作为数据源

javascript - 如何检查字符串是否包含 JavaScript 中的子字符串?

javascript - "use strict"在 JavaScript 中做了什么,背后的原因是