Consider a collection named orders with the following documents:
{
"_id" : ObjectId("5f0a7e80d8c9c7b5a48c49e1"),
"order_number" : 1001,
"customer_id" : "CUST-001",
"order_date" : ISODate("2022-01-01T00:00:00Z"),
"total_amount" : 199.99
},
{
"_id" : ObjectId("5f0a7e80d8c9c7b5a48c49e2"),
"order_number" : 1002,
"customer_id" : "CUST-002",
"order_date" : ISODate("2022-02-01T00:00:00Z"),
"total_amount" : 299.99
},
{
"_id" : ObjectId("5f0a7e80d8c9c7b5a48c49e3"),
"order_number" : 1003,
"customer_id" : "CUST-003",
"order_date" : ISODate("2022-03-01T00:00:00Z"),
"total_amount" : 399.99
}
What is the query to create a compound index on the "customer_id" (ascending) and "order_date" (descending) fields in the orders collection?
Answer: C
Question 2
We have a movies collection with the following document structure:
{
_id: ObjectId("573a1390f29313caabcd6223"),
genres: [ 'Comedy', 'Drama', 'Family' ],
title: 'The Poor Little Rich Girl',
released: ISODate("1917-03-05T00:00:00.000Z"),
year: 1917,
imdb: { rating: 6.9, votes: 884, id: 8443 }
},
{
_id: ObjectId("573a13e3f29313caabdc08a4"),
genres: [ 'Horror', 'Thriller' ],
title: 'Mary Loss of Soul',
year: 2014,
imdb: { rating: '', votes: '', id: 2904798 }
}
We need to use Aggregation Framework to calculate the following aggregates:
average imdb rating
minimum imdb rating
maximum imdb rating
Expected output:
[
{
_id: null,
avg_rating: 6.6934040649367255,
min_rating: 1.6,
max_rating: 9.6
}
]
Please note that some documents have "" (empty string) for the field "imdb.rating". Exclude these documents before aggregation.
Which pipeline should you use?
Answer: C
Question 3
What is the difference between a sparse index and a non-sparse index in MongoDB?
Answer: A
Question 4
Suppose you're designing a MongoDB database for an online ticket booking system where millions of users can concurrently book tickets for various events. To maintain data consistency, you need to ensure that only one user can book a specific seat for an event at a time. Which MongoDB feature would you use to ensure data consistency in this high-concurrency situation?
Answer: A
Question 5
Consider a MongoDB collection named orders which has documents in the following format:
{
"_id" : ObjectId("5f5b0d2f3e3dfbcc11c84444"),
"orderId" : 1001,
"customerId" : 200,
"items" : [
{
"productId" : 101,
"quantity" : 2
},
{
"productId" : 102,
"quantity" : 1
}
],
"totalAmount" : 250,
"status" : "Pending"
}
What is the correct syntax to find and update the first document in the orders collection where "status" is "Pending", and change the "status" field to "Completed" using the findAndModify method?