Reputation: 517
I have many document in collection I want to run a aggregation query. Let say sample doc are like as shown below
{"A":1,"B":2, "c":"x"}
{"A":3,"B":2, "c":"play"}
{"A":1,"B":2, "c":"test"}
to these doc I want to run a OR query and also I want to return number of or condition matched. for example if query is
{"A":1, "A":3, "c":"test"}
it should return result like
[
{"A":1,"B":2, "c":"x", "count":1}
{"A":3,"B":2, "c":"play", "count", 1}
{"A":1,"B":2, "c":"test", "count":2}
]
Upvotes: 0
Views: 48
Reputation: 49945
You can use $addFields and evaluate your value based on specified conditions:
db.col.aggregate([
{
$addFields: {
count: {
$add: [
{ $cond: [ { $eq: ["$A", 1] }, 1, 0 ] },
{ $cond: [ { $eq: ["$A", 3] }, 1, 0 ] },
{ $cond: [ { $eq: ["$c", "test"] }, 1, 0 ] },
]
}
}
},
{
$match: {
"count": { $gt: 0 }
}
}
])
Upvotes: 1