M220P – MongoDB for Python Developers
Let us go through MongoDB University | M220P-MongoDB for Python Developers Quiz Answer | Introduction and Setup | Chapter 0 – Introduction and Setup first. And then we will cover rest of questions and answers in this article.
MFlix Application Architecture
During this course, which of the following files in mflix will you have to edit?
- db.py
- factory.py
- api/movies.py
- api/user.py
Basic ReadsWhat do the methods find() and find_one() have in common?
- They return a cursor.
- They accept a query predicate.
- They accept a field projection.
- They are used to query documents in a collection.
Quiz
Cursor Methods and Aggregation EquivalentsWhich of the following aggregation stages have equivalent cursor methods?- $skip
- $out
- $limit
- $sort
- $sortByCount
Basic WritesWhich of the following is true about InsertOneResult?- It is a cursor.
- It contains the _id of an inserted document.
- It contains a copy of the inserted document.
- It can tell us whether the operation was acknowledged by the server.
Write ConcernsWhich of the following Write Concerns are valid in a 3-node replica set?- w: 0
- w: 1
- w: 4
- w: 5
- w: majority
Basic UpdatesWhich of the following are valid update operators in Pymongo?- $push
- $update
- $inc
- $remove
- $set
Basic JoinsWhy did we use a let expression with expressive $lookup, when joining the comments and the movies collection?- To use fields from the comments documents in the pipeline.
- To count the number of comments documents.
- To store the output of the pipeline in the movie_comments field.
- To use fields from the movies documents in the pipeline.
Basic DeletesWhich of the following is true about deleting documents in Pymongo?- delete_one() can only delete one document.
- delete_many() can delete any number of documents.
- Pymongo can only delete one document at a time.
- DeleteResult objects contain the number of deleted documents.
- delete_one() will not return a DeleteResult object.
Quiz
Read ConcernsWhich of the following Read Concerns are valid in a 3-node replica set?- 1
- “majority”
- 0
- “local”
- “nearest”
Bulk WritesWhich of the following is true about bulk writes?- By default, bulk writes are ordered.
- Bulk writes decrease the effect of latency on overall operation time.
- The server will send one acknowledgement for each write in a bulk operation.
- f a failure occurs during an ordered bulk write, the server will continue executing the rest of the batch.
Quiz
Connection PoolingWhich of the following are benefits of connection pooling?- A large influx of operations can be handled more quickly with a pool of existing connections.
- Multiple database clients can share a connection pool.
- The connection pool will persist after the client is terminated.
- New operations can be serviced with pre-existing connections, so a new connection doesn’t have to be created each time.
Robust Client ConfigurationWhen should you set a wtimeout?- When our application is issuing bulk operations in large batches.
- When our application is using a Write Concern more durable than w: 1.
- When our application is using a connection pool of 100 or more connections.
- When our application is using a Read Concern more durable than “available”.
Change StreamsWhat of the following is true about Change Streams in Pymongo?- They can stay open for up to 10 minutes.
- They can be used to log changes to a MongoDB collection.
- They output cursors, which contain change event documents.
- They will not log changes associated with insert operations.
- They accept pipelines, which can be used to filter output from the change stream.
Question 1)Assume a collection called elections that holds data about all United States Presidential Elections since 1789. All the documents in the elections collection look like this:{“year” : 1828,“winner” : “Andrew Jackson”,“winner_running_mate” : “John C. Calhoun”,“winner_party” : “Democratic”,“winner_electoral_votes” : 178,“total_electoral_votes” : 261}total_electoral_votes represents the total number of electoral votes that year, and winner_electoral_votes represents the number of electoral votes received by the winning candidates.Which of the following queries will retrieve all the Republican winners with at least 160 electoral votes?db.elections.find( { “winner_party”: “Republican”,“winner_electoral_votes”: { “$lt”: 160 } } )db.elections.find( { “winner_party”: “Republican”,“total_electoral_votes”: { “$lte”: 160 } } )db.elections.find( { “winner_party”: “Republican”,
“winner_electoral_votes”: { “$gte”: 160 } } )db.elections.find( { “total_electoral_votes”: { “$gte”: 160 },“winner_party”: “Republican” } )– db.elections.find( { “winner_electoral_votes”: { “$gte”: 160 } } )Question 2)Consider a collection of phones called phones, used by a phone manufacturer to keep track of the phones currently in production.Each document in phones looks like this:{“model”: 5,“date_issued” : ISODate(“2016-07-27T20:27:52.834Z”),“software_version”: 4.8,“needs_to_update”: false}There is an update required for phones with software_version earlier than 4.0. Anyone still using a version older than 4.0 will be asked to update.The phone manufacturer wants to set the flag needs_to_update to true when the value of software_version is lower than 4.0. For example, a document like this one:{“model”: 5,“date_issued” : ISODate(“2014-03-04T14:23:43.123Z”),“software_version”: 3.7,“needs_to_update”: false}Should be updated to look like this:{“model”: 5,“date_issued” : ISODate(“2014-03-04T14:23:43.123Z”),“software_version”: 3.7,“needs_to_update”: true}Which of the following update statements will correctly perform this update?db.phones.update_many( { “software_version”: { “$lte”: 4.0 } },{
“$set”: { “needs_to_update”: True } } )db.phones.update_one( { “software_version”: { “$lt”: 4.0 } },{ “$set”:
{ “needs_to_update”: True } } )db.phones.update_many( { “software_version”: { “$lt”: 4.0 } },
{ “$set”: { “needs_to_update”: True } } )db.phones.update_many( { “software_version”: { “$gt”: 4.0 } },{
“$set”: { “needs_to_update”: True } } )db.phones.update_many( { “software_version”: { “$lt”: 4.0 } },{
“$inc”: { “needs_to_update”: True } } )Question 3)Suppose an instance of MongoClient is created with the following URI string:from pymongo import MongoClienturi =
“mongodb+srv://m220-user:[email protected]/test”mc = MongoClient(uri, authSource=”admin”, retryWrites=True,
connectTimeoutMS=50)The variable representing our client, mc, will:- automatically retry writes that fail.
- use SSL when connecting to MongoDB.
- authenticate against the test database.
- allow a maximum of 50 connections in the connection pool.
- wait at most 50 milliseconds for timing out a connection.
Question 4)Suppose a client application is sending writes to a replica set with
3 nodes:Before returning an acknowledgement back to the client, the replica
set waits.When the write has been applied by the nodes marked in stripes, it
returns an acknowledgement back to the client.What Write Concern was used in this operation?- w: 0
- w: 1
- w: available
- w: majority
Question 5)Given the following bulk write statement, to a collection called
employees:requests = [InsertOne({ ‘_id’: 11, ‘name’: ‘Edgar Martinez’, ‘salary’:
“8.5M” }), # Insert #1InsertOne({ ‘_id’: 3, ‘name’: ‘Alex Rodriguez’, ‘salary’:
“18.3M” }), # Insert #2InsertOne({ ‘_id’: 24, ‘name’: ‘Ken Griffey Jr.’, ‘salary’:
“12.4M” }), # Insert #3InsertOne({ ‘_id’: 11, ‘name’: ‘David Bell’, ‘salary’: “2.5M”
}), # Insert #4InsertOne({ ‘_id’: 19, ‘name’: ‘Jay Buhner’, ‘salary’: “5.1M”
}) # Insert #5]response = employees.bulk_write(requests)Assume the employees collection is empty, and that there were no
network errors in the execution of the bulk write.Which of the insert operations in requests will succeed?- Insert #1
- Insert #2
- Insert #3
- Insert #4
- Insert #5
Question 6)Suppose a client application is sending writes to a replica set with
three nodes, but the primary node stops responding:Assume that none of the connection settings have been changed, and
that the client is only sending insert statements with write concern
w: 1 to the server.After 30 seconds, the client still cannot connect to a new primary.
Which of the following exceptions will be raised by Pymongo?- DocumentTooLarge
- ConfigurationError
- InvalidURI
- DuplicateKeyError
- ServerSelectionTimeoutError
Question 7)Assume a collection called people_heights with documents that look
like this:{“name”: “Ada”,“height”: 1.7}Which of the following queries will find only the 4th- and
5th-tallest people in the people_heights collection?-
- db.people_heights.find().sort(“height”, -1).skip(5).limit(3)
- db.people_heights.find().sort(“height”, -1).limit(5).skip(3)
- db.people_heights.find().sort(“height”,
-1).skip(3).limit(2) - db.people_heights.find().sort(“height”, -1).skip(3).limit(5)