[
{
$sort: {
_id: -1,
},
},
{
$lookup: {
from: "SalesHub_Contacts",
localField: "contactid",
foreignField: "contactid",
as: "contactid",
},
},
{
$addFields: {
contactid: {
$arrayElemAt: ["$contactid", 0],
},
},
},
{
$addFields: {
contactid: {
$concat: [
{
$arrayElemAt: [
[
"$contactid.contactid",
"$contactid.firstname",
"$contactid.lastname",
],
0,
],
},
{
$reduce: {
input: [
{
$concat: [
" (",
"$contactid.firstname",
],
},
{
$concat: [
", ",
"$contactid.lastname",
],
},
],
initialValue: "",
in: {
$concat: ["$$value", "$$this"],
},
},
},
{
$cond: {
if: {
$gt: [
{
$size: {
$ifNull: [
[
{
$concat: [
" (",
"$contactid.firstname",
],
},
{
$concat: [
", ",
"$contactid.lastname",
],
},
],
[],
],
},
},
0,
],
},
then: ")",
else: "",
},
},
],
},
},
},
{
$lookup: {
from: "SalesHub_Accounts",
localField: "accountid",
foreignField: "accountid",
as: "accountid",
},
},
{
$addFields: {
accountid: {
$arrayElemAt: ["$accountid", 0],
},
},
},
{
$addFields: {
accountid: {
$concat: [
{
$arrayElemAt: [
[
"$accountid.accountid",
"$accountid.accountname",
],
0,
],
},
{
$reduce: {
input: [
{
$concat: [
" (",
"$accountid.accountname",
],
},
],
initialValue: "",
in: {
$concat: ["$$value", "$$this"],
},
},
},
{
$cond: {
if: {
$gt: [
{
$size: {
$ifNull: [
[
{
$concat: [
" (",
"$accountid.accountname",
],
},
],
[],
],
},
},
0,
],
},
then: ")",
else: "",
},
},
],
},
},
},
{
$match: {
role: {
$regex: "^dire",
$options: "i",
},
},
},
].
This is my query. I am trying to lookup a feild and modify the content of the forign feild. and I am trying to make search with that using $match and $regex. I created a index too. Not working.