Seven Databases in Seven Weeks: A Guide to Modern Databases and the NoSQL Movement by Eric Redmond; Jim Wilson

Seven Databases in Seven Weeks: A Guide to Modern Databases and the NoSQL Movement by Eric Redmond; Jim Wilson

Author:Eric Redmond; Jim Wilson
Language: eng
Format: mobi
Tags: Pragmatic Bookshelf, Database Management, Data Modeling & Design, Computers, General, Software Development & Engineering, Databases
ISBN: 9781934356920
Publisher: Oreilly & Associates Incorporated
Published: 2012-06-05T18:30:00+00:00


​​db.phones.group({​​

​​ initial: { prefixes : {} },​​

​​ reduce: function(phone, output) {​​

​​ output.prefixes[phone.components.prefix] = 1;​​

​​ },​​

​​ finalize: function(out) {​​

​​ var ary = [];​​

​​ for(var p in out.prefixes) { ary.push( parseInt( p ) ); }​​

​​ out.prefixes = ary;​​

​​ }​​

​​})[0].prefixes​​

​​[ 555, 556, 557, 558, 559, 560, 561, 562, 563, 564 ]​​

The group function is powerful—like SQL’s GROUP BY—but Mongo’s implementation has a downside, too. First, you are limited to a result of 10,000 documents. Moreover, if you shard your Mongo collection (which we will tomorrow) group won’t work. There are also much more flexible ways of crafting queries. For these and other reasons, we’ll dive into MongoDB’s version of mapreduce in just a bit. But first, we’ll touch on the boundary between client-side and server-side commands, which is a distinction that has important consequences for your applications.

Server-Side Commands

If you were to run the following function through a command line (or through a driver), the client will pull each phone locally, all 100,000 of them, and save each phone document one by one to the server.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.