diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..dfe0770 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +# Auto detect text files and perform LF normalization +* text=auto diff --git a/9781484258163.jpg b/9781484258163.jpg new file mode 100644 index 0000000..e06438f Binary files /dev/null and b/9781484258163.jpg differ diff --git a/Contributing.md b/Contributing.md new file mode 100644 index 0000000..f6005ad --- /dev/null +++ b/Contributing.md @@ -0,0 +1,14 @@ +# Contributing to Apress Source Code + +Copyright for Apress source code belongs to the author(s). However, under fair use you are encouraged to fork and contribute minor corrections and updates for the benefit of the author(s) and other readers. + +## How to Contribute + +1. Make sure you have a GitHub account. +2. Fork the repository for the relevant book. +3. Create a new branch on which to make your change, e.g. +`git checkout -b my_code_contribution` +4. Commit your change. Include a commit message describing the correction. Please note that if your commit message is not clear, the correction will not be accepted. +5. Submit a pull request. + +Thank you for your contribution! \ No newline at end of file diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..d3a31c7 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,27 @@ +Freeware License, some rights reserved + +Copyright (c) 2020 Nicholas Cottrell + +Permission is hereby granted, free of charge, to anyone obtaining a copy +of this software and associated documentation files (the "Software"), +to work with the Software within the limits of freeware distribution and fair use. +This includes the rights to use, copy, and modify the Software for personal use. +Users are also allowed and encouraged to submit corrections and modifications +to the Software for the benefit of other users. + +It is not allowed to reuse, modify, or redistribute the Software for +commercial use in any way, or for a user’s educational materials such as books +or blog articles without prior permission from the copyright holder. + +The above copyright notice and this permission notice need to be included +in all copies or substantial portions of the software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + + diff --git a/README.md b/README.md new file mode 100644 index 0000000..54975d5 --- /dev/null +++ b/README.md @@ -0,0 +1,16 @@ +# Apress Source Code + +This repository accompanies [*MongoDB Topology Design*](https://www.apress.com/9781484258163) by Nicholas Cottrell (Apress, 2020). + +[comment]: #cover +![Cover image](9781484258163.jpg) + +Download the files as a zip using the green button, or clone the repository to your machine using Git. + +## Releases + +Release v1.0 corresponds to the code in the published book, without corrections or updates. + +## Contributions + +See the file Contributing.md for more information on how you can contribute to this repository. \ No newline at end of file diff --git a/ch01.js b/ch01.js new file mode 100644 index 0000000..fb020be --- /dev/null +++ b/ch01.js @@ -0,0 +1,59 @@ +use library; + +db.borrowers.insertOne({ + _id: "js1234", + name: "John", + surname: "Smith", + email: "john@smith.net", + state: "NY", + dob: Date("1978-05-12") +}); + +db.authors.insertOne({ + _id: 123, + name: "Homer", + books: [{ + id: ObjectId("5ee64b887077a4ee07e49e81"), + title: "The Oddysey", + cover_img: "the_oddysey.png" + }] +}); + + +db.books.insertOne({ + _id: ObjectId("5ee64b887077a4ee07e49e81"), + author: 123, + title: "The Oddysey", + cover_img: "the_oddysey.png", + available: 4, + checkout: [{ + by: "js1234", + date: "2019-10-14" + }, { + by: "nic", + date: "2019-09-12" + }], + publisher: "Penguin", + ISBN: "123-456789-012" +}); + +// Find a particular book, and check it out _only_ if there is one available +db.books.findAndModify({ + query: { + _id: ObjectId("5ee64b887077a4ee07e49e81"), + available: { + $gt: 0 + } + }, + update: { + $inc: { + available: -1 + }, + $push: { + checkout: { + by: "jill", + date: new Date() + } + } + } +}); diff --git a/ch04.js b/ch04.js new file mode 100644 index 0000000..f37f054 --- /dev/null +++ b/ch04.js @@ -0,0 +1,79 @@ +use test; + +// Insert one example document +db.msglog.insertOne( + { + from: "nic", to: "sophie", + createDate: ISODate("2019-11-04T20:55"), + msg: "What's for dessert?", + metadata: { + deviceId: "34-A4-8C-71", + deviceName: "iPhone XS", + ip: "123.45.67.89", + gps: { lat: 48.86, long: 2.349 } + }); + +// Create an index that will purge documents after 365 days +db.msglog.createIndex( + { "createDate": 1 }, + { expireAfterSeconds: 365*24*3600 } ) + +// Update a document and just remove the metadata which contains personal data +db.msglog.update( + { _id: ObjectId(1234...) }, + { $unset: {metadata: "" } ) + +// Insert a document with medical test data +db.participants.insertOne({ + "name" : "Jon Smith", + "ssn" : "213-43535-612", + "birthdate" : ISODate("1979-05-20T22:00:00Z"), + "weight" : NumberDecimal("81.6"), + "address": { + "street" : "123 High St", + "suburb": "Oxford", + "state": "OXF", + "country": "GB" , + "postcode": "OX1 4DF" + } +}); + +// Create a view which includes age (but not birthdate) for all adults in the collection +db.createView( + "participants_adults_nonpersonal", + "participants", + [ { $addFields: { + age: { + $divide: [{ + $subtract: [new Date(), "$birthdate"] + }, + 365.25 * 24 * 60 * 60 * 1000] + } + }}, + { $match: { age: { $gt: 18 }}}, + { $project: { + name: 0, ssn: 0, birthdate: 0, "address.street": 0, age: 0 + }} + ]); + +// Create a researcher role to access only adult data +use admin; +db.createRole({ + role : "researcher", + privileges : [ + { + resource : { + db : "app1", + collection : " participants_adults_nonpersonal" }, + actions : [ "find" ] + } + ], + roles : [] + }); + +// Create an account for a researcher +db.createUser({ + user : "nic", + pwd : "securepassword", + roles : ["researcher "] } +); diff --git a/ch05.js b/ch05.js new file mode 100644 index 0000000..7ce72f2 --- /dev/null +++ b/ch05.js @@ -0,0 +1,68 @@ +rs.reconfig({ + "_id" : "myRS", + … + "members" : [ + { "_id" : 0, "host" : "mongodb0.bigbank.local:27017", ..., + "tags": { "dc": "east-1", "use": "prod" }, ... }, + { "_id" : 1, "host" : "mongodb1.bigbank.local:27017", ..., + "tags": { "dc": "east-2", "use": "prod" }, ... }, + { "_id" : 2, "host" : "mongodb2.bigbank.local:27017", ..., + "tags": { "dc": "west", "use": "analytics" }, ... } + ], + … +} +) + + +conf = rs.conf(); +conf.settings = { + getLastErrorModes: { multi_dc : { "region": 2 } } +}; +rs.reconfig(conf); + +// Update a document with this custom write concern +db.collection.update( { _id: 123, status: "Important" }, + { writeConcern: { w: "multi_dc" } } ); + + +// Remove votes from one node +conf = rs.conf(); +/* +The `conf` variable will look similar to: +{ + "_id" : "rs0", + "version" : 1, + "protocolVersion" : NumberLong(1), + "members" : [ + { + "_id" : 0, + "host" : "mongodb0.example.local:27017", + "arbiterOnly" : false, + "hidden" : false, + "priority" : 1, + "votes" : 1 + }, + { + "_id" : 1, + "host" : "mongodb1.example.local:27017", + "arbiterOnly" : false, + "hidden" : false, + "priority" : 1, + "votes" : 1 + }, + { + "_id" : 2, + "host" : "mongodb2.example.local:27017", + "arbiterOnly" : true, + "hidden" : false, + "priority" : 0 + "votes" : 1 + } + ], + "settings" : { + … + } +} +*/ +conf. members[1].votes = 0; +rs.reconfig(conf); diff --git a/ch11.js b/ch11.js new file mode 100644 index 0000000..955c648 --- /dev/null +++ b/ch11.js @@ -0,0 +1,48 @@ +use test; + +db.players.insertMany([{ + _id: 123, + userName: "petesampras", + country: "US", + titles: 64 + }, + { + _id: 124, + userName: "bjornborg", + country: "SE", + titles: 63 + } +]); + +db.players.aggregate([ + { $match: { country: "SE" } }, + { $group: { + _id: "$country", + count: { $sum: "$titles" } + } + } +]); + +sh.updateZoneKeyRange("myDb.players", + { country: "SE", userName: MinKey }, + { country: "SE", userName: MaxKey }, "EUR") + +sh.addShardToZone("shardA", "EUR") + + +// See http://blog.rueckstiess.com/mtools/mlaunch.html +// mlaunch --replicaset --sharded shardA shardB shardC + +use crm; + +sh.splitAt("crm.contacts", {country: "US", acct: "a"}); +sh.splitAt("crm.contacts", {country: "US", acct: "l"}); +sh.splitAt("crm.contacts", {country: "US", acct: "r"}); +sh.splitAt("crm.contacts", {country: "US", acct: "zzzzzzzzz"}); + +sh.moveChunk("crm.contacts", + {country: "US", acct: "abc"}, "shardA") +sh.moveChunk("crm.contacts", + {country: "US", acct: "man"}, "shardB") +sh.moveChunk("crm.contacts", + {country: "US", acct: "sta"}, "shardC")