|
The only question is: What happens, when the cursor is at $$ROOT level and I call $$PARENT ? I think it has to throw an exception, because it's always possible to avoid that call, i.e.:
$cond: {
|
if: { $eq: [ "$$CURRENT", "$$ROOT" ] },
|
then: "...",
|
else: "..."
|
}
|
|
|
benneq, if I understand correctly your use case I think can make it work using $$ROOT. For example, given the following documents:
> db.foo.find().pretty()
|
{
|
"_id" : ObjectId("54c9119d20e365e24cb28be1"),
|
"id" : 1,
|
"i18n" : {
|
"en" : {
|
"foo" : "english1",
|
"_lang" : "en"
|
},
|
"jp" : {
|
"foo" : "japanese1",
|
"_lang" : "jp"
|
}
|
}
|
}
|
{
|
"_id" : ObjectId("54c9119d20e365e24cb28be2"),
|
"id" : 2,
|
"i18n" : {
|
"en" : {
|
"foo" : "english1",
|
"_lang" : "en"
|
}
|
}
|
}
|
you can run the following:
> db.foo.aggregate([ { $redact: { $cond: { if: { $or: [ { $not : "$_lang" }, { $eq: [ "$_lang", "jp" ] }, { $and: [ { $eq: [ "$_lang", "en" ] }, { $not: "$$ROOT.i18n.jp" } ] }, ] }, then: "$$DESCEND", else: "$$PRUNE" } }}]).pretty()
|
{
|
"_id" : ObjectId("54c9119d20e365e24cb28be1"),
|
"id" : 1,
|
"i18n" : {
|
"jp" : {
|
"foo" : "japanese1",
|
"_lang" : "jp"
|
}
|
}
|
}
|
{
|
"_id" : ObjectId("54c9119d20e365e24cb28be2"),
|
"id" : 2,
|
"i18n" : {
|
"en" : {
|
"foo" : "english1",
|
"_lang" : "en"
|
}
|
}
|
}
|
Hope this helps.
|