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?
Answer: B
Question 2
Which of the following stages do you need to use to randomly select 10 documents from a specific collection?
Answer: D
Question 3
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 4
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 5
In MongoDB, which statement accurately describes a typical task for a MongoDB developer related to schema validation?