Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Shaxadhere/6ce8336cd6145cdc849ef2bbc8e54d91 to your computer and use it in GitHub Desktop.
Save Shaxadhere/6ce8336cd6145cdc849ef2bbc8e54d91 to your computer and use it in GitHub Desktop.
db.collection.aggregate([
{
"$facet": {
"caseByTitle": [
{
"$match": {
"caseTitle": {
"$regex": ".*godrej.*",
"$options": "i"
}
}
}
],
"caseByCourtName": [
{
"$match": {
"caseTitle": {
"$regex": ".*madhya.*",
"$options": "i"
}
}
}
]
}
},
{
"$addFields": {
"tempCases": {
"$concatArrays": [
"$caseByTitle",
"$caseByCourtName"
]
}
}
},
{
$addFields: {
cases: {
$reduce: {
input: "$tempCases",
initialValue: [],
in: {
$concatArrays: [
"$$value",
{
$cond: [
{
$in: [
"$$this._id",
"$$value._id"
]
},
[],
[
"$$this"
]
]
}
]
}
}
}
}
},
{
"$project": {
cases: 1
}
}
])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment