对比项 | MapReduce | 管道聚合 |
---|---|---|
查询速度 | 非常慢 不适合实时查询 | 比较快适合实时查询 |
语法 | js查询语法 | 分批执行 |
特点 | 适合大规模数据 | 占用的内存超过20%自动报错 |
简单示例
1:MapReduce
>db.collection.mapReduce(
function() {emit(key,value);}, //map 函数
function(key,values) {return reduceFunction}, //reduce 函数
{
out: collection,
query: document,
sort: document,
limit: number,
finalize: <function>,
scope: <document>,
jsMode: <boolean>,
verbose: <boolean>
}
)
2:管道
db.orders.aggregate([
{
$unwind: "$specs"
},
{
$lookup:
{
from: "inventory",
localField: "specs",
foreignField: "size",
as: "inventory_docs"
}
},
{
$match: { "inventory_docs": { $ne: [] } }
}
])