The project stack is Django MongoDB (djongo library).
I have order table with data structure like this
{
name:"Help",
price:123,
products:[
{
"_id":"some id",
"organization":{
"id":1
}
},
{
"_id":"some id2",
"organization":{
"id":2
}
}
]
}
How can I get list order which has item in 'products' set where organization id field is equal 1?
When I will get this list how can I delete products set items where organization id field isn't equal 1?
So I should get something like this (Price field shouldn't be too)
{
name:"Help",
products:[
{
"_id":"some id",
"organization":{
"id":1
}
}
]
}
CodePudding user response:
Query
$filter
products and keep only those withorganization.id=1
- remove price field (
$$REMOVE
is system variable, and if a field gets this value its removed)
*you can use project also it is the same, but $set
leaves all other fields you possible have unchanged
aggregate(
[{"$set":
{"price":"$$REMOVE",
"products":
{"$filter":
{"input":"$products",
"cond":{"$eq":["$$this.organization.id", 1]}}}}}])