G’Day, Folks!
Let’s revisit MongoDB Basics and practice some questions along with it! Most of the concepts covered are targeted at beginner audiences and learners wanting to practice more queries. I have added syntax and documentation links too if you need any help.
Please feel free to comment if you face any issues or doubts when trying the queries. I’ll post detailed answers to the queries next week!
For the practice questions, we will be using the listingsAndReviews
collection from the sample_airbnb
database. You can use the same Atlas Cluster that you set up while doing the university courses😄 If you haven’t loaded the sample data yet, fear not! You can do so by following the steps: Load Sample Data in Atlas
The sample_airbnb.listingsAndReviews
collection contains documents that represent the vacation home listing details and reviews of customers about the listing. These documents contain fields like name, property name, reviews, room type, bed type and other details related to Airbnb. You can read more about the collection as well as see a sample document from the documentation.
-
Firstly, to get all documents in a collection and see, we use the following command:
db.collections.find({})
The limit() function in MongoDB is used to specify the maximum number of results to be returned. Only one parameter is required for this function to return the number of the desired result.
To output a single document in our
listingsAndReviews
collection, we use the following command :use sample_airbnb db.listingsAndReviews.find({}).limit(1)
-
Using .count():
db.collections.find(<query>).count()
returns the count of documents that would match the find() query for the collection or view. You can read more examples from the Documentation.
Now, let’s see if you can answer a few questions related to this concept.
listingsAndReviews
collection have the number of reviews as 3?- 250
- 300
- 247
- None of the above
0 voters
listingAndReviews
collection have a moderate
cancellation policy?- 1336
- 1446
- 1550
- None of the above
0 voters
moderate
cancellation policy with 2 bedrooms?- 550
- 273
- 1000
- 481
0 voters
-
Finding distinct values in an array: We can use the following syntax to get all the distinct values:
This finds the distinct values for a specified field across a single collection or view and returns the results in an array.db.collection.distinct(field, query, options)
For eg., if we want to see the different kinds of room types we have in our collection, we use the following command:
This returns the following result, showing the distinct room types we have in our collection:db.listingsAndReviews.distinct("room_type")
Adding> db.listingsAndReviews.distinct("room_type") [ 'Entire home/apt', 'Private room', 'Shared room' ]
.length
to the end of the query helps get the count of all the distinct values.>db.listingsAndReviews.distinct("room_type").length 3
- 30
- 36
- 46
- 56
0 voters
Let’s practice some more questions.
- 80
- 81
- 82
- 83
0 voters
- Le Bed & Cocktail: Discover Montreal like a local
- Be Happy in Porto
- 3 chambres au coeur du Plateau
- None of the above
0 voters
- 5303
- 5000
- 6023
- None of the above
0 voters
- Ribeira Charming Duplex
- Wyndham Kona Hawaiian Resort - Two Bedroom Condo WVR
- Private Room in Bushwick
- Marriott Waiohai Beach Club 2Bd Villa 2020
0 voters
- Couch
- Real Bed
- Airbed
- Bunk Bed
0 voters
Do try these and as already said, please feel to post any doubts or queries you face while trying the questions! Also. we’ll post the solutions when the participation in the quiz crosses 30 folks, so keep an eye out for the solutions too!
Regards,
Satyam