You are on page 1of 10

Building Cross Platform Mobile and

Web Apps for Engineers and Scientists


An Active Learning Approach 1st
Edition Lingras Solutions Manual
Visit to download the full and correct content document: https://testbankdeal.com/dow
nload/building-cross-platform-mobile-and-web-apps-for-engineers-and-scientists-an-a
ctive-learning-approach-1st-edition-lingras-solutions-manual/
Chapter 9

Self-test exercises

ST1. Name three database models.


Answer: Hierarchical, network, relational, NoSQL.

ST2. Who introduced the relational database model?


Answer: E.F. Codd.

ST3. What is MongoDB?


Answer: An implementation of a NoSQL database.

ST4. What is the command used to view existing collections in MongoDB?


Answer: show collections

ST5. What is the command used to retrieve records in MongoDB?


Answer: find()

ST6. What is the command used to add records in MongoDB?


Answer: insert()

ST7. What is the command used to delete records in MongoDB?


Answer: remove()

9-1
© 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Programming exercises

PE1. Create the MongoDB collection on your MongoDB server as shown in the
text and add the library collection.

Answer:

// See section 9.3 - Modeling a NoSQL database


db.library.insert({ author: "Pawan" });
db.library.update({ author: "Pawan" },
{ author: "Pawan",
books:
[
{ title: "Web mining", year: 2007 },
{ title: "Web programming", year: 2012 }
]
});
db.library.update({ author: "Pawan" },
{
$push: {
books: { title: "Mobile app development", year:
2015 }
}
});
db.library.insert({
author : "Walter",
books :
[
{ title : "Intro programming", year : 2000 },
{ title : "Data structures", year : 2002 }
]
});

PE2. Write a JavaScript function to add the following books in the library
collection for author John Grisham. The number in the parentheses is the
year of publication.

A Time to Kill (1989)


The Firm (1991)
The Pelican Brief (1992)
The Client (1993)
The Chamber (1994)
The Rainmaker (1995)
The Runaway Jury (1996)
The Partner (1997)
The Street Lawyer (1998)
The Testament (1999)

9-2
© 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
The Brethren (2000)
A Painted House (2001)
Skipping Christmas (2001)
The Summons (2002)
The King of Torts (2003)
Bleachers† (2003)
The Last Juror (2004)
The Broker (2005)
Playing for Pizza (2007)
The Appeal (2008)
The Associate (2009)
The Confession (2010)
The Litigators (2011)
Calico Joe (2012)
The Racketeer (2012)
Sycamore Row (2013)
Gray Mountain (2014)

Answer:

In file: PE2answer.js

var mongodb = require('mongodb');

var host = '127.0.0.1';


var port = '27017'; // Default MongoDB port
var database = 'mobileappbook';
// var user = 'username';
// var password = 'password';
// var connectionString = 'mongodb://' + user + ':' + password + '@' +
host + ':' + port + '/' + database;
var connectionString = 'mongodb://' + host + ':' + port + '/' +
database;

var johnGrisham = {
author: "John Grisham",
books: [
{ title: "A Time to Kill", year: 1989 },
{ title: "The Firm", year: 1991 },
{ title: "The Pelican Brief", year: 1992 },
{ title: "The Client", year: 1993 },
{ title: "The Chamber", year: 1994 },
{ title: "The Rainmaker", year: 1995 },
{ title: "The Runaway Jury", year: 1996 },
{ title: "The Partner", year: 1997 },
{ title: "The Street Lawyer", year: 1998 },
{ title: "The Testament", year: 1999 },

9-3
© 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
{ title: "The Brethren", year: 2000 },
{ title: "A Painted House", year: 2001 },
{ title: "Skipping Christmas", year: 2001 },
{ title: "The Summons", year: 2002 },
{ title: "The King of Torts", year: 2003 },
{ title: "Bleachers†", year: 2003 },
{ title: "The Last Juror", year: 2004 },
{ title: "The Broker", year: 2005 },
{ title: "Playing for Pizza", year: 2007 },
{ title: "The Appeal", year: 2008 },
{ title: "The Associate", year: 2009 },
{ title: "The Confession", year: 2010 },
{ title: "The Litigators", year: 2011 },
{ title: "Calico Joe", year: 2012 },
{ title: "The Racketeer", year: 2012 },
{ title: "Sycamore Row", year: 2013 },
{ title: "Gray Mountain", year: 2014 }
]
};

