Overview
In this unit, you’ll learn how to modify query results in MongoDB by using sorts, limits, projections, and counts. First, you will learn how to organize query results by sorting and limiting the documents that are returned. Then you'll explore how to use projection to return selected fields from a query. Finally, you’ll learn how to count the number of documents that match a query. Using these query modifications will help enhance the functionality and performance of your applications.
LESSON 1: SORTING AND LIMITING QUERY RESULTS IN MONGODB
Using the
inspections
collection within thesample_training
database, you need to find all inspections that were passed. Your manager has requested that you organize this data by the certificate number in ascending order. Which query should you use? (Select one).
A.db.inspections.find( { result : "Pass" }).sort( {certificate_number: 1});
B.
db.inspections.find( { result : "Pass" }).sort( {certificate_number: - 1});
C.
db.inspections.find( { result : "Pass" }, { sort: { certificate_number: 1 }})
D.
db.inspections.find( { result : "Pass" }, { sort: { certificate_number: - 1 }})
A. Option A
B. Option B
C. Option C
D. Option D
You are considering creating a new membership tier for your bike sharing service for users who take long trips. Using the
trips
collection within thesample-training
database, you need to find the trips, taken by subscribers, with the longest trip duration. Return the top 5 results in descending order. Which query should you use? (Select one.)A.
db.trips.find( { usertype: "Subscriber"},( sort: { tripduration: - 1 }), { limit :5 })
B.
db.trips.find( { usertype: "Subscriber"},( sort: { tripduration: 1 }), { limit :5 })
C.
db.trips.find( { usertype: "Subscriber"}).sort( { tripduration: 1 }).limit(5)
D.
db.trips.find( { usertype: "Subscriber"}).sort( { tripduration: - 1 }).limit(5)
A. Option A
B. Option B
C. Option C
D. Option D
LESSON 2: RETURNING SPECIFIC DATA FROM A QUERY IN MONGODB
Which of the following statements are true about a projection document? (Select all that apply.)
A. We can include fields in our results by setting their values to 1 in the projection document.
B. We can exclude fields from our results by setting their values to 0 in the projection document.
C. We can either include or exclude fields in the results, but not both. The _id field is the exception to this rule.
D. Inclusion and exclusion statements, not including _id statements, can be combined with each other in a projection document.
If we don’t want to return the
_id
field, we can add it to the projection document and set it to which of the following values? (Select all that apply.)A. 0
B. 1
C. -1
D. None of the above
LESSON 3: COUNTING DOCUMENTS IN A MONGODB COLLECTION
Which of the following statements are true about the
countDocuments()
collection method? (Select all that apply.)A. The method takes a query parameter, which selects the documents to be counted.
B. We can use the method to count all documents in a collection.
C. The method does not support the use of operators in queries that are passed as a parameter.
What can we expect to be returned by running
db.inspections.countDocuments({})
? (Select one.)A. This command doesn't return anything because it requires a query parameter.
B. This command returns the total number of documents in the inspections database.
C. This command returns the total number of documents in the inspections collection.