I want to have the mongodb query given these details below, and to have best in performance.
Let me start by an example:
Let me have a collection,
/// the collection
services =
[
{
_id: obj(id1),
provider_id: "provider1",
service_code: "code1",
price: 20
},
{
_id: obj(id2),
provider_id: "provider1",
service_code: "code2",
price: 20
},
{
_id: obj(id3),
provider_id: "provider1",
service_code: "code3",
price: 20
},
{
_id: obj(id4),
provider_id: "provider2",
service_code: "code1",
price: 10
},
{
_id: obj(id5),
provider_id: "provider2",
service_code: "code2",
price: 20
},
{
_id: obj(id6),
provider_id: "provider3",
service_code: "code1",
price: 20
}
]
I want to get all service providers that (only) support these both service codes : code1, code2.
And the total price sorted by total price.
The output is like this:
/// required output based the posted collection and required requirements.
output =
[
{
provider_id: "provider1",
total_price: 40,
},
{
provider_id: "provider2",
total_price: 30,
}
]
I don’t want this output:
[
{
"_id": "provider1",
"total_price": 40
},
{
"_id": "provider2",
"total_price": 30
},
{
"_id": "provider3",
"total_price": 20
}
]
How to accomplish this? Thanks
3
Answers
This is probably the most basic use case of
$group
.Here is the Mongo playground for your reference.
Mongo Playground
This is not like your desired output but it only returns the data which matches with provider_Id.
$unwind documentation $match documentation
I was able to get the output you needed using $project:
I modified rays answer a little bit and just added the project aggregate 🙂 My playground is here Mongo Playground