MongoDB中汇总($ match)和查找之间的区别? [英] difference between aggregate ($match) and find, in MongoDB?
问题描述
在聚合函数中使用的$match
运算符与Mongodb中的常规find
有什么区别?
What is the difference between the $match
operator used inside the aggregate function and the regular find
in Mongodb?
为什么find
函数不允许重命名字段名称(如聚合函数)?
例如总之,我们可以传递以下字符串:
Why doesn't the find
function allow renaming the field names like the aggregate function?
e.g. In aggregate we can pass the following string:
{ "$project" : { "OrderNumber" : "$PurchaseOrder.OrderNumber" , "ShipDate" : "$PurchaseOrder.ShipDate"}}
但是,find不允许这样做.
Whereas, find does not allow this.
为什么汇总输出不以DBCursor或List的形式返回?以及为什么我们无法计数返回的文档?
Why does not the aggregate output return as a DBCursor or a List? and also why can't we get a count of the documents that are returned?
谢谢.
推荐答案
为什么聚合输出不以DBCursor或列表的形式返回?
Why does not the aggregate output return as a DBCursor or a List?
创建聚合框架是为了解决简单的问题,否则这些问题将需要map-reduce.
The aggregation framework was created to solve easy problems that otherwise would require map-reduce.
该框架通常用于计算需要完整的db作为输入而很少的文档作为输出的数据.
This framework is commonly used to compute data that requires the full db as input and few document as output.
在聚合函数中使用的$ match运算符与Mongodb中的常规查找之间有什么区别?
What is the difference between the $match operator used inside the aggregate function and the regular find in Mongodb?
与您所说的不同的是返回类型.查找操作输出返回为 DBCursor .
One of differences, like you stated, is the return type. Find operations output return as a DBCursor.
其他区别:
- 聚合结果必须小于16MB .如果您使用的是碎片,则完整数据为. mongodb.org/manual/reference/aggregation/group/#stage._S_group"rel =" noreferrer> $ group 或
- Aggregation result must be under 16MB. If you are using shards, the full data must be collected in a single point after the first $group or $sort.
- $match only purpose is to improve aggregation's power, but it has some other uses, like improve the aggregation performance.
还有为什么我们不能计数返回的文档?
and also why can't we get a count of the documents that are returned?
可以.只需计算结果数组中的元素数,或将以下命令添加到管道的末尾即可:
You can. Just count the number of elements in the resulting array or add the following command to the end of the pipe:
{$group: {_id: null, count: {$sum: 1}}}
为什么find函数不能像重命名函数那样重命名字段名称?
Why doesn't the find function allow renaming the field names like the aggregate function?
MongoDB还很年轻,功能仍在继续.也许在将来的版本中,我们将能够做到这一点.在 aggregation 中,重命名字段比在 find 中更重要.
MongoDB is young and features are still coming. Maybe in a future version we'll be able to do that. Renaming fields is more critical in aggregation than in find.
这篇关于MongoDB中汇总($ match)和查找之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!