-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jill Balzano
authored and
Jill Balzano
committed
Sep 28, 2020
0 parents
commit 7e43501
Showing
9 changed files
with
313 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Auto detect text files and perform LF normalization | ||
* text=auto |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
} | ||
} | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 "] } | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") |