mongodb.connect(connectionString, function (error, db) {


if (error) {
throw error;
}

var libraryCollection = db.collection('library');


libraryCollection.insert(johnGrisham, function(err, result) {
if (err) {
console.log(err);
} else {
console.log(result);
console.log("Successfully inserted John Grisham");
}
process.exit();
});
});

PE3. Write queries to find the following:


• All the books published before 2000 (by any author)
• All the books published by John Grisham
• All the books not published by John Grisham
• All the books published between years 2000 and 2009
• The number of books published after 2009

9-4
© 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Answer:

// Note, some of these questions require the students to refer to


www.mongodb.org
// to create the more sophisticated queries

// All the books published before 2000 (by any author)


db.library.aggregate(
{ $unwind: "$books" },
{ $match: { "books.year": { $lt: 2000 } }}
);

// All the books published by John Grisham


db.library.find({ author: "John Grisham" }, { books: 1 })

// All the books not published by John Grisham


db.library.find({ author: { $ne: "John Grisham" }}, { books: 1 })

// All the books published between years 2000 and 2009


db.library.aggregate(
{ $unwind: "$books" },
{ $match: { "books.year": { $gte: 2000, $lte: 2009 } }}
);

// The number of books published after 2009


db.library.aggregate(
{ $unwind: "$books" },
{ $match: { "books.year": { $gt: 2009 } }},
{ $group: { _id: null, count: { $sum: 1 }}}
);

PE4. Write queries to delete the following:


• All the books published before 2000 (by any author)
• All the books published by John Grisham
• All the books not published by John Grisham
• All the books published between years 2000 and 2009 (by any author)

Answer:
// Note, some of these questions require the students to refer to
www.mongodb.org
// to create the more sophisticated queries

// All the books published before 2000 (by any author)


db.library.update(
{},
{ $pull: { books: { year: { $lt: 2000 }}}},
{ multi: true }
);

9-5
© 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
// All the books published by John Grisham
db.library.update(
{ author: "John Grisham" },
{ author: "John Grisham", books: [] }
);

// All the books not published by John Grisham


db.library.update(
{ author: { $ne: "John Grisham" }},
{ $set: { books: [] }},
{ multi: true }
);

// All the books published between years 2000 and 2009 (by any
author)
db.library.update(
{},
{ $pull: { books: { year: { $gte: 2000, $lte: 2009 }}}},
{ multi: true }
);

9-6
© 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Programming projects

PRJ1. Boiler monitor server-side version: We want to change the app that
monitors the temperature and pressure of a boiler so that the data is
stored on a MongoDB server instead of in localStorage. You can model
the app based on the server-side Thyroid app. We have modified the
webpages in the previous chapter. In this chapter, you need to design the
MongoDB database so that the records can be stored and retrieved using
the boiler ID as the key, and to create the routes in your server for the
app to access.

PRJ2. Blood pressure monitor server-side version: We want to change the


app that monitors blood pressure so that the data is stored on a
MongoDB server instead of in localStorage. You can model the app
based on the server-side Thyroid app. We have modified the webpages in
the previous chapter. In this chapter, you need to design the MongoDB
database so that the records can be stored and retrieved using the
person’s ID as the key, and to create the routes in your server for the app
to access.

PRJ3. Power consumption monitor server-side version: We want to change


the app that monitors the power consumption of a manufacturing plant so
that the data is stored on a MongoDB server instead of in localStorage.
You can model the app based on the server-side Thyroid app. We have
modified the webpages in the previous chapter. In this chapter, you need
to design the MongoDB database so that the records can be stored and
retrieved using the plant ID as the key, and to create the routes in your
server for the app to access.

PRJ4. Body mass index (BMI) monitor server-side version: We want to


change the app that monitors the body mass index of a person based on
height and weight so that the data is stored on a MongoDB server instead
of in localStorage. You can model the app based on the server-side
Thyroid app. We have modified the webpages in the previous chapter. In
this chapter, you need to design the MongoDB database so that the
records can be stored and retrieved using the person’s ID as the key, and
to create the routes in your server for the app to access.

9-7
© 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
PRJ5. Managing a line of credit server-side version: We want to change the
app that manages our line of credit at a bank so that the data is stored on
a server instead of in localStorage. You can model the app based on the
server-side Thyroid app. We have modified the webpages in the previous
chapter. In this chapter, you need to design the MongoDB database so
that the records can be stored and retrieved using the organization’s ID
as the key, and to create the routes in your server for the app to access.

9-8
© 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.

You might also like