MongoDB 数据库操作(五)—MapReduce(groupBy)
1. MongoDB 的MapReduce 相当于Mysql 中的group by,所以在MongoDB 上使用 Map/Reduce进行并行统计很容易。使用MapReduce 要实现两个函数 Map 函数和Reduce 函数,Map 函数调用emit(key, value),遍历collection中所有的记录,将key与value传递给Reduce 函数
1. MongoDB 的MapReduce 相当于Mysql 中的"group by",所以在MongoDB 上使用 Map/Reduce进行并行"统计"很容易。使用MapReduce 要实现两个函数 Map 函数和Reduce 函数,Map 函数调用emit(key, value),遍历collection中所有的记录,将key与value传递给Reduce 函数进行处理。Map函数和Reduce函数可以使用JavaScript 来实现,可以通过db.runCommand 或mapReduce 命令来执行一个MapReduce 的操作。
2.运行MapReduce程序(runCommand)
1 2 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
mapreduce: 要操作的目标集合。
map: 映射函数 (生成键值对序列,作为 reduce 函数参数)。
reduce: 统计函数。
query: 目标记录过滤。
sort: 目标记录排序。
limit: 限制目标记录数量。
out: 统计结果存放集合 (不指定则使用临时集合,在客户端断开后自动删除)。
keeptemp: 是否保留临时集合。
finalize: 最终处理函数 (对 reduce 返回结果进行最终整理后存入结果集合)。
scope: 向 map、reduce、finalize 导入外部变量。
verbose: 显示详细的时间统计信息。
3.Map
测试数据:
1 2 3 4 5 6 7 8 |
|
map函数:必须调用 emit(key, value) 返回键值对,使用 this 访问当前待处理的 Document。通过你提供的key值来进行groupby操作。下面例子就是通过classid来进行分组。另外,value 可以使用 JSON Object 传递 (支持多个属性值)。例如:emit(this.classid, {count:1})
1 |
|
4.Reduce
Reduce 函数接收的参数类似 Group 效果,将 Map 返回的键值序列组合成 { key, [value1,value2, value3, value...] } 传递给 reduce。Reduce 函数对这些 values 进行 "统计" 操作,返回结果可以使用 JSON Object
1 2 3 4 5 |
|
1 2 3 4 5 6 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
6.进一步处理结果
利用 finalize() 我们可以对 reduce() 的结果做进一步处理。函数的输入是分类的key和统计之后的结果值。
1 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
7.过滤,排序选项等,具体的过滤选项在上面已经介绍过了。
例如根据年龄过滤:
1 2 3 4 5 6 7 8 |
|

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Node.js is a server-side JavaScript runtime, while Vue.js is a client-side JavaScript framework for creating interactive user interfaces. Node.js is used for server-side development, such as back-end service API development and data processing, while Vue.js is used for client-side development, such as single-page applications and responsive user interfaces.

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

Solutions to resolve Navicat expiration issues include: renew the license; uninstall and reinstall; disable automatic updates; use Navicat Premium Essentials free version; contact Navicat customer support.

Node.js can be used for both front-end (handling user interface and interactions) and back-end (managing logic and data). The front-end uses HTML, CSS, and JavaScript frameworks, while the front-end uses Node.js framework, database, and cloud services. The focus is different (the front-end focuses on experience, the back-end focuses on functionality), the running environment is different (the front-end is in the browser, the back-end is on the server), and the tools are different (the front-end and back-end use different code compilation and packaging tool sets), although both use JavaScript , but with access to different APIs and libraries.

For front-end developers, the difficulty of learning Node.js depends on their JavaScript foundation, server-side programming experience, command line familiarity, and learning style. The learning curve includes entry-level and advanced-level modules focusing on fundamental concepts, server-side architecture, database integration, and asynchronous programming. Overall, learning Node.js is not difficult for developers who have a solid foundation in JavaScript and are willing to invest the time and effort, but for those who lack relevant experience, there may be certain challenges to overcome.

Table of Contents Astar Dapp Staking Principle Staking Revenue Dismantling of Potential Airdrop Projects: AlgemNeurolancheHealthreeAstar Degens DAOVeryLongSwap Staking Strategy & Operation "AstarDapp Staking" has been upgraded to the V3 version at the beginning of this year, and many adjustments have been made to the staking revenue rules. At present, the first staking cycle has ended, and the "voting" sub-cycle of the second staking cycle has just begun. To obtain the "extra reward" benefits, you need to grasp this critical stage (expected to last until June 26, with less than 5 days remaining). I will break down the Astar staking income in detail,

How to integrate GoWebSocket with a database: Set up a database connection: Use the database/sql package to connect to the database. Store WebSocket messages to the database: Use the INSERT statement to insert the message into the database. Retrieve WebSocket messages from the database: Use the SELECT statement to retrieve messages from the database.
