b¬äd†ødd

Dedicated to design and performance of databases and audio systems.

MongoDB: updating test data

Overview:

Working off the Data Insert blog, which inserted Winter Olympics data into MongoDB, we now update that set by adding the sport disciplines to each of the games.

With MongoDB we can add fields with updates. This differs from relational database tables where we would first have to alter the table to add the columns or create a related table for the new fields.

Since each of the games has many disciplines (e.g. Luge, Figure Skating), we will add them as an array to each document. Remember, our documents are at the games instance (e.g. 2014 in Sochi, Russia) grain. Therefore, each array can contain zero-to-many of the disciplines. As we know, every so often, additional disciplines are added; for example, Snowboarding in 1998. Others have been present, were removed, and have reappeared, like Curling.

One option to add updates:

// Alpine skiing
db.olympics.update(
{ year : { $gte: 1936, $lte: 2018 } },
{ $push: { discipline : "Alpine skiing" } },
{ multi : true }
)
 
// Biathlon
db.olympics.update(
{ year : { $gte: 1960, $lte: 2018 } },
{ $push: { discipline : "Biathlon" } },
{ multi : true }
)
 
// Bobsleigh (1924 - 1956)
db.olympics.update(
{ year : { $gte: 1924, $lte: 1956 } },
{ $push: { discipline : "Bobsleigh" } },
{ multi : true }
)
 
// Bobsleigh (1964 - 2014)
db.olympics.update(
{ year : { $gte: 1964, $lte: 2018 } },
{ $push: { discipline : "Bobsleigh" } },
{ multi : true }
)
 
// Cross-country skiing
db.olympics.update(
{ year : { $gte: 1924, $lte: 2018 } },
{ $push: { discipline : "Cross-country skiing" } },
{ multi : true }
)
 
// Curling (1924)
db.olympics.update(
{ year : 1924 },
{ $push: { discipline : "Curling" } }
)
 
// Curling (1998 - 2014)
db.olympics.update(
{ year : { $gte: 1998, $lte: 2018 } },
{ $push: { discipline : "Curling" } },
{ multi : true }
)
 
// Figure skating
db.olympics.update(
{ year : { $gte: 1924, $lte: 2018 } },
{ $push: { discipline : "Figure skating" } },
{ multi : true }
)
 
// Freestyle skiing
db.olympics.update(
{ year : { $gte: 1992, $lte: 2018 } },
{ $push: { discipline : "Freestyle skiing" } },
{ multi : true }
)
 
// Ice hockey
db.olympics.update(
{ year : { $gte: 1924, $lte: 2018 } },
{ $push: { discipline : "Ice hockey" } },
{ multi : true }
)
 
// Luge
db.olympics.update(
{ year : { $gte: 1964, $lte: 2018 } },
{ $push: { discipline : "Luge" } },
{ multi : true }
)
 
// Nordic combined
db.olympics.update(
{ year : { $gte: 1924, $lte: 2018 } },
{ $push: { discipline : "Nordic combined" } },
{ multi : true }
)
 
// Short track speed skating
db.olympics.update(
{ year : { $gte: 1992, $lte: 2018 } },
{ $push: { discipline : "Short track speed skating" } },
{ multi : true }
)
 
// Skeleton (1928 & 1948)
db.olympics.update(
{ year : { $in: [1928,1948] } },
{ $push: { discipline : "Skeleton" } },
{ multi : true }
)
 
// Skeleton (2002 - 2014)
db.olympics.update(
{ year : { $gte: 2002, $lte: 2018 } },
{ $push: { discipline : "Skeleton" } },
{ multi : true }
)
 
// Ski jumping
db.olympics.update(
{ year : { $gte: 1924, $lte: 2018 } },
{ $push: { discipline : "Ski jumping" } },
{ multi : true }
)
 
// Snowboarding
db.olympics.update(
{ year : { $gte: 1998, $lte: 2018 } },
{ $push: { discipline : "Snowboarding" } },
{ multi : true }
)
 
// Speed skating
db.olympics.update(
{ year : { $gte: 1924, $lte: 2018 } },
{ $push: { discipline : "Speed skating" } },
{ multi : true }
)
 
// clean up: 1940 & 1944
// remove the discipline array from the games which never occurred
db.olympics.update(
{ celebrated : false },
{ $unset : { discipline : "" } },
{ multi : true }
)

A few things to note. First, the $push function creates the array, if not already present, and adds its discipline to it. Second, you must specify true for multi in order for the update to affect more than one collection. Third, $unset deletes fields.

Next time, we'll add the aggregate framework to do some counts.

I hope this adds more context to your MongoDB understanding.

Cheers
-Brad