My objects looks like the following
{
id: 1,
a: "val7f",
b: "online",
c: "reg",
d: "retd"
},
{
id: 2,
a: "val6f",
b: "online",
c: "reg",
d: "new"
},
{
id: 3,
a: "val6f",
b: "offline",
c: "non-reg",
d: "new"
}
I want to build a query in the following manner.
if (a=="val7f" && b=="offline" && c="reg") has objs > 1 return result Objs
else if(a=="val7f" && b=="offline" && c="reg") has objs > 1 return result objs
etc.
I built the following query but values displayed are from else
query even if there are results for the if case:
db.Collection.aggregate([
{ $redact: {
$cond: {
if: {
$and: [
{ $eq: [ "a", "val7f" ] },
{ $eq: [ "b", "offline" ] },
{ $eq: [ "c", "reg" ] }
]
},
then: "$$DESCEND",
else: {
$cond: {
if: {
$and: [
{ $eq: [ "$a", "val6f"] },
{ $eq: [ "b", "offline"] },
{ $eq: [ "c", "reg"] }
]
},
else: "$$PRUNE"
}
}
}
} }
]);
Summarised as follows
I've put condition as follows
if: {
$and: [
{ $eq: [ "a", "val7f" ] },
{ $eq: [ "b", "offline" ] },
{ $eq: [ "c", "reg" ] }
]
}
if this has result say
{
id: 6,
a: "val7f",
b: "online",
c: "reg",
d: "retd"
}
it should return the above document. in case there are no document matching criteria
else case should execute
if: {
$and: [
{ $eq: [ "$a", "val6f"] },
{ $eq: [ "b", "offline"] },
{ $eq: [ "c", "reg"] }
]
}
and if there is match for this say
{
id: 11,
a: "val6f",
b: "offline",
c: "reg",
d: "new"
}
In my case, for the query built above I'm getting both the documents in the output shell.
Aucun commentaire:
Enregistrer un commentaire